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'm really new to WordPress and having a problem with my page template and a sidebar loop. I made a block that displays posts in a category in the sidebar, it also displays a custom field below the title of each post and the code looks like this:

<?php $published = get_post_meta( $post->ID, 'article_Details', true ); ?>

<div id="sidebar">
  <div id="latest">
    <h2>Recent Articles</h2>
    <?php 
        $catquery = new WP_Query( 'cat=2&posts_per_page=6' ); 
        while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <a class="listing" href="<?php the_permalink() ?>"><?php the_title(); ?></a>

    <p class="pubdetails"><?php echo $published[0]['publication-details']; ?></p>

    <?php endwhile; ?>

  </div></div>

It gets displayed properly on single posts, categories and homepage but for every page on the site where it should be, it breaks, only the first item in the block gets displayed without the custom field and the foooter of the site never appears.

The template I use for my pages looks like this:

<?php get_header(); ?>

<div id="main">
    <?php while (have_posts()) : the_post(); ?>

    <article <?php post_class(); ?>>

        <header class="entry-header">

            <h1 class="entry-title"><?php the_title(); ?></h1>

        </header>

        <div class="entry-content">

            <?php the_content(); ?>

        </div>

    </article>
    </div>

<?php endwhile; ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

I'm not sure what the issue is, I've tried changing several things to fix the code in both the page template and the sidebar template without any luck so far.

share|improve this question

2 Answers 2

Well for one thing I can spot that it seems your endwhile loop is closing OUTSIDE of a stray DIV tag. Try fixing that.

share|improve this answer

You are basically changing the loop in the side bar and that might be affecting your loop in the main template. For your loop to remain in tact i would suggest you do some thing like this for the side bar code.

$temp = $wp_query;
<?php 
        $catquery = new WP_Query( 'cat=2&posts_per_page=6' ); 
        while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <a class="listing" href="<?php the_permalink() ?>"><?php the_title(); ?></a>

    <p class="pubdetails"><?php echo $published[0]['publication-details']; ?></p>

    <?php endwhile; ?>
$wp_query = $temp;

Hope this helps!

share|improve this answer

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.