WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] If is category id not working (10 posts)

  1. jumust
    Member
    Posted 1 year ago #

    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?

  2. ahmedNaguib
    Member
    Posted 1 year ago #

    I think u need to use an if statement over there ,
    is_category() is returning true or false , but it is not used .

  3. deepbevel
    Member
    Posted 1 year ago #

    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.

  4. jumust
    Member
    Posted 1 year ago #

    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...

  5. jumust
    Member
    Posted 1 year ago #

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

  6. ahmedNaguib
    Member
    Posted 1 year ago #

    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>';
                        }
                    }
    	?>
  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    <?php is_category( array( 3,7,18,28 ) ); ?>
  8. jumust
    Member
    Posted 1 year ago #

    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 ;?>
  9. ahmedNaguib
    Member
    Posted 1 year ago #

    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>';
                        }
                    }
    	?>
  10. jumust
    Member
    Posted 1 year ago #

    AWESOME ahmedNaguib it WORKED!!!

    Thanks so much

Topic Closed

This topic has been closed to new replies.

About this Topic