0

I am trying to alter a plugin that has this code to display Related Categories names under the post:

<?php if ( count( get_the_category() ) ) : ?>
        <span class="cat-links">
            <?php printf( __( '<span class="%1$s">See related Videos</span> %2$s', 'posts-in-page' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
        </span>
    <?php endif; ?>

Can someone please tell me how I can alter this code to exclude one Category name from the list that it generates...?? The category name that I want to exclude is "TopONLY" and its ID is 11.

1
$d = get_the_category();
$glu = [];
foreach($d as $rst ):
    //exclude category name
    if($rst->name != 'Sticky'):
    $glu[]="<a href=".get_category_link( $rst->cat_ID ).">{$rst->name}</a>";
    endif;
endforeach;

//error_log(print_r($glu, true).'/n', 3, WP_CONTENT_DIR.'/debug.log');
echo "These are the categories". implode(', ', $glu);

I'm a beginner. Hope this will help!

  • Appreciate the response. Please reformat your response within code blocks so I can better understand it. Also, please include your code within the code that I had originally posted. – user3512522 Aug 11 '17 at 6:18
0

use wp_list_categories function instead and use 'exclude' argument.

use this code

<?php 
    $categories = get_the_category();
    $count=count($categories)
    if($count){
    foreach ( $categories as $category ) { 
    if ( $category->term_id == 11 ) { 
         continue;
    }?>
    <span class="cat-links">
        <?php printf( __( '<span class="%1$s">See related Videos</span> %2$s', 'posts-in-page' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    </span>
<?php endif; 
}
?>

Hope this will help you.

  • Appreciate the response but it is saying Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\wp-content\themes\child\posts_loop_template_toponly.php So I added a semi-colon after the third line but now it is saying Parse error: syntax error, unexpected 'endif' (T_ENDIF) in C:\xampp\htdocs\wp-content\themes\child\posts_loop_template_toponly.php on line 45 – user3512522 Aug 11 '17 at 5:58
  • <?php $categories = get_the_category(); foreach ( $categories as $category ) { if ( $category->term_id == 11 ) { continue; }?> <span class="cat-links"> <?php printf( __( '<span class="%1$s">See related Videos</span> %2$s', 'posts-in-page' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?> </span> <?php endif; } Use this one – user106916 Aug 11 '17 at 6:08
  • Sorry, still getting the same error Parse error: syntax error, unexpected 'endif' (T_ENDIF) in C:\xampp\htdocs\wp-content\themes\child\posts_loop_template_toponly.php on line 34 This code that you have sent me has to go into a template file that is displaying various content on the page. I cannot change the other code in this template file and I also cannot change any shortcode. Any ideas...?? – user3512522 Aug 11 '17 at 6:16
  • okay remove endif from bottom of code – user106916 Aug 11 '17 at 6:21

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.