How to list all categories and tags in a page?

Suppose we have few categories, tags and custom post type and taxonomies. How can we list all categories, tags in a page? Something like;

Page

Category Thumbnail and Category Name 1(link to category page)
Description

Category Thumbnail and Category Name 2(link to category page)
Description

Category Thumbnail and Category Name 3(link to category page)
Description

Divider

Tag Thumbnail and Tag Name 1(link to Tag page) Description

Tag Thumbnail and Tag Name 2(link to Tag page) Description

Tag Thumbnail and Tag Name 3(link to Tag page) Description

Same with custom post type and taxonomies.

And if the description and thumbnail are not possible then only a name with link will be adequate.

Most of this is achievable by these plugins.

Right now I’m looking in plugin code to achieve what I want.

1 Answer
1

TERM METADATA

It’s possible to get thumbnails for terms using the new Term Metadata in 4.4. You just also need to define those ahead of time yourself.

  • add_term_meta( int $term_id, string $meta_key, mixed $meta_value, bool $unique = false )
  • get_term_meta( int $term_id, string $key = '', bool $single = false )
  • update_term_meta( int $term_id, string $meta_key, mixed $meta_value, mixed $prev_value="" )

CATEGORIES

get_categories ( ) Returns an array of category objects matching the query parameters.

echo "Categories:<br/>";

$args = array( 
    'orderby'                  => 'name',
    'order'                    => 'ASC', 
    'public'                   => true,
); 

$categories = get_categories( $args );

foreach ( $categories as $category ) {
     echo '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a><br/>';
}

echo "<hr>"; // divider

TAGS

get_tags ( ) Retrieves an array of objects for each term in post_tag taxonomy.

echo "Tags:<br/>";

$args = array( 
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'public'                   => true,
); 

$tags = get_tags( $args );

foreach ( $tags as $tag ) {
     echo '<a href="' . get_tag_link( $tag ) . '">' . $tag->name . '</a><br/>';
}

TAXONOMIES

get_taxonomies ( ) Get a list of registered taxonomy objects.

echo "Taxonomies:<br/>";

$args = array(  
    '_builtin'                 => false, 
    'public'                   => true,
); 

$taxonomies = get_taxonomies( $args, 'objects' ); 

foreach ( $taxonomies as $taxonomy ) {
    echo '<p>' . $taxonomy->labels->name . '</p>';
}

POST TYPES

get_post_types ( ) Returns the registered post types as found in $wp_post_types. get_posts ( ) creates an array of posts based on a set of parameters so use it with your post_type_list.

echo "Post Types:<br/>";

$args = array(
    'public'   => true,
    '_builtin' => false,
);
$output="names"; // names or objects, note names is the default
$operator="and"; // 'and' or 'or'
$post_types = get_post_types($args, $output, $operator);

// get all the posts for all post_types

$posts = get_posts(array(
                           'post_type'   => $post_types,
                           'numberposts' => - 1,
                           'orderby'     => 'post_type',
                           'order'       => 'ASC',
                           'post_status' => array( 'publish' ),
                           'public'      => true,
                       ));

foreach($posts as $post) {

    $link = get_permalink($post);
    $title = $post->post_title;

    if($post_type !== $post->post_type) {
        $post_type = $post->post_type;
        echo '</br/><p>' . $post_type . '</p></br/>';
    }

    // show link to post

    echo "<p><a href=\"$link\">$title</a></p>";
}

TRANSIENT CACHING

If you want to cache the results of all these queries then consider the Transients API. This will store the result into the database for a limited time (which you ~request~). It’s not a certain timeframe because plugins like WP Optimize can clear the cache anytime.

// check if the value exists using the key -- if it fails then we'll generate the content

if(false === ($trans_value = get_transient($trans_key = 'my_transient_key'))) {

    print_r('NO CACHE FOR ' . $trans_key); // (debug output)

    // start the output buffer to capture our generated content

    ob_start();

    // run your expensive operations -- data sent to the screen will be captured

    print_r('This is an expensive operation!');

    // using ob_get_clean we'll store the results and set the value variable.

    // double check the amount of time you want to keep the data cached
    // set to 0 for auto loading on every page hit.

    set_transient($trans_key, $trans_value = ob_get_clean(), 1 * MINUTE_IN_SECONDS);
}
else {
    print_r('THIS IS A CACHED RESULT FOR ' . $trans_key); // (debug output)
}

// value is ready and cached
print_r("<pre>$trans_value</pre>");

To clear the value just delete using the key.

delete_transient ( $trans_key );

Leave a Comment