0

This is My Category List :

1- Years 2- Genere

And I use This Code For show in posts :

<?php the_category(' , ') ?>

How Can Hide "Year or Genere" in up code?

Please Help Me.i want Show Only One Category.

1
  • You can use get_the_categories filter and unset certain categories that you want. Just remember to remove the filter after the_category.
    – Sumit
    Mar 12, 2016 at 16:24

2 Answers 2

0

First add get_the_categories filter before the_category(' , ') then remove it. So it will not effect categories on other places.

add_filter( 'get_the_categories', 'remove_selected_categories' );
the_category(' , ');
remove_filter( 'get_the_categories', 'remove_selected_categories' );

In callback function check for categories to remove then remove them!

function remove_selected_categories( $categories ) {

    $categories_to_remove = array(
        'years',
        'genere'
    ); //Place the slug for categories

    foreach ($categories as $index => $single_cat) {

        if (in_array($single_cat->slug, $categories_to_remove)) {
            unset($categories[$index]);
        }
    }

    return $categories;
}

Check the documentation for get_the_categories filter.

0
0

Try this code..

$categories = get_the_category();
if ( ! empty( $categories ) ):
$separator = ' ';
$skipped_cat = 'Genere'; 
    foreach( $categories as $category ):
        if( $category->name != $skipped_cat ):
        echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
        endif;
    endforeach;
endif;
3
  • I could not do. Where should I copy this code?
    – Pedroxam
    Mar 12, 2016 at 17:20
  • It depends on where do you need to run this code, is it on a single post? or a loop where it shows multiple ppsts?, ..etc. Please provide more details to answer this question. Mar 12, 2016 at 17:42
  • in single.php after the <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
    – Pedroxam
    Mar 12, 2016 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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