建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

wordpress中获取当前文章的分类和链接(WordPress获取当前文章ID标签:the_ID)

GG网络技术分享 2025-03-18 16:13 5


wordpress中获取当前文章的分类和链接

在开发wordpress主题时,我们会在很多地方用到,获取当前文章的分类和链接,因此我们需要熟练地掌握此方法,下面说一下如何获取当前文章的分类名称和链接。

需要用到下面三个函数

获取当前文章的ID:get_the_ID()

获取当前文章的分类:get_the_category($postid) //传入文章ID

获取当前文章的链接:get_term_link($slug, $taxonomy) //传入get_the_category中返回的 slug和taxonomy字段即可。

可以根据具体使用时,封装对应的方法,下面是我封装的方法

WordPress获取当前文章ID标签:the_ID

WordPress模板标签the_ID用于输出当前文章的ID,需要用在The Loop主循环里。

the_ID()

使用示例:

<p>文章ID: <?php the_ID(); ?></p>

扩展阅读

the_ID()函数位于:wp-includes/post-template.php

相关函数:

  • get_the_ID()
  • the_title()
  • the_title_attribute()

标签:

提交需求或反馈

Demand feedback