Help coding PHP for WordPress
February 2, 2007 6:39 AM   Subscribe

I need some help coding PHP for WordPress.

I'm an utter amateur at PHP, but my site redesign is slowly coming together. I now find myself stuck, though. I'm trying to write code for my index.php that will:

1) Detect that it's the home page. I know this involves php if (is home) somehow.
2) Run my Project Wonderful ad code.
3) Display only posts from Category 1, using the same Loop structure as everywhere else. I know this involves query_posts somehow.
4) If it's not the home page, don't show the ads, show whatever category people want, and proceed as normal.

Gurus, can you help me out?

(Incidentally, I've E-mailed this question to a couple of helpful MeFites already, and I'm eternally grateful for all their previous assistance, but frankly I'm impatient for an answer, so I thought I'd toss it out to the hive mind. I apologize profusely if anyone feels slighted by this.)
posted by Faint of Butt to Computers & Internet (7 answers total) 1 user marked this as a favorite
 
http://www.php.net/reserved.variables

see entry under PHP_SELF.

Learn how to contruct an if else statement.
posted by ReiToei at 7:07 AM on February 2, 2007


basic if else statements in PHP
posted by ReiToei at 7:10 AM on February 2, 2007


very roughly...

< ?phpbr>
$mypage = $_SERVER['PHP_SELF'];

if($mypage !== 'index.html') {
echo $projectwonderfulcode;
} else {
}

?>
posted by ReiToei at 7:15 AM on February 2, 2007


I've used wordpress but haven't hacked it much, what you want are their conditionals.

See Conditional Tags

so in this case to see if you're on the home page its

if ( is_home()) {
.... do stuff here
}

I don't have time to look up the category loop right now
posted by bitdamaged at 7:49 AM on February 2, 2007


To display only category 1, I believe you can put:

$cat = "1";

before:

require('wp-blog-header.php');

in your index.php file.
posted by null terminated at 7:59 AM on February 2, 2007


Conditionnal Tags it is. You also have Template Tags :
http://codex.wordpress.org/Template_Tags

For categories, you wish to dive into The Loop using in_category('42') (or !in_category('42') ) :
http://codex.wordpress.org/The_Loop
http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category
posted by XiBe at 9:58 AM on February 2, 2007


query_posts works like this:

<?php query_posts('cat=1'); ? >

<?php while (have_posts()) : the_post(); ?>
<!-- Do your inside-the-loop stuff here -->
<?php endwhile;?>


your is_home code would look like this:

<?php if(is_home()) {
<!--put your project wonderful code here-->
} else {
<!-- put what goes on all the other pages here-->
} ?>


But you may not need the "else" statement, if what goes there is nothing when it's not the home page.

If you put the two together:
<?php if(is_home()) {
query_posts('cat=1');

while (have_posts()) : the_post(); ?>
<!-- Do your inside-the-loop stuff here -->
<?php endwhile;
} else {
while (have_posts()) : the_post(); ?>
<!-- Do your inside-the-loop stuff here -->
<?php endwhile;
} ?>

I'm assuming that you're putting this code in the sidebar. If not, if you're putting it in the main body of the page, you may not need is_home at all. It may make more sense for you to exploit the template hierarchy instead.
posted by eustacescrubb at 7:51 PM on February 2, 2007


« Older How does Tupper turn a number into a map?   |   Get an internship in America Newer »
This thread is closed to new comments.