Page Template with Categories
-
Hi guys, hopefully some of you might be able to help me with this.
I have made a page template which (awesomely!) displays a page and then gets a set number of posts from the category which has the same name of that page… Very useful.
However, I wish to put next and previous links so that users can scroll through to older posts in that category.
Problem is, the standard<?php next_posts_link('« Older Entries') ?> <?php previous_posts_link('Newer Entries »') ?>
Doesn’t work – I’m assuming because the next and previous links are referring to the page, and not the category of posts….
Below is the page template. Feel free to use if you are looking for something similar – but what I really need is to figure out how to get those Older/Newer Post links working. Then it’ll be the ultimate page template!
<?php /* Template Name: Page + Category */ ?> <?php get_header(); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div id="accordion"> <?php //Gets the name of the category // Match to Page $cat_list = get_categories(); //Print Category Array --> print_r($cat_list); foreach($cat_list as $cat) { //gets Category name from Array $catName = $cat->category_nicename . ' '; //Page Title $post_obj = $wp_query->get_queried_object(); $pageName = $post_obj->post_name; //cut whitespace $pN = trim($pageName); $cN = trim($catName); //if category name and pagename is the same, assign id Number -> display later. //or if post is part of two categories (ie 7 and 8 would be 7,8) - added comma before and after $pN if(($cN == $pN)||($cN == $pN . ',')||($cN== ',' . $pN)){ $id = $cat->cat_ID . ' '; $id = trim($id); } } ?> <div class="post-top" id="post-<?php the_ID(); ?>"> <h2 class="toggler"><?php the_title(); ?></h2> <div class="element"> <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?> <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> </div> </div> <?php //$id from start of page... $posts = get_posts( "category=" . $id . "&numberposts=3&order=DESC" );?> <?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?> <div class="post"> <h2 class="toggler"><?php echo $post->post_title; ?></h2> <div class="element"> <?php the_content('Read the rest of this entry »'); ?> </div> </div> <?php endforeach; ?> <?php endif; ?> </div><!-- End of Accordion --> <ul id="new-old-links"> <li><?php next_posts_link('« Older Entries') ?></li> <li><?php previous_posts_link('Newer Entries »') ?></li> </ul> <?php endwhile; ?> <?php endif; ?> <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?> </div><!-- End of class content--> <?php //get_sidebar(); ?> <?php get_footer(); ?>
- The topic ‘Page Template with Categories’ is closed to new replies.