• Resolved jumust

    (@jumust)


    Hi,
    I want to show the category link of post only for some categories.

    I tried this, it’s the bottom of each post

    <?php is_category( array( '3,7,18,28' ) ); ?>
    <?php $category = get_the_category();
    		if($category[0]){
    		echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    		} ?>	<?php ;?>

    but it shows the link for all categories and not only for the IDs above…Why?

Viewing 9 replies - 1 through 9 (of 9 total)
  • I think u need to use an if statement over there ,
    is_category() is returning true or false , but it is not used .

    I used this to exclude a single category “test category one”from appearing below the post:

    <?php
    // For each of the categories, create them as category variables
    foreach((get_the_category()) as $category) {
        // If the category is NOT the cat_name (category name) then echo some stuff
        // So grab all... except where they match cat_name (exclude basically)
        if ($category->cat_name != 'Test Category One') {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>

    but you’d have to mod it to exclude more than one. I haven’t been successful yet.

    Thread Starter jumust

    (@jumust)

    Thanks a lot.
    I just tried this and broke my page

    <?php if (is_category( array( '3,7,18,28' ) )); ?>
    <?php $category = get_the_category();
    		if($category[0]){
    		echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    		}
    	?><?php endif ;?>

    also I tried this but again the category link is displayed for all categories

    <?php if (is_category( array( '3,7,18,28' ) )); ?>
    <?php $category = get_the_category();
    		if($category[0]){
    		echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    		}
    	?><?php ;?>

    I think I’m doing something wrong…

    Thread Starter jumust

    (@jumust)

    Maybe something in my file that doesn’t make it work. here it’s all the final part of the category.php template https://pastebin.com/FwePp8nN

    the if statement shouldn’t be followed by a semi-column .
    U can try using this snippet :

    <?php if (is_category( array( '3,7,18,28' ) ))
                    {$category = get_the_category();
    		if($category[0]){
    		echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
                        }
                    }
    	?>

    <?php is_category( array( 3,7,18,28 ) ); ?>
    Thread Starter jumust

    (@jumust)

    Thanks for your help!

    ahmedNaguib it doesn’t display link for any category.

    alchymyth I used this but shows link for all categories

    <?php is_category( array( 3,7,18,28 ) ); ?>
    <?php
    		$category = get_the_category();
    		if($category[0]){
    		echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    		}
    	?><?php ;?>

    sorry , I wrote a wrong array of categories , try it now:

    <?php if (is_category( array( 3,7,18,28 ) ))
                    {$category = get_the_category();
    		if($category[0]){
    		echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
                        }
                    }
    	?>

    Thread Starter jumust

    (@jumust)

    AWESOME ahmedNaguib it WORKED!!!

    Thanks so much

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘If is category id not working’ is closed to new replies.