1

How can I display only the specific category when accessing the posts related to that category? For example, I want to show only CSR Events under Categories When accessing posts related to CSR events. Here's the link to CSR post https://www.mi-eq.com/blood-donation-compaign/

Screenshot: https://i.stack.imgur.com/YoTfk.jpg

Similarly, when visiting posts related to other categories, only the specific category will be shown.

0

I have copied and pasted your code to function.php but it turned out to be no categories shown. Please see screenshot here - https://ibb.co/gZt2zQF . Anything wrong with the code? Can anyone else help?

0

Here's some code that will modify the arguments for the WP Categories widget so that the only category displayed will be the current category. Add this to your theme's/child theme's functions.php or create a plugin for this code:

/**
 * Modify the arguments for the Categories widget on single templates so that
 * only the current category is returned.
 *
 * @param array $cat_args Arguments passed to wp_list_categories(),
 * @param array $instance Category widget instamce.
 *
 * @return array wp_list_categories() arguments.
 */
function wpse_widget_categories_args( $cat_args, $instance ) {
    // Bail if this isn't the single template.
    if ( ! is_single() ) {
        return $cat_args;
    }

    global $wp_query;

    // Get term id for current category.
    $current_term = get_term_by(
        'slug',
        $wp_query->query['category_name'],
        'category'
    );

    // Get term objects for all terms except the current term.
    $exclude_categories = get_categories(
        [
            'exclude' => [ $current_term->term_id ],
        ]
    );

    // Use our list of excluded terms to ensure results are only returned for the current term.
    $cat_args['exclude'] = wp_list_pluck( $exclude_categories, 'term_id' );

    return $cat_args;
}
add_filter( 'widget_categories_args', 'wpse_widget_categories_args', 10, 2 );

Example with this code enabled: enter image description here

Example without code enabled (cropped because there are lots of categories): enter image description here

4
  • Hi Dave, I have copied and pasted your code to function.php but it turned out to be no categories shown. Please see screenshot here - ibb.co/gZt2zQF . Anything wrong with the code? Can anyone else help?
    – David Lee
    Jun 30 '20 at 9:24
  • Sorry that didn't work for you @DavidLee. I did double check it and the code works for me.I added a screenshot of what I'm seeing -- When I'm on a single post, the WP Categories widget will just show the main category of the current post. Jun 30 '20 at 22:08
  • Could it be there are no categories to display, so get_categories() will return an empty array? I have tested with following code and categories name can be returned. Is there any other changes need to make other than functions.php?
    – David Lee
    Jul 1 '20 at 1:58
  • These two are my categories: mi-eq.com/?cat=73 mi-eq.com/?cat=74 which respectively redirected to mi-eq.com/category/main-events mi-eq.com/category/corporate-social-responsibilies
    – David Lee
    Jul 1 '20 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.