如何将类别和标签描述合并到我的WordPress子主题中?

时间:2016-05-14 18:52:49

标签: php wordpress

我准备为我的WordPress网站推出新外观。当有那么多GPL主题时,我没有重新发明轮子,而是选择了一个具有我想要的大部分内容并开始定制它的方法。但是,我遇到了绊脚石。

在我定制的主题中,存档标题由functions.php中的函数控制。我的子主题的functions.php中有以下代码(这是从父主题的functions.php文件中的相同代码块派生的):

function theme_get_meta_title() {
$patt = '<div class="bg-white text-center text-uppercase meta-title"><h2 class="text-gamma">%s</h2></div>';

if(is_archive()) {
    return sprintf($patt, get_the_archive_title());
} else if(is_search()) {
    return sprintf($patt, get_search_query());
}

}

endif;

这会在我的内容和导航栏之间生成一个带有标题(类别标题,标签标题或日期)的白框。未显示的是我之前为我的类别和标签创建的描述。由于创建这些描述的麻烦,我希望他们在访问者查看类别或标记存档时显示在前端。我试图修改这个函数来添加描述,但我没有得到结果。

我首先尝试添加类别说明:

function theme_get_meta_title() {
$patt = '<div class="bg-white text-center text-uppercase meta-title"><h2 class="text-gamma">%s</h2></div>';

if(is_category()) {
    return sprintf($patt, get_the_category(), category_description());
} else if(is_archive()) {
    return sprintf($patt, get_the_archive_title());
} else if(is_search()) {
    return sprintf($patt, get_search_query());
}

}

endif;

然而,这不起作用,我尝试的其他排列也没有。我希望能够在标题后显示标签以及类别描述(但是在创建该白色框的div内),并且在基于日期的存档显示中,只需要一条股票消息说明,你是在标题之后浏览旧帖子的存档。这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

Sprintf通过查找匹配的参数来工作。您只在字符串中提供了一个参数,因此只匹配第一个参数。另外,您的代码中有错误。

 function theme_get_meta_title() {
    $patt = '<div class="bg-white text-center text-uppercase meta-title"><h2 class="text-gamma">%s</h2> <div class="description">%s</div>   </div>';

    if(is_category()) {
        return sprintf($patt, get_the_category(), category_description());
    } else if(is_archive()) {
        return sprintf($patt, get_the_archive_title());
    } else if(is_search()) {
        return sprintf($patt, get_search_query());
    }

}

答案 1 :(得分:0)

此链接显示了您尝试执行的操作的方法: wordpress forum

相关问题