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.

So after several attempts to correct this - It seems the pages not load but don't pull in the correct posts from each category. It just seems to pull in the same default posts whenever I click each category link.

I've attempted to customise the query but with no luck here is the code I'm using below.

<?php global $up_options, $post, $paged, $wp_query; ?>
<?php get_header(); ?>
<div id="bodywrapper">
        <?php $args = array(
            'title_li'           => __( '' ),
        ); ?>
        <div class="category-list"><?php wp_list_categories($args); ?></div>
    <div style="height: 25px"></div>

    <div id="content"> 
        <h1>Case Studies</h1>

  <?php

/* retrieves all the terms from the taxonomy portfolio */

$args = array(
    'type' => 'post',
    'orderby' => 'name',
    'order' => 'ASC',
    'taxonomy' => 'category');

$categories = get_categories( $args );

/* pulls 5 posts from each of the individual categories */

foreach( $categories as $catergory ) {
    $args = array(
    'posts_per_page' => 5,
    'post_type' => 'post',
    'category' => $category->slug,
    'no_found_rows' => true,
    'update_post_meta_cache' => false,
    'update_post_term_cache' => false
    );
}

$the_query = new WP_Query ( $args );
    // the loop
while ( $the_query->have_posts()) : $the_query->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="post">
                        <a href="<?php the_permalink() ?>">
                            <?php if (has_post_thumbnail()) { ?>
                                <?php the_post_thumbnail('thumbnail'); ?>
                            <?php }?>
                        </a>
                        <h1 class="post-title"><a href="<?php  echo get_post_meta($post->ID, 'lj_file_url', true) ?>" title="<?php the_title(); ?>"> <?php the_title(); ?></a></h1>
                        <div class="post-entry">
<a href="<?php echo get_post_meta($post->ID, 'lj_file_url', true) ?>" title="Download pdf <?php the_title(); ?>"> <img src="http://www.integra-av.co.uk/wp-content/uploads/2012/04/pdflogo.png" alt="" class="alignleft"/></a>
                            <?php the_excerpt(); ?>
                        </div>
                    </div>


                      <?php endwhile;

                       ?>

 <?php // Reset Post Data
wp_reset_postdata(); ?>

                <div class="clear">
                    <?php wpld_pagenavi(); ?>
                </div>

<br/><br/><br/><br/><br/><br/>

<p style="text-align: center;"><span style="font-size: 35px;"><span style="color: #2788e4;">Secure Solutions</span></span></p>
    </div>
    <?php get_footer(); ?>

here is a link to the current page I'm working on here

share|improve this question
    
Why are you using a custom page template, rather than relying on the Template Hierarchy to display the category archive index? –  Chip Bennett Oct 16 '13 at 12:58
    
it is my category.php –  dannyw24 Oct 16 '13 at 13:34

1 Answer 1

up vote 1 down vote accepted

Your foreach is closed prematurely.

foreach( $categories as $catergory ) {

    // query args
    $args = array(
        // args
    );
}

// generate the loop
$the_query = new WP_Query ( $args );

// output the loop
while ( $the_query->have_posts()) : $the_query->the_post(); 
    // Loop markup
endwhile;

wp_reset_postdata();

You're saying:

  1. Step through each of the categories; for each category, add query results to $the_query.
  2. After stepping through the categories, output a loop for $the_query

So, you're only ever going to display the loop for the last category in $categories.

Instead, you need to put the loop output inside your foreach loop:

foreach( $categories as $catergory ) {

    // query args
    $args = array(
        // args
    );

    // generate the loop
    $the_query = new WP_Query ( $args );

    // output the loop
    while ( $the_query->have_posts()) : $the_query->the_post(); 
        // Loop markup
    endwhile;

    wp_reset_postdata();
}

That way, you output a loop for each category, rather than just the last category.

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.