• Resolved calex3710

    (@calex3710)


    Hi,

    My apologies if this has been answered elsewhere. I know that people have tackled a similar problem here but I’m new to PHP and am having trouble implementing other given fixes in my code.

    I have a conditional tag in my footer so that certain elements from it don’t display on a certain page (page 15):

    <?php if( !is_page( '15' ) ) : ?>

    Within page 15, I have code to call up a certain post type (“Accessories”):

    <?php
    $type = 'Accessories';
    $args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'paged' => $paged,
     'posts_per_page' => 3,
    'caller_get_posts'=> 1
    );
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    ?>
    <?php 					while(have_posts()):the_post();
    the_content();endwhile;?>

    I know the latter code is breaking the former code in some way. I think this must be a problem with the WP_Query function but am clueless about how to fix it. Any help would be much appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Try adding <?php wp_reset_query();?> after that custom Loop.

    Thread Starter calex3710

    (@calex3710)

    Thanks for your response Esmi. Like what I did below? It doesn’t seem to work (again, forgive me if I’m doing something stupid).

    <?php
    $type = 'Accessories';
    $args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'paged' => $paged,
    'posts_per_page' => 3,
    'caller_get_posts'=> 1
    );
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    ?>
    <?php while(have_posts()):the_post();
    the_content();endwhile;?>
    <?php wp_reset_query();?>

    Sorry – try <?php wp_reset_postdata();?>.

    Thread Starter calex3710

    (@calex3710)

    Darn it, still does nothing. Any other possibilities? Thanks again.

    Darn it! (polite version) wp_reset_postdata() should work according to the documentation.

    What if we try this from a completely different angle. Grab the page id as a variable before you set up WP_Query and then run a conditional on the value of this variable lower down. Worth a try?

    Try using:

    $temp = clone $wp_query;

    instead of:

    $temp = $wp_query;

    Then, you should have:

    $wp_query = clone $temp;

    after your loop to reassign the original query.

    Thread Starter calex3710

    (@calex3710)

    Curtiss — that worked! Thank you!! And thanks Esmi, it’s possible your solution works too but I haven’t tried yet.

    I love forum boards — they restore my faith in humanity.

    Complete code below for anyone else facing this problem:

    <?php
    $type = 'Accessories';
    $args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'paged' => $paged,
    'posts_per_page' => 3,
    'caller_get_posts'=> 1
    );
    $temp = clone $wp_query; // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    ?>
    <?php
    while(have_posts()):the_post();
    the_content();endwhile;?>
    <?php $wp_query = clone $temp;?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help with get_post breaking conditional tag’ is closed to new replies.