Forums

Creating a list of Categories with recent posts (11 posts)

  1. weirdale
    Member
    Posted 1 year ago #

    The question was asked in this thread:

    http://wordpress.org/support/topic/list-categories-with-recent-postings?replies=3

    ...three years ago and it was never resolved. I would post in that thread but it has been closed.

    So, is there any way to create a list of categories that have the most recent posts? Not a list of the posts themselves, just the categories...

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Maybe you can use the code shown here.

  3. weirdale
    Member
    Posted 1 year ago #

    No, it appears that is for comments. I want to create a list of categories.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    OOPS - sorry about that.

    Latest Post from each Category plugin might do what you want. Don't let the name fool you, you can select how many posts to show for each category.

    If you don't want a plugin, the code below might be a starting point:

    <?php
    $num_cats_to_show = 10;
    $count = 0;
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          $count ++;
          if ($count <= $num_cats_to_show) {
            // Put a header for the term here, if desired.
            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;
          }
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  5. MichaelH
    Volunteer
    Posted 1 year ago #

  6. weirdale
    Member
    Posted 1 year ago #

    Hello, I apologize for taking so long to get back to this. vtxyzzy, that Latest Post from Each Category widget you linked me to was very close. A minor problem is that it interprets a post count of zero to mean "all of them". But that's not a dealbreaker, actually.

    The major problem is that it won't work as a widget, so I can't put it in a sidebar, only a seperate page or post. Is there a way to get it to work in a sidebar? Some kind of plugin that will enable the code to work in a widget, maybe?

  7. vtxyzzy
    Member
    Posted 1 year ago #

    You can 'wrap' the dd_last_from_each() function in a shortcode function and use a shortcode in a text widget. Add this code to functions.php:

    <?php
    
    //  Add a shortcode dd-last-from-each for a text widget to show the
    //  latest post for each category using the dd-latest-in-category plugin.
    function dd_last_from_each_function() {
       if (function_exists('dd_last_from_each')) {
          return dd_last_from_each();
       } else {
          return '';
       }
    }
    add_shortcode('dd-last-from-each','dd_last_from_each_function');
    add_filter('widget_text','do_shortcode');
    
    ?>

    Then, put a Text widget in your sideber with the shortcode:

    [dd-last-from-each]

  8. weirdale
    Member
    Posted 1 year ago #

    I tried adding it to functions.php and got this error:

    Fatal error: Call to undefined function add_shortcode() in /home/mackmntn/public_html/wp-includes/functions.php on line 37

    Accord to the codex, "add_shortcode() is located in wp-includes/shortcodes.php."

    And I do have that file right where it's supposed to be, and I already have at least one plugin that uses a shortcode, so I know it works.

    So I don't understand what went wrong here...

  9. vtxyzzy
    Member
    Posted 1 year ago #

    It doesn't go in wp-includes/functions.php, it goes in your theme's functions.php

  10. weirdale
    Member
    Posted 1 year ago #

    Aaah, thank you vtxyzzy, that worked beautifully. Now the only thing making it imperfect is that you can't set the number of posts to display to zero. I know just enough php to get myself in trouble, I might be able to do this myself, but let me ask the wizards here, is there a quick and dirty way to edit the php to accomplish this?

    I'll post the code for the plugin if that's within the board rules...

  11. vtxyzzy
    Member
    Posted 1 year ago #

    No need to post the code - it is available at the plugin page.

    I took a look, but I don't see a 'quick and dirty' way of doing what you want.

    The problem is that you need a minimum of 1 post per category in order to get the query to work. Then, you would need to skip that post in the display. That means changes in more than one place in the code - too much to work out without testing.

Topic Closed

This topic has been closed to new replies.

About this Topic