简体   繁体   English

wordpress中的类别永久链接

[英]Category permalink in wordpress

What is category permalink in wordpress? 什么是wordpress中的类别永久链接?

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

What is the output for this? 这是什么输出?

永久链接意味着seo友好的类别网址,为了获得永久链接,你可以使用下面的代码

$category_link = get_category_link( $category_id );

The given code will output nothing because get_post_meta is useful for the meta_values associated with the post . 给定的代码会输出什么,因为get_post_meta是有用的meta_values帖子相关联。

So to get category link, there is one function in wordpress which is get_category_link but it has required parameter category_id . 因此,要获得类别链接,wordpress中有一个函数是get_category_link但它需要参数category_id So you eventually you also need category ID also to get category link. 所以你最终还需要类别ID才能获得类别链接。

To get category ID,you can use get_the_category and you can do something like , 要获取类别ID,您可以使用get_the_category ,您可以执行类似的操作,

$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. 因此它将为您提供一个对象数组(如果您想查看该数组对象,只需执行var_dump($cat_name) ),您就可以从该对象获取category_ID

So now you will have category link which you can get by get_category_link($cat_name[0]->cat_ID) . 所以现在你将有get_category_link($cat_name[0]->cat_ID)获得的category link

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 注意:如果您想要循环链接,您只需使用the_category

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM