WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Categories List for Custom Post Type/ No Children/ Ordered by Post number (3 posts)

  1. cmdshiftdesign
    Member
    Posted 2 years ago #

    Setting up a listing of Categories w/ their descriptions. All of the categories are within a custom post type named "works"

    Have something that is ALMOST giving me what I want, and so far have not been able to edit in correctly to get what I need.

    THIS IS WHAT I WANT:

    • A list of each category within the custom post type "works" along with it's description.
    • ONLY the top level Categories, NO child categories.
    • The listed categories ordered by which has the greatest number of posts within it FIRST.

    THIS IS WHAT I HAVE:

    • A list in reverse ascending alphabetical order of ALL of the categories within the custom post type parent, and children with descriptions.

    the code i have now...

    <?php $args=array('orderby' => 'name','order' => 'ASC');$categories=get_categories('taxonomy=works');foreach($categories as $category) {echo '<h1><a href="' . get_category_link( $category) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h1>';echo '<p>'. $category->description . '</p>'; }?>

  2. tresinvolved
    Member
    Posted 2 years ago #

    Untested, but hopefully this works:

    <?php         
    
    $args = array('orderby' => 'count', 'order' => 'ASC', 'taxonomy' => 'works');
    
    $categories=get_categories($args);
    
    foreach($categories as $category) {
      if ( 0 == $category->category_parent ) { // if parent category
          echo '<h1><a href="' . get_category_link( $category) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h1>';
          echo '<p>'. $category->description . '</p>';
      } // end if parent category
    } // end foreach
    
    ?>
  3. cmdshiftdesign
    Member
    Posted 2 years ago #

    Thank you!

    This was near spot on! Just changed the ASC to DESC and this just as i was looking for, you rock! :)

Topic Closed

This topic has been closed to new replies.

About this Topic