Products
GG网络技术分享 2025-03-18 16:12 2
What I want to do is, within single.php
, pull different properties of the Category separately, like this:
<?php if ( have_posts() ) : ?><?php while ( have_posts() ) : the_post(); ?>
<a class=\"CATEGORYSLUG\" href=\"CATEGORYLINK\">
<i class=\"fas CATEGORYDESCRIPTION\"></i>
<span>CATEGORYNAME</span>
</a>
<?php endwhile; ?>
<?php endif; ?>
To produce an end product like this:
So that I can use:
(For this project, each post will only be assigned a single category, but I suppose a future-proof solution would need to output ALL categories assigned to the post in this format.)
So I really just need to know how to individually pull:
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我想要做的是,在 single.php </ code>中,拉出不同的属性 分类,如下:</ p>
&lt;?php if(have_posts()):?&gt; &lt;?php while(have_posts()):the_post() ; ?&gt;
&lt; a class =“CATEGORYSLUG”href =“CATEGORYLINK”&gt;
&lt; i class =“fas CATEGORYDESCRIPTION”&gt;&lt; / i&gt;
&lt; span&gt; CATEGORYNAME&lt; / span&gt ;
&lt; / a&gt;
&lt;?php endwhile; ?&gt;
&lt;?php endif; ?&gt;
</ code> </ pre>
要生成这样的最终产品:</ p>
这样我就可以使用:</ p>
- 类别 slug </ strong>作为CSS类, 将每个类别设为一种独特的颜色</ li>
- 类别描述</ strong>作为Font Awesome类(在本例中为“fa-wrench”),为每个类别指定一个唯一的图标< / li>
</ ol>
(对于这个项目,每个帖子只会被分配一个类别,但我想一个面向未来的解决方案需要输出分配给帖子的所有类别 这种格式。)</ p>
所以我真的只需要知道如何单独拉动:</ p>
- 类别slug </ li> \\ n
- 类别链接</ li>
- 类别说明</ li>
- 类别名称</ li>
</ ol>
</ div>
网友观点:
You can use the get_the_terms()
function to get all of the categories assigned to that post object and loop through each one to create your icons etc.
Below is an example, I\'ve called each individual variable so that you can see the object properties clearly.
This will also work if you only have one category assigned.
//get all categories:$categories = get_the_terms(get_the_ID(), \'category\');
//loop through categories
foreach ($categories as $category) {
//get variables
$slug = $category->slug;
$link = get_term_link($category->term_id);
$descr = $category->description;
$name = $category->name;
//echo your content
echo \'<a class=\\\"\'.$slug.\'\\\" href=\\\"\'.$link.\'\\\"><i class=\\\"fas \'.$descr.\'\\\"></i><span>\'.$name.\'</span></a>\';
}
###
You can get this using following code.
$postcat = get_the_category( $post->ID );
Its return all the fields which you want for more info refer this https://developer.wordpress.org/reference/functions/get_the_category/
Demand feedback