Forums

Link to more posts(category archive) if more than # posts (2 posts)

  1. Noromami
    Member
    Posted 2 years ago #

    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 &raquo;', '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 &raquo;', '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

  2. Noromami
    Member
    Posted 2 years ago #

    Update!

    I've made a little more progress on this problem. I have a link working now that takes the category of the posts and places it in a link at the bottom of the page. (Thanks to another bit of code I found on the forums) All I'd like to do now is to have the link only show up when there are more posts than the page is set to display (in the current form, more than 5 posts).

    I assume I need some sort of conditional statement that compares 'showposts' to the number of posts in he category. Then if the posts in the category is greater than showposts, it will output the div. Otherwise it will leave the space blank. Anyone good with their PHP and can help me with this, It would be greatly appreciated!

    Here's the current code. The div with the class 'catlink' contains the archive link:

    <?php
    /*
    Template Name: Categories As Blog 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 &raquo;', '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 &raquo;', 'default')) . the_title('', 
    
    '', false)); ?></div>
         <?php wp_link_pages(); ?>
    	</div>
        </div>
    
    <?php $cat = get_the_category(); $cat = $cat[0]; ?>
    
    <?php endwhile;
    }
    }
    }
    wp_reset_query(); // Restore global post data stomped by the_post().
    ?>
    
    <div class="catlink">
    <p class="catlink">Read more from the <a class="catlink" href="<?php echo get_category_link($cat->cat_ID);?>"><?php echo $cat->cat_name; ?></a> Archives</p>
    </div>
    
    </div>
    
    <?php include (TEMPLATEPATH . '/sidebarRight.php'); ?>
    <?php get_footer(); ?>
    </div><!--/Wrapper-->
    <div id="footershadow"></div>
    </div><!--/Container-->
    </body>
    </html>

Topic Closed

This topic has been closed to new replies.

About this Topic