Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rajucs/962e792db18d06ab741a67e822fa99fb to your computer and use it in GitHub Desktop.

Select an option

Save rajucs/962e792db18d06ab741a67e822fa99fb to your computer and use it in GitHub Desktop.
Method -1
$args = array(
'hide_empty' => 0, //Show me all the categories, even the empty ones
'orderby' => 'count', //which accepts a string. You can pick from the following options: ID to order the categories by their ID (no, really?), name to sort them alphabetically (the default value), slug to sort them in the alphabetical order of their slugs, and count to order by the number of posts they contain.
'order' => 'DESC', //The chosen order can be reversed by setting DESC (descending) as a value for the order option (by default this option is set to ASC (ascending)).
'include' => '15,16,9'
);
wp_list_categories($args);
Method - 2
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'include' => '15,16,9'
) );
foreach( $categories as $category ) {
$category_link = sprintf(
'<a href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
}
Credit- https://stackoverflow.com/a/45729730/7162602
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment