Forums

[resolved] multiple recent post widgets (8 posts)

  1. goldmember
    Member
    Posted 2 years ago #

    i have two categories of post that i make, Previous Events and Upcoming Events.

    i put the "Recent Posts" widget in my Main Sidebar and titled it "Upcoming Events". The problem is i can only set it to list the 5 most recent posts. I can't get it to be more specific so that it only lists the 5 most recent "Upcoming Events".

    ideally, I'd like to have one widget list the most recent five "Upcoming Events" and one widget list the most recent five "Previous Events.

    is there anyway to do this? please advise. Thanks!

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Could use this in a PHP Code Widget:

    <?php
    //display 5 posts for category ID 12
    $taxonomy = 'category';
    $taxargs ='include=12';
    $terms = get_terms( $taxonomy, $taxargs );
    foreach( $terms as $term )  {
      $args=array(
        'category__in' => array($term->term_id),
        'showposts'=> 5,
        'caller_get_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<h2>Posts from '. $term->name . '</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
        endwhile;
      } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //foreach
    ?>
  3. goldmember
    Member
    Posted 2 years ago #

    thanks. but what do you mean "in"? where do i put this code?

  4. goldmember
    Member
    Posted 2 years ago #

    thanks. but what do you mean "in"? where do i put this code?

  5. MichaelH
    Volunteer
    Posted 2 years ago #

    Consider downloading and installing Otto's PHP Code Widget.

    Then put the code in one those widgets.

  6. goldmember
    Member
    Posted 2 years ago #

    thanks. so i have the widget installed and the "Previous Events" heading correctly appears in my lefthand nav bar where I want it. And i dropped the php code that you detailed above into the widget. however nothing appears. how do i tell it what i want to list?

    btw, i know little to nothing about the inner workings of php so bear with me. thanks!

  7. MichaelH
    Volunteer
    Posted 2 years ago #

    Change this to be the category of your previous or upcoming events:
    $taxargs ='include=12';

  8. goldmember
    Member
    Posted 2 years ago #

    got it. thanks!!!

Topic Closed

This topic has been closed to new replies.

About this Topic