如何将可点击的标签和类别添加到 WordPress post?
How can I add clickable tags and category to WordPress post?
我想将可点击的 Tags
和 Category
添加到我的 WordPress
post 模板中。我在 WordPress post.
中使用 PHP
显示插件
这是我的代码:
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name.' ';
}
}
但我想显示可点击的标签和类别。
我应该使用什么 PHP
代码?
您可以使用get_tag_link
获取标签的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/
我想将可点击的 Tags
和 Category
添加到我的 WordPress
post 模板中。我在 WordPress post.
PHP
显示插件
这是我的代码:
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name.' ';
}
}
但我想显示可点击的标签和类别。
我应该使用什么 PHP
代码?
您可以使用get_tag_link
获取标签的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/