【发布时间】:2014-03-31 21:15:23
【问题描述】:
我已将标签添加到我的 Wordpress 页面(工作正常),但我正在尝试在页面上列出它们。
这个想法是获取特定页面(不是帖子)具有的所有标签并格式化它们。我得到的是 2 个随机(不相关)标签或整个标签列表。我不确定我在这里缺少什么。
$relatedArea = the_slug(false); //gets the slug of the page - false represses the echo statement
$allTags = get_tags(); //gets all the tags
$areasOfLaw = array(); //starts the array
// check if the tag is included on the page
foreach ($allTags as $tag) {
if(has_tag($relatedArea)) {
$areasOfLaw[] = $tag; //add to the array
}
}
试过$areasOfLaw = get_the_tags(),但它给了我随机标签。
这是格式,但我很确定它工作正常。
foreach($areasOfLaw as $tag){
echo '
<a href="'.get_site_url().'/services/'.$tag->slug.'">
<li>'.$tag->name.'
<span class="glyphicon glyphicon-chevron-right pull-right action"></span>
</li></a>';
}
编辑:这是函数文件中包含的我的标签支持
// add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
// ensure all tags are included in queries
function tags_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}
// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');
【问题讨论】:
-
你是如何在循环内使用
get_the_tags()函数的? -
我一直使用它作为
$areasOfLaw = get_the_tags();,但它没有返回我想要的东西