简体   繁体   English

WordPress的被张贴在类别永久链接

[英]Wordpress get posted in category permalink

So each post it will be posted in one category, and for each post I want to get the posted in category permalink, only the URL without markup. 因此,每个帖子都将发布在一个类别中,对于每个帖子,我都希望将其张贴在永久链接类别中,只有没有标记的URL。 How can I do that? 我怎样才能做到这一点?

//Explained again //再次解释

Well the post is posted in a category, right? 那么帖子是按类别发布的,对不对? Well I need to show that category, but not the name of the category, the url for that category. 好吧,我需要显示该类别,而不是类别名称,即该类别的网址。

I would use get_the_category (), which will return an array of objects, one for each category. 我将使用get_the_category (),它将返回一组对象,每个类别一个。 In your case, you'll get an array containing one object back, because you only have one category. 对于您的情况,您将获得一个包含一个对象的数组,因为您只有一个类别。 Then use get_category_link () to turn the ID of the category into a URL. 然后使用get_category_link ()将类别的ID转换为URL。

So, if you're in The Loop , I would do something like this (NB: untested!) 因此,如果您在The Loop中 ,我会做类似的事情(注意:未经测试!)

$categories = get_the_category();
$url = get_category_link($categories[0]->cat_ID);

Obviously, you'll want to make sure this doesn't fall on its backside if there are no categories assigned, for example. 显然,例如,如果没有分配类别,您将要确保它不会落在背面。

This is similar to what WordPress does if you include a category in your permalink structure -- it will get the category with the lowest numeric ID and use that in the permalink. 如果您在永久链接结构中包含类别,则这与WordPress的做法类似-它将获得具有最低数字ID的类别,并在永久链接中使用该类别。

No need to even place the anchor tag just place the following code, it will echo the URL and NAME as a permalink. 甚至不需要放置锚标记,只需放置以下代码,它将回显URL和NAME作为永久链接。 It's build in already in WordPress. 它已经内置在WordPress中。

<?php the_category(', ') ?>

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

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