0

I am using the Starker Theme as a foundation to build my custom WordPress theme. The Starker Theme comes with a loop.php file for setting up the main loop. I decided to use the loop.php file to display content on the homepage of my site. I would like to incorporate this loop as well as the pagination on other pages of my site but I would only like to query specific categories. Below is the code I have in my loop.php. What's the best way to setup other pages or templates on my site and have their own unique queries without conflicting with the main loop?

            <!-- 960 16 Column Grid -->
            <div class="container_16"> 

            <!-- Featured News -->
            <section class="grid_10 featured-news">

            <!-- Featured News Heading -->
            <h1></h1>
            <!-- /Featured News Heading -->

            <!-- Featured News Loop -->
            <?php
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args=array(
            'post_type'=>'post',
            'cat' => 'featured-news',
            'posts_per_page' => 2,
            'paged'=>$paged
            );
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query($args);

            if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
            if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
            ?> 
            <!-- /Featured News Loop -->

            <!-- Post --> 
            <article class="post" id="post-<?php the_ID(); ?>" <?php post_class(); ?> >

            <!-- Featured News Title -->
            <span><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </span>
            <!-- /Featured News Title -->

            <!-- Featured News Meta -->
            <p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date('m.d.Y'); ?></p>
            <!-- /Featured News Meta -->

            <!-- Featured News Thumbnail -->
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(640,320)); ?></a>
            <!-- /Featured News Thumbnail -->

            <!-- Featured News Excerpt -->
            <p class="featured-news-excerpt"><?php the_excerpt(); ?></p>
            <!-- /Featured News Excerpt -->

            <!-- Featured News Social Links -->
            <?php include('includes/social.php'); ?>
            <!-- /Featured News Social Links -->

            </article>
            <!-- /Post -->

            <?php
            endwhile; endif;
            /* PageNavi at Bottom */
            if (function_exists('wp_pagenavi')){wp_pagenavi();}
            $wp_query = null;
            $wp_query = $temp;
            wp_reset_query();
            ?>
            <!-- /Featured News Loop -->

            <!-- Pagination -->
            <?php if ($wp_query->max_num_pages > 1) : ?>
            <div class="grid_10 pagination older-news">
            <?php next_posts_link( __( '<span class="arrow">&larr;</span> Older News', 'twentyten' ) ); ?>
            <div class="pagination newer-news">
            <?php previous_posts_link( __( 'Newer News <span class="arrow">&rarr;</span>', 'twentyten' ) ); ?>
            </div>
            </div>

            <?php endif; ?>
            <!-- /Pagination -->

            </section>
            <!-- /Featured News -->

            <!-- Other News -->
            <aside class="grid_5 other-news">

            <!-- Other News Heading -->
            <h3></h3>
            <!-- /Other News Heading -->

            <!-- Other News Loop -->
            <?php query_posts('category_name=other-news&showposts=6'); ?>
            <?php while (have_posts()) : the_post(); ?>

            <!-- Other News List -->
            <ul>
            <li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </li>
            <li class="other-news-post-date"><?php echo get_the_date('d.m.Y'); ?>
            </li>
            </ul>
            <!-- /Other News List -->

            <?php endwhile; ?>
            <!-- /Other News Loop -->

            <!-- View More -->
            <div class="view-more">
            <a href="/other-news">View More <span class="arrow">&rarr;</span></a>
            <div>
            <!-- /View More -->

            </aside>
            <!-- /Other News -->

            </div>
            <!-- /960 16 Column Grid -->

2 Answers 2

1

In your main loop file, check the $args. It filters the query. you can use the same query on other pages using your own options.

suppose, on another page, you want to fetch the posts of a different category. your query parameters may be like:

$args = array(
  'post_type' => 'post',
  'cat' => 'DIFFERENT_CATEGORY',
  'posts_per_page' => 2,
  'paged' => $paged
);

change the DIFFERENT_CATEGORY with specific category ID.

then you can pass these arguments to WP_Query as in your loop.php.

if your page has multiple such queries, you should use wp_reset_query before running further query. More her: http://codex.wordpress.org/Function_Reference/wp_reset_query

Does it help?

1
  • tried setting up a template called and following these instructions (which were very straightforward - thank you); however, my page still fetches the same exact content as the homepage (main loop.php). For example my site: example.com has the loop.php as specified above and the other page I setup the new instance of the loop on with its own unique arguments example.com/other-news pulls in the same exact content...Not sure what I'm doing wrong?
    – Matt
    Commented Jun 18, 2012 at 16:18
1

create a new loop-otherpage1.php (with a different otherpage1 name) for each of these other pages; http://codex.wordpress.org/Function_Reference/get_template_part

within the loop-otherpage1.php templates, use different category parameters for the query; http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

in the page templates, use: <?php get_template_part('loop','otherpage1'); ?> to call this loop template - with a different otherpage1 for each of the templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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