Grow your CSS skills. Land your dream job.

WP conditional tags and mini loop on pages

  • cjk
    # October 24, 2009 at 12:21 pm

    Hi,

    I would like to run a mini-loop that grabs the excerpt from posts in a specific category and displays them in a div under the content of a specific static page.

    So far what I have done is added the following to my page.php right after the ‘endif’

    Code:
    < ?php if ( is_page('letters-from-africa') ) { ?>

    this is my text

    < ?php } ?>

    This does work as expected – that is to say only on that specific page ‘letters-from-africa’ the text ‘this is my text’ appears in the new div I created and I am able to style it fine in my css file.

    So, I then tried to add a snippett of php to tell wordpress to run loop on specific category. I found this snippett here on css-tricks,

    Code:
    < ?php query_posts('cat=5'); ?>
    < ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    < ?php the_content(); ?>
    < ?php endwhile; endif; ?>

    I changed the ‘cat=5′ to the category I wanted and added this snippett to the conditional tag code as such…

    Code:
    < ?php if ( is_page('letters-from-africa') ) { ?>

    < ?php query_posts('cat=1'); ?>
    < ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    < ?php the_content(); ?>
    < ?php endwhile; endif; ?>

    < ?php } ?>

    And I get a blank page. So I am thinking it might be the second loop causing confusion and I might need to add <?php wp_reset_query(); ?> somewhere, but where? I tried before, after still just get a blank page. I must be missing something else, yes? Anyone?

    Cheers!
    Chris

    # October 24, 2009 at 6:21 pm

    Read the Codex my friend,

    http://codex.wordpress.org/Template_Tags/query_posts#Important_note

    "WordPress Codex Documentation" wrote:
    The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

    The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

    All about how to use WP_Query on this page: http://codex.wordpress.org/Function_Reference/WP_Query

    It’s pretty much equall to query_posts, except that it doesn’t override, do no need to rewind, reset or save the main loop.
    You can just WP_Query something within a page that has a loop without inteferring or being limited by either.

    If there’s any issues still, ofcourse we’re glad to answer any questions and help out with any questions, but seeing the code you got going in your first post I think you’ll get it done with WP_Query ;)

    cjk
    # October 25, 2009 at 1:43 pm

    Yeah, you know the codex is a wonderful place and full of information that makes the assumption you know what you are doing. ;-)

    For those of us trying to learn it can be frustrating though.

    I did go through that info a month ago, tried to use it and when I posted on the wp support forums not a single answer. So I gave up on that idea. Then I found a post here in the snippets area that seemed to describe a simpler way to accomplish what I was after. When it didn’t work I posted here hoping to learn why.

    In the meantime I did some more research and found some excellent explanations and more importantly examples using the $newquery = new function. 2 minutes later I had what I wanted up and live. I was also able to modify the example code and understand why, so I actually learned something.

    Simply telling a coder in learning to read the codex may seem logical to an advanced coder, I guess.

    I appreciate your reply,

    Chris

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".