WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent posts (Show category name & Exclude a category) (6 posts)

  1. atdheh
    Member
    Posted 4 years ago #

    Hello, I'm showing recent posts with this:

    <ul><li><h2>Recent posts</h2><ul>
    <?php wp_get_archives('title_li=&type=postbypost&limit=15'); ?>

    This code to me only display my 15 recent post titles (linked):
    Postname1
    Postname2
    ...
    Postname15

    1.
    Now I want to display category name too, example:
    category - postname1
    category - postname2
    ...
    category - postname3

    2.
    How to exclude posts from a category, example to exclude posts from category 88.

    Thank you.

  2. MichaelH
    Member
    Posted 4 years ago #

    This might be easier:

    <?php
        $args=array(
          'showposts'=>15,
          'category__not_in' => array(88),
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '15 recent Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p> <?php the_category(', '); ?> -
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. atdheh
    Member
    Posted 4 years ago #

    MichaelH thank you, its working perfectly.

    But I have one question more:
    Its anyway to add this in sidebar?

  4. MichaelH
    Member
    Posted 4 years ago #

    That could go in the sidebar template (probably sidebar.php) or even in a PHP Code Widget

  5. trefethen
    Member
    Posted 4 years ago #

    Is there an easy way for a non PHP guy like me to exclude post from certain categories while showing the recent posts of another category in the sidebar?

    I appreciate all the help you can provide.

    ~ John

  6. muymalestado
    Member
    Posted 3 years ago #

    Is this what atdheh is asking ?

    trefethen may also be asking the same question I have - in the Recent Posts sidebar widget is it possible to exclude posts of a specified category?

Topic Closed

This topic has been closed to new replies.

About this Topic