• I’m using the following code to display a sinlge upcoming event at the very top of my websites home page, above the “blog” that will display all of the posts from another category.

    <div id="home-nextevent">
    
    <?php query_posts('category_name=events&showposts=1&meta_key=sortdate&orderby=meta_value&order=asc'); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    
    <?php endwhile; ?>
    
    </div> <!-- closes "home-nextevent" -->

    However, this code appears to affect the remainder of the website, causing my blog section (still on the home/index.php page) to only display a single post.

    I’m a PHP beginner, and I’m wondering if the event code needs to be terminated somehow, so that it doesn’t mess the rest of the page up. Is there an easy fix for this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Each call to query_posts() destroys the previous one. One call is made before your template is loaded, and your call overwrites it. So, the second loop just repeats the first one.

    You must either save and restore the query, or use WP_Query() instead of query_posts(). Here is how you would save the query:

    <?php $temp_query = $wp_query; ?>
    <div id="home-nextevent">
    
    <?php query_posts('category_name=events&showposts=1&meta_key=sortdate&orderby=meta_value&order=asc'); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    
    <?php endwhile; ?>
    
    </div> <!-- closes "home-nextevent" -->
    <?php $wp_query = $temp_query; ?>
    Thread Starter Chris H

    (@chris-h)

    Hi there,

    Thanks so much for your reply. I tried putting the code you posted into my template, but couldn’t get anything to show up. Is there something I’m still not doing right, do you think?

    Thanks again.

    Almost impossible to say without seeing the code. Please put your code into a pastebin and post a link to it here.

    Thread Starter Chris H

    (@chris-h)

    Thanks for the tip on Pastebin. I’ve never used that before. ??

    https://pastebin.com/iWJiHemU

    Cheers,

    Chris

    The code in question is not in the pastebin.

    Does the code you posted at first show anything? I thought that it showed the correct post, but messed up the display of the following posts. If that is not the case, get that code working and put it between the save and restore:

    <?php $temp_query = $wp_query; ?>
    
       <!-- Your working code here -->
    
    <?php $wp_query = $temp_query; ?>
    Thread Starter Chris H

    (@chris-h)

    Seems to be working now. Not sure what I’ve done differently, as I simply copied and pasted the code both times. Bizarre. ??

    Thanks so much for your help on this. I really appreciate it.

    Cheers,

    Chris

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Terminating PHP?’ is closed to new replies.