Title: Quick PHP Question
Last modified: August 18, 2016

---

# Quick PHP Question

 *  [mlanger](https://wordpress.org/support/users/mlanger/)
 * (@mlanger)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/)
 * I’m not a PHP expert, but I can usually accomplish tasks by example.
 * I’m trying to use if/then logic AFTER this to display certain components of my
   sidebar on only some pages. My IF statement works fine if I place it above the
   following code, but it’s ignored by WordPress if I place it below the code. So
   I’m thinking that there’s something wrong with this code that disables if/then
   logic. The code came with my template and I didn’t modify it.
 * Anyone who is well-versed in PHP should see a glaring error here (if there is
   one):
 *     ```
       <h2>Recently Posted</h2>
       <ul>
       <?php query_posts('showposts=10'); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a><span class="recent_date"><?php the_time('n.j') ?></span></li>
       <?php endwhile; endif; ?>
       </ul>
       ```
   
 * I think the problem might be with the “punctuation” (: vs. ;), but I really have
   no clue.

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650631)
 * If the code from above works well in itself (without your if statements) – then
   it must be OK.
 * However, being a (mini)Loop, placing the if statements AFTER it I guess the if…
   checks the Loop that is just above/before it and not the main Loop of the page
   that is displayed. Just a hunch because I am not a coder.
 *  Thread Starter [mlanger](https://wordpress.org/support/users/mlanger/)
 * (@mlanger)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650632)
 * I should mention here that I think the error might be in the IF and WHILE portions
   of the code or the ENDWHILE or ENDIF portions. The rest should be right. The 
   code works perfectly as written.
 *  Thread Starter [mlanger](https://wordpress.org/support/users/mlanger/)
 * (@mlanger)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650633)
 * Moshu, I think you’re on the right track. I’m hoping someone with more coding
   expertise can help.
 *  [Jeremy Clark](https://wordpress.org/support/users/jeremyclark13/)
 * (@jeremyclark13)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650637)
 * Try this instead
 *     ```
       <h2>Recently Posted</h2>
       <ul>
       <?php query_posts('showposts=10'); ?>
         <?php while (have_posts()) : the_post(); ?>
       <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a><span class="recent_date"><?php the_time('n.j') ?></span></li>
       <?php endwhile; ?>
       </ul>
       <?php rewind_posts(); ?>
       ```
   
 * You know that the query will show posts, so no reason to check if you will have
   posts. The last bit of code resets the loop if your going to be doing anything
   else with the loop.
 *  Thread Starter [mlanger](https://wordpress.org/support/users/mlanger/)
 * (@mlanger)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650639)
 * Jeremy, I’ll try it right now and report back. Thanks for the quick response.
 *  Thread Starter [mlanger](https://wordpress.org/support/users/mlanger/)
 * (@mlanger)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650645)
 * Nope. That didn’t do it.
 * Although the code you provided above does work, the problem I’m having didn’t
   go away.
 * Perhaps it’s my own code. Here’s what I have now:
 * _…sidebar stuff_
 *     ```
       <!-- RECENTLY POSTED -->
       <li class="widget">
       <h2>Recently Posted</h2>
       <ul>
       <?php query_posts('showposts=10'); ?>
       <?php while (have_posts()) : the_post(); ?>
       <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a><span class="recent_date"><?php the_time('n.j') ?></span></li>
       <?php endwhile; ?>
       </ul>
       <?php rewind_posts(); ?>
       </li>
   
       <!-- ONLY ON HOME OR ARCHIVE PAGES -->
       <?php if ( is_home() || is_archive() ) { ?>
       ```
   
 * _…stuff I want to appear only on the Home or Archive pages…_
 *     ```
       <!-- END ONLY ON HOME OR ARCHIVE -->
       <?php } ?>
       ```
   
 * _…more sidebar stuff…_
 * Is my conditional statement in error? I’m clueless when it comes to the : and;
   thing.
 * I should note here that my theme has two sidebars that appear side by side. The
   above code also “turns off” the IF/THEN logic in the other sidebar, which appears
   to its right.
 * However, when I place my IF statement above the RECENTLY POSTED stuff, everything
   works fine.
 *  [Jake Bohall](https://wordpress.org/support/users/jblifestyles/)
 * (@jblifestyles)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650856)
 * you are trying to use php in the sidebar widget.. I don’t believe they offer 
   this functionality standard. (I’m assuming your placing this code into one of
   the text widgets) If this is the case, download the phpwidget plugin, and place
   your code there..
 * if your not doing this in a widget.. try this change..
 *     ```
       <?php while (have_posts()) : the_post() { ?>
       <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a><span class="recent_date"><?php the_time('n.j') ?></span></li>
       <?php } ?>
       ```
   
 *  [Jake Bohall](https://wordpress.org/support/users/jblifestyles/)
 * (@jblifestyles)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650857)
 * good luck!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Quick PHP Question’ is closed to new replies.

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 4 participants
 * Last reply from: [Jake Bohall](https://wordpress.org/support/users/jblifestyles/)
 * Last activity: [18 years, 4 months ago](https://wordpress.org/support/topic/quick-php-question-2/#post-650857)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
