Collect Wordpress category name

Asked

Viewed 1,540 times

0

I am trying to collect the category name with the following code

<a href="<?php get_cat_name( $cat_id ); ?>"></a>

But I’m not having much success. Another thing is, I have about 4 categories but the code needs to know if this using category a or b within 4, if there is no category that is A or B shows nothing.. if anyone can help me.

1 answer

0


The function get_cat_name returns a string as reported in the documentation, and on your line where you tag a you’re not using the echo nor <?= to display the name.

<a href="<?php echo get_cat_name( $cat_id ); ?>"></a>

OR

<a href="<?= get_cat_name( $cat_id ); ?>"></a>

With regard to checking you can make a simple if

<a href="<? echo (get_cat_name( $cat_id ) == 'a' || get_cat_name( $cat_id ) == 'b') ? 'exibe o resultado' : ''; ?>"></a>

Replace the "exibe o resultado" by the result you want to appear if category a or category b exists

  • Hello Rafael, thanks for the help, I used the code but it didn’t work: >> <a href="<? echo (get_cat_name( $cat_id ) == '48' || get_cat_name( $cat_id ) == '49') ? 'displays result' '; '; ? >"></a>

  • what error gave @Renatoassis?

  • Didn’t work @Rafaelacioly

  • @Renatoassis as you put the code?

  • in the same way that you see above > <a href="<? echo (get_cat_name( $cat_id ) == '48' || get_cat_name( $cat_id ) == '49') ? 'displays the result' : '; ? >"></a>

  • @Renatoassis suggest you study a little more than PHP to proceed with the use of wordpress no headache, I changed the answer to make it clearer.

  • I don’t understand PHP anymore I turn around the ways I can... you don’t understand very well what I want, I don’t need to display a precise specific result that only shows the name of the category.. if it is A displays the name A if it is B displays the name B, if it is not A or B displays nothing.. nothing more than that if you can help I am grateful.

  • I understand that the display result would be ideal to insert the function that informs the name of the category, but that’s where I can not.. I don’t know what function will do that.. I’ve tried every way.

  • Could you pass on more information? - @Rafaelacioly

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.