【问题标题】:Is there a conditional in Wordpress for a specific tag page?Wordpress 中是否有针对特定标签页面的条件?
【发布时间】:2014-05-02 19:56:33
【问题描述】:

我想更改我的 Wordpress 网站上某个页面的元描述,该页面查询包含某个标签的所有帖子。

我说的页面是这样的:

mainwebsite.com/tag/tag-name

所以我想在 header.php 文件中添加一个条件,我只能在该页面上更改元描述。我尝试了以下方法:

<?php if(has_tag('this-is-the-tag-name')){
echo 'test';
}
?>

这会将“测试”放在每个标签页上。我希望更改仅适用于标签页“this-is-the-tag-name”。

【问题讨论】:

  • 你试过is_tag( 'this-is-the-tag-name' ) 吗?还可以尝试在 has_tag 函数中指定 tag_id 吗?

标签: php wordpress if-statement tags conditional


【解决方案1】:

假设您的标签名称是“slug”。查看标签的模板层次结构。 Wordpress 将从顶部开始,然后向下工作,直到找到可用的模板来显示标签

tag-slug.php > tag-id.php > tag.php > archive.php > index.php

话虽如此,如果您创建一个名为tag-slug.php 的模板,您只需将if( is_page_template( 'tag-slug.php' )) 语句添加到您的标题或functions.php,您的条件将仅显示在该特定页面上。

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 2014-10-25
    • 2022-09-23
    • 1970-01-01
    • 2020-02-21
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多