• Resolved sdonikian

    (@sdonikian)


    Hello, I would like to display posts belonging to the categories of the current post without duplicating the present post. I found the following code on this forum and it works great. Now I’d like to add the excerpt and the date. What is the easiest way of going about this?

    <?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;
    ?>

    Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter sdonikian

    (@sdonikian)

    Hi, can you give me a sample of what the code required to generate the list would look like please?

    I know of the tags, but I’m not enough of a programmer to specify conditions, ie: list posts of same category ONLY.

    Thanks!

    Okay you got me confused–so please start over. What is it you are looking for?

    Thread Starter sdonikian

    (@sdonikian)

    Ok, I wish to display a list of the posts from a single category UNDER a single post (in the single.php template).

    Say I view a single post, I want to see a list of more posts from the SAME category listed below. This ought to be simple. I’ve actually managed to do it with the above code, but it only displays the title of the posts. I want an excerpt and a date too.

    Actually this might be a better example since that example puts all the data in the $out variable before displaying it:

    <?php
    if ( is_single() ) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
            'post__not_in' => array($post->ID),
            'showposts'=>5,
            'caller_get_posts'=>1
          );
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo '<h2>More Posts from '. $category->category_description . '</h2>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
             <?php
              the_time('F jS, Y');
              the_excerpt();
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>

    Thread Starter sdonikian

    (@sdonikian)

    Yes, that seems to work.

    Although I must ask, what does this line of code do?
    echo "<pre>"; print_r($category); echo "</pre>";

    I removed it because it was generating some random information like:

    stdClass Object
    (
    [term_id] => 4
    [name] => Habitation
    [slug] => habitation
    [term_group] => 0
    [term_taxonomy_id] => 4
    [taxonomy] => category
    [description] =>
    [parent] => 0
    [count] => 7
    [object_id] => 68
    [cat_ID] => 4
    [category_count] => 7
    [category_description] =>
    [cat_name] => Habitation
    [category_nicename] => habitation
    [category_parent] => 0
    )

    Is it ok for me to remove it?

    Thread Starter sdonikian

    (@sdonikian)

    Well I guess it is since it works.
    I was able to achieve my goal.

    Thanks a lot!

    Oh sorry, that’s just to display all the elements left over when testing. Thanks. I’ll edit that code to make it ‘non-executing’.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display posts belonging to the categories of current post’ is closed to new replies.