Mudassar ali
Mudassar ali

Reputation: 301

Category permalink in wordpress

What is category permalink in wordpress?

     get_post_meta($post->ID, '_category_permalink', true);

What is the output for this?

Upvotes: 1

Views: 208

Answers (2)

Rohil_PHPBeginner
Rohil_PHPBeginner

Reputation: 6080

The given code will output nothing because get_post_meta is useful for the meta_values associated with the post.

So to get category link, there is one function in wordpress which is get_category_link but it has required parameter category_id. So you eventually you also need category ID also to get category link.

To get category ID,you can use get_the_category and you can do something like ,

$cat_name = get_the_category($post->ID);

So it will give you an array of object(If you want to see that array object, simply do var_dump($cat_name)) and you can get category_ID from that object.

So now you will have category link which you can get by get_category_link($cat_name[0]->cat_ID).

Final code to get category link :

    $cat_name = get_the_category($post->ID);
    echo get_category_link($cat_name[0]->cat_ID);

NOTE : If you want link in loop, you can simply use the_category

Upvotes: 0

Sunny
Sunny

Reputation: 432

permalink means seo friendly url of category, for get permalink you can use below code

$category_link = get_category_link( $category_id );

Upvotes: 3

Related Questions