php - 如何将可点击的标签和类别添加到 WordPress 帖子?
问题描述
我想将可点击的Tags
和添加Category
到我WordPress
的帖子模板中。我使用插件PHP
在 WordPress 帖子中显示。
这是我的代码:
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name.' ';
}
}
但我想显示可点击的标签和类别。PHP
我应该使用什么代码?
解决方案
您可以使用get_tag_link
获取标签的链接。看下面的例子:
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tag_link = get_tag_link($tag->term_id);
echo '<a href="' . $tag_link . '">' . $tag->name . '</a> ';
}
}
查看 WordPress 参考:https ://codex.wordpress.org/Function_Reference/get_the_tags
显示类别:
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
foreach( $categories as $category ) {
$category_link = '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . $category->name . '</a>';
echo $category_link;
}
参考:https ://developer.wordpress.org/reference/functions/get_categories/
推荐阅读
- android - 拖动动画期间的 Android Animate Alpha 现在可以工作
- alsa - 如何使用虚拟声卡驱动程序捕获音频?
- java - 可变归约到 StringBuilder 对象
- r - 如何为“Sas 中的 Select 语句”编写等效的 R 代码
- java - Java:A 类中 B 类的实例,其中 B 类使用 A 类中声明的变量
- java - 如何垂直显示用户输入的字符串与字符串中的字符一样多
- python - 黑色格式化程序 - 忽略特定的多行代码
- video-streaming - 来自 IP 摄像机的“实时采样”(非流式传输)
- reactjs - 将页面路由到组件中:React
- outlook - 如何使用 C# 在我有写权限的其他用户日历中创建一个 evet?