Viewing 6 replies - 1 through 6 (of 6 total)
  • MichaelH

    (@michaelh)

    See if you can make use of something like this.

    <?php
    // get 20 latest posts, display the categories used on those posts (most recent categories)
    $cat_array = array();
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 20,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        $cat_args=array('orderby' => 'none');
        $cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
        foreach($cats as $cat) {
          $cat_array[$cat->term_id] = $cat->term_id;
        }
      endwhile;
    }
    if ($cat_array) {
      echo '<p>List of categories used on 20 latest posts</p>';
      foreach($cat_array as $cat) {
        $category = get_term_by('ID',$cat, 'category');
        echo '<p><a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a><p> ';
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thread Starter jahsko

    (@jahsko)

    Thank you for the swift response Michael, it is now almost there. ??
    I changed the echo to output inside of
    ‘li’ tags, and now I want to limit the number of displayed categories to 5 and exclude some of the other categories

    Here was my original list categories statement:
    <?php wp_list_categories(‘number=5&exclude=3,13,14,20’); ?>

    In the code you sent where would I add this?
    ‘number=5&exclude=3,13,14,20’

    Thanks a ton, I’m just getting started with php.

    MichaelH

    (@michaelh)

    You’d need to add a counter and an if statement inside that foreach.

    afperea

    (@afperea)

    Thread Starter jahsko

    (@jahsko)

    Hi MichaelH, Could you be any more specific? Or provide a link to something similar? When I search for “add a counter” all I find are articles about adding site counters and hit counters.

    Also, I see two “foreach” sections
    foreach($cats as $cat)
    and
    foreach($cat_array as $cat)

    to which are you referring?

    Thanks!

    Thread Starter jahsko

    (@jahsko)

    Got this working, here is my example, displaying a category list based on which categories have been most recently updated and excluding four categories, News, Reviews, Specials, and Featured.

    <?php
    // get 5 latest posts, display the categories used on those posts (most recent categories)
    $cat_array = array();
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 5
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        $cat_args=array('orderby' => 'none');
        $cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
        foreach($cats as $cat) {
          $cat_array[$cat->term_id] = $cat->term_id;
        }
      endwhile;
    }
    if ($cat_array) {
      foreach($cat_array as $cat)  {	  
    
        $category = get_term_by('ID',$cat, 'category');
    	if ( $category->name != 'News' && $category->name != 'Reviews' && $category->name != 'Specials' && $category->name != 'Featured'){
        	echo '<li><a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li> ';
    	}
    
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Order Categories by most recently updated?’ is closed to new replies.