WordPress.org

Ready to get started?Download WordPress

Forums

How to show links to other posts from same Category? (9 posts)

  1. clicksharpmarketing
    Member
    Posted 5 years ago #

    We're trying to find a way to show category specific "additional content."

    So if you're reading a post from Category A, we're trying to have a little snippet of text at the end of the post which shows other recent posts from Category A.

    There are a ton of plugins out there which do similar things, and two come very close:

    1. Latest Post from Each Category plugin allows you to show, literally, the latest post from every category. You can specify categories you want to exclude, but basically it takes the one latest post from every category and displays a long list. Cool, but not what we're looking for.
    2. Category Posts plugin feeds recent posts of the current post's category - but it forces this into the sidebar like a widget, rather than showing it right in the post. AND it forces you to choose the category beforehand, rather than simply pulling from the category currently being viewed.

    It doesn't look like anything out there comes any closer. Really, the ideal would be the best of both these plugins: a specified "X" number of recent posts from the current category, shown directly in the body of the post.

    Is such a thing possible?

  2. Dunkkan
    Member
    Posted 5 years ago #

    Here there's some interesting info :

    a thread -> http://wordpress.org/support/topic/179755?replies=1
    another -> http://wordpress.org/support/topic/189282?replies=7

    In the first one, the person forgot to close the php, so you must add at the end of the code that :

    <?php endforeach; ?>
    <?php endforeach; ?>

    (yes, two times)

    The second thread, I've not tried it out (I'm going to)

  3. Dunkkan
    Member
    Posted 5 years ago #

  4. thebadone411
    Member
    Posted 5 years ago #

    I'm running into issues on this as well. Dunkkan - I've tried all three of the links you recommended. I don't like the plugin. It's just not what I need. I'm running into the same issue with the first two threads. I've got things setup on the main page (which only shows one post) and on individual posts, however, I'm only getting the most recent posts from the same category. Regardless of whether I'm looking at a post in category 1, 2, 3 or 4 I'm only getting the recent posts from category 4 to show up.

    KnightKato in the second thread says that once he put the code in his sidebar it started working but I'd like to have the list sitting below my posts. Any suggestions?

    http://www.ashtrayreviews.com/reviews2/

    Thanks in advance

  5. EJU
    Member
    Posted 5 years ago #

    this can help "outside the loop"

    <?php
    
      //Gets category and author info
      global $wp_query;
    $cats = get_the_category();
    $tempQuery = $wp_query;
      $currentId = $post->ID;
    
    // related category posts
      $catlist = "";
      forEach( $cats as $c ) {
      if( $catlist != "" ) { $catlist .= ","; }
      $catlist .= $c->cat_ID;
      }
      $newQuery = "posts_per_page=5&cat=" . $catlist;
      query_posts( $newQuery );
    $categoryPosts = "";
      $count = 0;
    if (have_posts()) {
      while (have_posts()) {
      the_post();
      if( $count<4 && $currentId!=$post->ID) {
      $count++;
      $categoryPosts .= '<li><a href="' . get_permalink() . '">' . the_title( "", "", false ) . '</a></li>';
      }
      }
      }
      $wp_query = $tempQuery;
      ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	<h2>Ultimos art&iacute;culos de la categor&iacute;a: <?php the_category(', ');?></h2>
    <ul><li>
    <?php echo $categoryPosts; ?>
    </li></ul>
    
    <?php endwhile;?>
  6. Joshuwar
    Member
    Posted 4 years ago #

    Hey Ernesto- I like your snippet here- very useful- but is there a way to make the $categoryPosts query output information other than the_title?

    I'm trying to make it include an excerpt and thumbnail, but when I add these tags to the the query it presumes they're to be passed into the php, not displayed.

    So I've had a fiddle and come up with this, which nearly works, but still has some odd behaviour... Can you see a better way (than using echoes etc) to build a more complicated output?

    //Gets category and author info
      global $wp_query;
    $cats = get_the_category();
    $tempQuery = $wp_query;
      $currentId = $post->ID;
    
    // related category posts
      $catlist = "";
      forEach( $cats as $c ) {
      if( $catlist != "" ) { $catlist .= ","; }
      $catlist .= $c->cat_ID;
      }
      $newQuery = "posts_per_page=3&cat=" . $catlist;
      query_posts( $newQuery );
    $categoryPosts = "";
      $count = 0;
    if (have_posts()) {
      while (have_posts()) {
      the_post();
      if( $count<4 && $currentId!=$post->ID) {
      $count++;
      echo '<ul>';
      $categoryPosts .= 
    
      '';
      echo '<li><h3><a href="' . get_permalink() . '" title="' . the_title( "", "", false ) . '">' . the_title( "", "", false ) . '</a></h3>';
      echo '<div class="onecol left topmarginsmall">' . wct_display_thumb() . '</div>';
      echo the_excerpt();
      echo '</li>';
      }
      echo '</ul>';
      }
      }
      $wp_query = $tempQuery;
      ?>

    Thanks!

  7. briansahagun
    Member
    Posted 4 years ago #

    Joshuwar, try:

    offset=1&posts_per_page=3&cat=

  8. shriac
    Member
    Posted 4 years ago #

    this can help "outside the loop"

    <?php
    
      //Gets category and author info
      global $wp_query;
    $cats = get_the_category();
    $tempQuery = $wp_query;
      $currentId = $post->ID;
    
    // related category posts
      $catlist = "";
      forEach( $cats as $c ) {
      if( $catlist != "" ) { $catlist .= ","; }
      $catlist .= $c->cat_ID;
      }
      $newQuery = "posts_per_page=5&cat=" . $catlist;
      query_posts( $newQuery );
    $categoryPosts = "";
      $count = 0;
    if (have_posts()) {
      while (have_posts()) {
      the_post();
      if( $count<4 && $currentId!=$post->ID) {
      $count++;
      $categoryPosts .= '<li><a href="' . get_permalink() . '">' . the_title( "", "", false ) . '</a></li>';
      }
      }
      }
      $wp_query = $tempQuery;
      ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	<h2>Ultimos art&iacute;culos de la categor&iacute;a: <?php the_category(', ');?></h2>
    <ul><li>
    <?php echo $categoryPosts; ?>
    </li></ul>
    
    <?php endwhile;?>

    Is there a way to exclude a category from this

  9. danpacey
    Member
    Posted 4 years ago #

    I've been battling with this for a while, and here's what i've come up with.

    <?php
    	global $post;
    	$categories = get_the_category();
    	$thiscat = $categories[0]->cat_ID;
    ?>
    <?php query_posts('showposts=5&orderby=rand&cat=' . $thiscat); ?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    		YOUR CONTENT HERE								
    
    	<?php endwhile; else : ?>
    		Error returning query.
    	<?php endif; ?>
    <?php wp_reset_query(); ?>

    I'm not sure if that is horrendous or pretty smart, but either way it seems to work, returning all posts that are in the same category as the post being viewed - but only the FIRST category ID, ie:

    Post is in categories A, B & C. The query shows all posts that are also in A, since A was the first cat listed.

    Hope this helps.

    Dan
    http://www.wearehench.com

Topic Closed

This topic has been closed to new replies.

About this Topic