• Resolved nemci7v

    (@nemci7v)


    I looked for a methods to exclude a category ID from the_category() but didn’t find a code that works properly. So instead of the_category tag I’m trying to use “get the category”

    I tried

    <?php
    $category = get_the_category();
    echo $exclude = array(10);
    ?>

    but that outputs the text “array”

    not sure what to modify if is this is even possible.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Example:

    <?php
    //in the loop, display categories that are not cat id 10
    $cats = get_the_category($post->ID);
      if ($cats) {
        foreach($cats as $cat) {
          if ($cat->cat_ID != 10) {
            echo '<p> category ' . '<a href="' . get_category_link( $cat->cat_ID, 'post_tag' ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->cat_name.'</a> has ' . $cat->count . ' post(s). </p> ';
          }
        }
      }
    ?>

    To test your arrays you can use print_r($array); you can’t just echo the array variable unless specifying an index echo $array[0]; or looping through as MichaelH shows.

    Thread Starter nemci7v

    (@nemci7v)

    Thank you for the lesson! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is this code correct?’ is closed to new replies.