0

Hi I've been googling for a while and I can't find help for this. I've got a page template that is listing a bunch of custom posts...I'm using this code before the loop to display my custom posts:

    <?php query_posts( array( 'post_status' => 'publish' , 'post_type' => array( 'ncp_news'     )  ) ); ?>

When I remove this to make it a normal page I can display my custom sidebar fine so I know it's got to do with the blog (custom) posts...

Here's the full code:

<div class="posts_wrapperk">    

<?php query_posts( array( 'post_status' => 'publish' , 'post_type' => array( 'nyacp_news' )  ) ); ?>

<?php if (have_posts()) : while(have_posts()) : the_post(); ?>

<?php

$sidebar_position = vp_metabox('_page_sidebar.sidebar_position');
$main_classes     = 'main';
$aside_classes    = 'aside';
if (!empty($sidebar_position)) {
$opp            = (($sidebar_position == 'left') ? 'right' : 'left');
$main_classes  .= " grid-9 $opp";
$aside_classes .= " grid-3 $sidebar_position";
}

?>

<section id="content"<?php echo qualia_build_class(array("content", "section", "color-        palette-1", "separator-{$qualia_content_separator}")); ?>>
<div class="section-inner">

    <div class="wrapper">

        <div<?php echo (!empty($sidebar_position)) ? ' class="grids"' : ''; ?>>


            <div class="<?php echo $main_classes; ?>">

                <article id="page-<?php the_ID(); ?>" <?php    post_class('original-content'); ?>>
                    <h1 class="nyacp-subheader"><?php echo get_the_title($ID); ?></h1> 
                    <?php the_content(); ?>
                </article>                      

            </div>


    </div>

</div>

    <?php //echo qualia_spacer(array('size' =>    $qualia_content_bottom_spacing)); ?>

</section>

<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

</div><!--.posts_wrapperk-->

            <?php if (!empty($sidebar_position)) : ?>
            <div class="<?php echo $aside_classes; ?> inner_sidebar">
                <?php if ( !function_exists('dynamic_sidebar') ||       !dynamic_sidebar('innerpage-sidebar') ) : endif; ?>
            </div>
            <?php endif; ?>



<?php get_footer(); ?>

How do I make my sidebar appear while displaying custom posts?

  • Why are you using query_posts. The correct way to construct a custom query is using WP_Query – Pieter Goosen Jun 14 '14 at 7:10
1

What you have is probably a bad structure... The sidebar itself should not be inside the loop since it might contain
(the widgets inside it) multiple loops that would break your code.

This is the "right structure"

  • PAGE HEADER
  • PAGE SIDEBAR (POSITION OPTIONAL)
  • CONTENT AKA LOOP
  • PAGE SIDEBAR (POSITION OPTIONAL)
  • PAGE FOOTER

As you can see we keep the sidebar and the content seperated...
here is an example using your code:

    <?php 
get_header(); 
global $qualia_content_separator, $qualia_content_top_spacing, $qualia_content_bottom_spacing; 
?>

<div class="inner_banner_news">
    <h1>In the News</h1> 
    <div class="inner_banner_feat">
        <img src="<?php bloginfo('template_directory'); ?>/images/board-image.jpg" />
    </div>
</div>

<div class="posts_wrapperk">    



<section id="content"<?php echo qualia_build_class(array("content", "section", "color-palette-1", "separator-{$qualia_content_separator}")); ?>>
    <div class="section-inner">

        <div class="wrapper">

            <?php query_posts( array( 'post_status' => 'publish' , 'post_type' => array( 'ncp_news' )  ) ); ?>

            <?php if (have_posts()) : while(have_posts()) : the_post(); ?>

            <?php

            $sidebar_position = vp_metabox('_page_sidebar.sidebar_position');
            $main_classes     = 'main';
            $aside_classes    = 'aside';
            if (!empty($sidebar_position)) {
                $opp            = (($sidebar_position == 'left') ? 'right' : 'left');
                $main_classes  .= " grid-9 $opp";
                $aside_classes .= " grid-3 $sidebar_position";
            }

            ?>

            <div<?php echo (!empty($sidebar_position)) ? ' class="grids"' : ''; ?>>


                <div class="<?php echo $main_classes; ?>">

                    <article id="page-<?php the_ID(); ?>" <?php post_class('original-content'); ?>>
                        <h1 class="nyacp-subheader"><?php echo get_the_title($ID); ?></h1> 
                        <?php the_content(); ?>
                    </article>                      

                </div>

            </div>

            <?php endwhile; endif; ?>


            <?php if (!empty($sidebar_position)) : ?>
            <div class="<?php echo $aside_classes; ?> inner_sidebar">
                <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('innerpage-sidebar') ) : endif; ?>
            </div>
            <?php endif; ?>     

    </div>

        <?php //echo qualia_spacer(array('size' => $qualia_content_bottom_spacing)); ?>

</section>


</div><!--.posts_wrapperk-->

<?php get_footer(); ?>

Its still a bit of a mess but it should work... How ever i think you would need to CSS the sidebar into the desired position.

  • Thank you :) I did realize that the sidebar should be outside the loop and I tried doing that now and earlier and it still doesn't work...it doesn't even show the sidebar in source code...the moment I remove the "query_posts" array though, the sidebar works fine so I'm not sure what's going on. Appreciate your help! – Liviness Jun 13 '14 at 23:30
  • try to add wp_reset_query(); right after this endwhile; endif; (inside the php tag of course) – Sagive SEO Jun 13 '14 at 23:47
  • That sounded promising but it's still not working. Darn. Nothing in the source code. – Liviness Jun 14 '14 at 0:08
  • P.S. Edited main post to show new structure of code. – Liviness Jun 14 '14 at 0:16
  • It had to do with this: <?php if (!empty($sidebar_position)) : ?> - that basically checks if the sidebar is positioned left or right when you create the page in the admin...it wasn't able to perform this check for some reason, so the sidebar wasn't being rendered at all (even though this works fine for pages without a post loop) - don't understand why this is so...but removing that if statement fixed my issue. Would still love to understand why if anyone can explain? – Liviness Jun 14 '14 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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