Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a function to display the children of the page in the sidebar php file using wp_list_pages:

global $post; ?>
        <div id="subChildren">
        <?php if($post->post_parent)
        $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$post->post_parent."&echo=0"); else
        $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$post->ID."&echo=0");
        if ($children) { ?>
        
        <ul id="submenu">
        <?php echo $children; ?>
         
        </ul>

I have also created a shortcode that shows a custom post type using query posts:

function casestudy_shortcode()
    {
    extract(shortcode_atts(array(
            'type' => 'case_studies',
            'limit' => '1',
            'case' => '',
            'size' => 'small'
            ),$atts));
     
            //The Query
        query_posts('post_type='.$type.'&showposts='.$limit.'&p='.$case);

        //The Loop
       if ( have_posts() ) : while ( have_posts() ) : the_post();
                   //return sb_post_image('100','100');
                //the_post_thumbnail('portfolio'); 
                return  "<div class='".$size."'><a href=\"".get_permalink()."\">".get_the_title()."</a>" . get_the_excerpt() ."</div>";;
      
        endwhile; else:
    endif;
     
        wp_reset_query();    
        
    }
    add_shortcode('casestudy', 'casestudy_shortcode');

The problem I have is that when the shortcode is in a page it makes the sub pages not show up. I assume it is overriding the $post variable.

I have tried using wp_query and get_posts for the shortcode but i get the same result.

Is there a way around this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

you really shouldn't use query_posts() for anything other then the main query of the page. Instead you should use wp_query() or get_posts() , also since you return a value before running wp_reset_query() then its never actually being reset.

so change your shortcode to this:

function casestudy_shortcode($atts){
    extract(shortcode_atts(array(
            'type' => 'case_studies',
            'limit' => '1',
            'case' => '',
            'size' => 'small'
            ),$atts));

    //save the real $post
    global $post; 
    $real = $post;

    //The Query
    $args = array(
        'post_type' => $type,
        'posts_per_page' => $limit);
    if ($case != ''){
        $args['p'] = $case;
    }

    $s_query = NEW WP_Query($args);


    //The Loop
    if ( $s_query->have_posts() ) : while ( $s_query->have_posts() ) : $s_query->the_post();
        //return sb_post_image('100','100');
        //the_post_thumbnail('portfolio'); 
        $return .= "<div class='".$size."'><a href=\"".get_permalink()."\">".get_the_title()."</a>" . get_the_excerpt() ."</div>";
    endwhile; else:
    endif;
     $post = $real;
        wp_reset_query();    
        return $return;

}
    add_shortcode('casestudy', 'casestudy_shortcode');
share|improve this answer
    
Perfect thanks worked like a charm :) I did try using wp_query but it didn't work for me as you rightly pointed out my reset was in the wrong place. –  Lee Jun 15 '11 at 22:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.