Support » Fixing WordPress » Having certain pages link to latest posts from specific categories

  • Hello,

    I have a few pages based on contributors and each page gives a brief overview of the author.

    At the bottom of each of their pages I would like to link to their 5 most recent posts based on their category. (One user makes all the posts, but each contributor has a category dedicated to them).

    Is this possible? I did some research and could not find anyone asking for something wanting to link to different categories based on different pages.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi

    So basically you want to list the 5 most recent posts of the current posts category (the one you are viewing) underneath the content of the current post?

    If so you can put this code under the get_content loop of your themes single.php file. I have not tested this code, but it should work. Let me know if it doesn’t.

    <ul>
    <?php
    
    $category = get_the_category();
    $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => ($category[0]->cat_ID) );
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    	<li>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	</li>
    <?php endforeach;
    wp_reset_postdata();?>
    
    </ul>

    Cheers,
    Alex

    Thread Starter etrnldarkness

    (@etrnldarkness)

    Very close! I’ll give a couple clearer examples. Maybe this is not possible or maybe I need to try it another way.

    We have “Home” a page which is the static front page which says welcome yada yada.

    Then we have four more PAGES. Each page explains who a contributor is with a profile pic etc. Within each page we would like to (near the end) have like an excerpt of the most recent (5) posts published under this user’s category. So let’s say the Page is about Person X. A page cannot be added to a category (to my knowledge). So on that page we would need to link automatically to the 5 most recent posts placed in Category X

    Same with thing Person Y linking to Category Y on their page.

    Is this something we could possibly do in wordpress at the moment?

    This is possible, you can either hard code the category in the code above and save it as a custom page template (http://codex.wordpress.org/Page_Templates). Or you could just create one custom page template and make the value of the $category value the slug of the current page, making sure that the slug of the page matched the appropriate category for that person.

    Thread Starter etrnldarkness

    (@etrnldarkness)

    OK, I tried to give this a run myself but the template they currently are using – seems to go blank no matter what I do and I’ve never really had this much trouble coding on a site before.

    This is the original code for the template page.php

    <?php get_header(); ?>
    
    			<?php get_sidebar('top'); ?>
    
    			<?php
    
    			if (have_posts()) {
    
    				/* Start the Loop */
    
    				while (have_posts()) {
    
    					the_post();
    
    					get_template_part('content', 'page');
    
    				}
    
    			} else {
    
    				theme_404_content();
    
    			}
    
    			?>
    
    			<?php get_sidebar('bottom'); ?>
    
    <?php get_footer(); ?>

    I inserted the coding provided to me previously in this thread (and changed cat_ID to cat_7 since it is category 7. Did I do that correctly?

    Regardless it still made the page turn up blank when I chose this template.

    Is there anyway to simplify this for me? IE: Someone look at the coding I just provided, insert the coding I need and feed it back to me? This way if it still fails – I know it’s not just me going insane, haha.

    Another quick question:

    Since I want to have some text such as a short bio before the 5 excerpts would display, how would this work with the custom template? Wouldn’t the template be inserting these automatically onto the page?

    Sorry for such newbie questions wordpress and I are usually just on a post and enjoy sort of relationship.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Having certain pages link to latest posts from specific categories’ is closed to new replies.