网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress类别:如何在单个帖子中显示名称,slug,描述和链接?

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:

enter image description here

So that I can use:

  1. the category slug as a CSS class, to style each category a unique color
  2. the category description as a Font Awesome class (in this case, \"fa-wrench\"), to assign each category a unique icon

(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:

  1. Category slug
  2. Category link
  3. Category description
  4. Category name

图片转代码服务由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>

</ a > </ p>

这样我就可以使用:</ p>

  1. 类别 slug </ strong>作为CSS类, 将每个类别设为一种独特的颜色</ li>

  2. 类别描述</ strong>作为Font Awesome类(在本例中为“fa-wrench”),为每个类别指定一个唯一的图标< / li>

    </ ol>

    (对于这个项目,每个帖子只会被分配一个类别,但我想一个面向未来的解决方案需要输出分配给帖子的所有类别 这种格式。)</ p>

    所以我真的只需要知道如何单独拉动:</ p>

    1. 类别slug </ li> \\ n
    2. 类别链接</ li>

    3. 类别说明</ li>

    4. 类别名称</ 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