• Resolved nbatioco

    (@nbatioco)


    I already have this code working that display 6 recent new post in my website.

    <?php
      $thumbnails = get_posts('numberposts=6');
      foreach ($thumbnails as $thumbnail) {
        if ( has_post_thumbnail($thumbnail->ID)) {
          echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
          echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
          echo '</a>';
        }
      }
    ?>

    But I have one problem, like if I have a new post in my portfolio it well display also as my recent post. Can someone help me with the proper code that it will only show the recent post from a certain category or a parent category?

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can add the category id as a parameter:

    $thumbnails = get_posts('numberposts=6&cat=34');

    Replace 34 with the id of your category.

    See the other parameters available here:

    http://codex.ww.wp.xz.cn/Template_Tags/get_posts

    Thread Starter nbatioco

    (@nbatioco)

    Thanks for the help vtxyzzy! But it gives me 12 more images lol.. But still thanks, I have look on the parameters and made them work.

    Here’s my new code:

    <?php
      $thumbnails = get_posts(array('numberposts' => 6, 'category__not_in' => array(15)));
      foreach ($thumbnails as $thumbnail) {
        if ( has_post_thumbnail($thumbnail->ID)) {
          echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
          echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
          echo '</a>';
        }
      }
    ?>

    Check how it looks like here WordPress Best Themes

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Recent Post thumbnail’ is closed to new replies.