• mlanger

    (@mlanger)


    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

    (@moshu)

    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

    (@mlanger)

    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

    (@mlanger)

    Moshu, I think you’re on the right track. I’m hoping someone with more coding expertise can help.

    Jeremy Clark

    (@jeremyclark13)

    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

    (@mlanger)

    Jeremy, I’ll try it right now and report back. Thanks for the quick response.

    Thread Starter mlanger

    (@mlanger)

    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

    (@jblifestyles)

    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

    (@jblifestyles)

    good luck!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Quick PHP Question’ is closed to new replies.