• 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('&laquo; Older Entries') ?>
    <?php previous_posts_link('Newer Entries &raquo;') ?>

    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 &raquo;</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 &raquo;'); ?>
    				     </div>
    				</div>
    
    				<?php endforeach; ?>
    
    			<?php endif; ?>
    
    		</div><!-- End of Accordion -->
    
    			<ul id="new-old-links">
    			<li><?php next_posts_link('&laquo; Older Entries') ?></li>
    			<li><?php previous_posts_link('Newer Entries &raquo;') ?></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(); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter alingham

    (@alingham)

    Forgot to mention – you can see this page template in action here:
    alingham.com/v2/day
    You can see the first ‘post’ “Day” is actually the Page content… and the rest of the posts below are actual posts in the category called “Day”

    hi
    i think you have to post this after header tag :?php get_header(); ?>

    then see what happen …

    [signature moderated Please read the Forum Rules]

    This is interesting I will try it on my test blog ??

    Geez, so many useless anwers to your question… and spam too. Ain’t that nice? Grrr.

    Ok, about your question: my two cents is that it does not work since it’s a page, not a post. I’m not 100% sure, but as far as I remember from diving into the wordpress code, these “previous-next” thingies only work on archive pages (like the date, tag, category and index pages).

    What you could do is the following: use a so called “custom loop”. Google, Bing or Yahoo it, there are plenty of examples out there on how to create a “custom wordpress loop”.

    Using this custom loop, you can fetch any number of entries from any position and display them, while implementing some sort of “paging” at the same time.

    Theoretically I’m talking about something like this:

    $page = 0 + $_GET['my_page'];
    
    $data = mysql... LIMIT $page, 10");
    while(fetch_result($data)) {
        display-the-results();
    }
    
    /* and finally your requested links: */
    <ul>
    <li><a href="yourpage/?my_page=<?php echo($page-1); ?>">prev</a></li>
    <li><a href="yourpage/?my_page=<?php echo($page+1); ?>">next</a></li>
    </ul>

    Sorry I didn’t provide something to copy-and-paste, but I’m sure you get what I mean, don’t you?

    Probably needs some tweaking and securing here and there, but it’s in theory a good way to make it work if everything else fails. To get rid of the “?my_page=…” you could even dive in deeper and make it more beautifull by using new, related rewrite rules in your .htaccess file; depending on your needs.

    Hope that helps?

    btw: Have a merry xmas y’all!

    hi alingham,

    thanks for the great codes. I tried your code, but I can only manage to pull 3 post from the category, any reason why?

    many thanks in advance.

    I’m a code newbie

    sorry I find out, I must be blind 10 mintues ago

    thanks again for the great code, it helps me a lot!!!

    Thread Starter alingham

    (@alingham)

    Thanks for your thoughts. I’ll have a look at custom wordpress loops. If anyone wants to have a go for me or already has a solution I wouldn’t say no!
    Thanks for the praises on the template. I’ve found it really useful. I’ve used it in 4 sites I manage. I think it’s essential, bordering on a requirement. I don’t know why wordpress hasn’t made it part of the default options!
    For those wondering, if you want more than 3 posts displayed, change numberposts=3 to however many you want…

    Thread Starter alingham

    (@alingham)

    Got it people!!! Here is the final code. Feel free to use it s you see fit.
    As you can see at https://www.alingham.com/v2/day I am using mootools accordion, which is the reason for some of the formatting. Shouldn’t take much to adjust it for your own site!

    <?php
    /*
    Template Name: Page + Category II
    */
    ?>
    
    <?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 name
    			$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);
    					$page_category_name = $catName;
    				}
    			}
    		?>
    
    		<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 &raquo;</p>'); ?>
    			<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    			</div>
    		</div>
    
    		<?php endwhile; ?>
    		<?php endif; ?>
    
    		<?php //Display posts from Category of the same name as the page....
    		$wp_query = null;
    		$wp_query = new WP_Query();
    		$wp_query->query( 'cat='.$id.'&showposts=3'.'&paged='.$paged );
    		?>
    
    		<?php while ( $wp_query->have_posts() ) : $wp_query->the_post() ?>
    
    				<div id="post-<?php the_ID() ?>" class="post">
    					<h2 class="toggler"><?php the_title() ?></h2>
    					<div class="element">
    					<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    					    <div class="entry-meta">
    						<span class="cat-links"><?php printf(__('Posted in %s', 'sandbox'), get_the_category_list(', ')) ?></span>
    						<?php the_tags(__('<span class="tag-links">Tagged ', 'sandbox'), ", ", "</span>\n\t\t\t\t\t<span class=\"meta-sep\">|</span>\n") ?>
    						<span class="comments-link"><?php comments_popup_link(__('Comments (0)', 'sandbox'), __('Comments (1)', 'sandbox'), __('Comments (%)', 'sandbox')) ?></span>
    					    </div>
    					</div><!-- .element -->
    				</div><!-- .post -->
    
    		<?php endwhile ?>
    
    			</div> <!-- End of Accordion -->
    
    			<ul id="new-old-links">
    			    <li><?php next_posts_link('&laquo; Older Entries') ?></li>
    			    <li><?php previous_posts_link('Newer Entries &raquo;') ?></li>
    			</ul>
    
    <?php $wp_query = null; $wp_query = $temp; ?>
    
    </div><!-- End of class content-->
    <?php //get_sidebar(); ?>
    
    <?php get_footer(); ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Page Template with Categories’ is closed to new replies.