WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display 4 more posts from same category (11 posts)

  1. souverian
    Member
    Posted 5 years ago #

    My blog features just 1 post on the home page. However, i'd like it so that i could automatically show 4 more posts from the same category as the latest post.

    For example if the latest post is in the "Football" category, 4 more "Football" posts are displayed alongside the most recent on the homepage.

    Is this possible?

  2. MichaelH
    Member
    Posted 5 years ago #

    From my notes -- display posts belonging to the categories of the current post without duplicating the present post

    <?php
    global $post;
    $cat_ID=array();
    $categories = get_the_category(); //get all categories for this post
      foreach($categories as $category) {
        array_push($cat_ID,$category->cat_ID);
      }
      $args = array(
      'orderby' => 'date',
      'order' => 'DESC',
    	'post_type' => 'post',
    	'numberposts' => 8,
    	'post__not_in' => array($post->ID),
    	'category__in' => $cat_ID
      ); // post__not_in will exclude the post we are displaying
        $cat_posts = get_posts($args);
        $out='';
          foreach($cat_posts as $cat_post) {
              $out .= '<li>';
              $out .=  '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></li>';
          }
        $out = '<ul class="cat_post">' . $out . '</ul>';
        echo $out;
    ?>
  3. souverian
    Member
    Posted 5 years ago #

    Hi Michael, one slight issue is for each echoed post I'd like to use a custom field to pull up an image, is this possible?

    Is there a foreach I can do for each post?

  4. MichaelH
    Member
    Posted 5 years ago #

  5. souverian
    Member
    Posted 5 years ago #

    Hi Michael, basically, for each category post displayed (using your code above) I'd like this to show up:

    <a href="<?php the_permalink() ?>"><img class="photo" src="<?php bloginfo('url'); ?>/core/photos/<?php echo get_post_meta($post->ID, "image", true); ?>.jpg" border="0" alt="" /></a>

    I tried inputting this into your code but it failed. Any ideas?

    Thanks so much for your help so far.

  6. MichaelH
    Member
    Posted 5 years ago #

    Untested but...

    <?php
    global $post;
    $cat_ID=array();
    $categories = get_the_category(); //get all categories for this post
      foreach($categories as $category) {
        array_push($cat_ID,$category->cat_ID);
      }
      $args = array(
      'orderby' => 'date',
      'order' => 'DESC',
    	'post_type' => 'post',
    	'numberposts' => 8,
    	'post__not_in' => array($post->ID),
    	'category__in' => $cat_ID
      ); // post__not_in will exclude the post we are displaying
        $cat_posts = get_posts($args);
    if ($cat_posts) {
      foreach ($cat_posts as $cat_post) {
        ?>
        <a href="<?php echo get_permalink($cat_post->ID); ?>"><img class="photo" src="<?php bloginfo('url'); ?>/core/photos/<?php echo get_post_meta($cat_post->ID, "image", true); ?>.jpg" border="0" alt="" /></a>
        <?php
      }
    }
    ?>
  7. souverian
    Member
    Posted 5 years ago #

    Thank you Michael, that worked sensationally. Thank you, thank you, thank you so much!!

  8. andyasher55
    Member
    Posted 4 years ago #

    Michael,

    I'm trying hard to modify your fabulous patch...A similar situation to souverian.

    I'm trying to show more posts from the selected category, but am using a custom field for the thumbail.

    http://nicolasheller.com/test/?p=1

    Here is the thumbnail code:

    <!-- Begin Thumbnail -->
    				    <?php $postimageurl = get_post_meta($post->ID, 'post-img', true);
    				    if ($postimageurl) {
    				    ?>
    					  <a href="<?php the_permalink($cat_post->ID); ?>" rel="bookmark"><img src="<?php echo $postimageurl; ?>" alt="thumbnail" /></a>
    				    <?php } else { ?>
    					  <a href="<?php the_permalink($cat_post->ID); ?>" rel="bookmark"><img src="<?php bloginfo('template_url'); ?>/images/_.jpg" alt="thumbnail" /></a>
    				    <?php } ?>
    				    <!-- End Thumbnail -->

    How would I adapt this to what you provided?:

    <?php
    			global $post;
    			$cat_ID=array();
    			$categories = get_the_category(); //get all categories for this post
    			  foreach($categories as $category) {
    			    array_push($cat_ID,$category->cat_ID);
    			  }
    			  $args = array(
    			  'orderby' => 'date',
    			  'order' => 'DESC',
    				'post_type' => 'post',
    				'numberposts' => 8,
    				'post__not_in' => array($post->ID),
    				'category__in' => $cat_ID
    			  ); // post__not_in will exclude the post we are displaying
    			    $cat_posts = get_posts($args);
    			    $out='';
    			      foreach($cat_posts as $cat_post) {
    			          $out .= '<h1>';
    			          $out .=  '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></h1>';
    			      }
    			    $out = '<ul class="cat_post">' . $out . '</ul>';
    			    echo $out;
    			?>

    Thanks so much!

  9. andyasher55
    Member
    Posted 4 years ago #

    Doh! Nevermind, I stumbled on a solution.

  10. dallasbpeters
    Member
    Posted 4 years ago #

    I'm trying to show one post at the top of the page. Then ALL other posts in the category with title and content (read more) included. Possible an image from a custom field. I've tried to figure out how this:

    $cat_posts = get_posts($args);
    					    $out='';
    					      foreach($cat_posts as $cat_post) {
    					          $out .= '<h3>';
    					          $out .=  '<a href="'.get_permalink($cat_post->ID).'">'.wptexturize($cat_post->post_title).'</a></h3>';
    					      }
    					    $out = '<div class="cat_post">' . $out . '</div>';
    					    echo $out;<code></code>

    is printing. I can't seem to figure out how to add content under the title. Help?

  11. justatest47
    Member
    Posted 4 years ago #

    Hi. I just found this code for making thumbnails out of URLs.

    <?php $postimageurl = get_post_meta($post->ID, 'post-img', true);
    	if ($postimageurl) {
    	?>
    	      <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php echo $postimageurl; ?>" alt="Post Pic" width="200" height="200" /></a>
    	<?php } else { ?>
    	      <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php bloginfo('template_url'); ?>/images/wprecipes.gif" alt="Screenshot" width="200" height="200" /></a>
    	<?php } ?>

    I want to replace this code with my existing one because my existing one only shows thumbnails from uploaded images.
    But the thing is that the above code doesn't show the thumbnails from the uploaded pictures on my host.
    Can you experts modify the above code so that it works in both ways? Showing thumbnails from custom field "post-img" and from uploaded pictures on my host.

    THANK YOU in advance!!!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags