Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Checking of cat number WordPress

  • SOLVED

I have this bit of code to ouput the categories of the post, however it hides category '35' from the list.

How can I say to hide category 35 and 65?


foreach( $categories as $category ) {
if( $category->term_id == 35 ) continue;
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );


Thank you

Answers (2)

2019-10-01

Mohamed Ahmed answers:

Hello Ross,

Could you use this


if( $category->term_id == 35 || $category->term_id == 65) continue;


Instead of


if( $category->term_id == 35 )

2019-10-01

Hugo Gonçalves answers:

Hi Ross!

Try this to exclude cat 35.

Instead of:

foreach( $categories as $category ) {
if( $category->term_id == 35 ) continue;
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );


Use this:

foreach( $categories as $category ) {
if( $category->term_id !== 35 ) {
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
}
echo trim( $output, $separator );


Ross Gosling comments:

Thanks for the comment, my code works fine to exclude cat 35.

My question was to exclude cat 35 and cat 65.