Hi all! I'm working on a custom Page template for my website and need a little help.
This template displays the contents of the page at the top, and then allows 5 posts of a category with a slug that matches the page slug. Everything seems to be working fine. What I want to do now is add a link at the very bottom of the posts that will go to an archive of the category. If possible, I would like this to display only if there are more than the 5 posts that are on the page.
Code is below. If something is muddled, I apologize. I've just started learning how to do this and have 'frankensteined' this code together from other peoples code. Page is being tested at http://northrunbaptist.org/resources/church-library/reader-reviews
<?php
/*
Template Name: Categories As Sections Page
*/
?>
<?php get_header(); ?>
<?php include (TEMPLATEPATH . '/sidebarLeft.php'); ?>
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php $blogheader = get_post_meta($post->ID, 'blog-header', true); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h1 class="post"><?php the_title(); ?></h1>
<p> <?php edit_post_link(__( 'Edit', 'default' ), '', ''); ?></p>
<div class="entry">
<div class="article"><?php the_content((__( 'Continue Reading »', 'default')) . the_title('',
'', false)); ?></div>
<?php wp_link_pages(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php // This displays posts with the same category as the title of the page
if ( is_page() ) {
$page_name = $posts[0]->post_name; //or use $posts[0]->post_title
$category = get_term_by('slug',$page_name,'category');
if ($category) {
$args=array(
'category__in' => array($category->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'showposts' => 5,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2>'.$blogheader.'</h2>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<table class="subhead"><tr><td class="subheadleft">
<h3 class="post"><?php the_title(); ?></h3>
</td><td class="subheadright">
<p><?php the_date( 'F d , Y', $before, $after, true ); ?></p>
</td></tr></table>
<p> <?php edit_post_link(__( 'Edit', 'default' ), '', ''); ?></p>
<div class="entry">
<div class="article"><?php the_content((__( 'Continue Reading »', 'default')) . the_title('',
'', false)); ?></div>
<?php wp_link_pages(); ?>
</div>
</div>
<?php endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</div>
<?php include (TEMPLATEPATH . '/sidebarRight.php'); ?>
<?php get_footer(); ?>
</div><!--/Wrapper-->
<div id="footershadow"></div>
</div><!--/Container-->
</body>
</html>
Thanks!
~Josh