【发布时间】:2017-01-10 20:42:19
【问题描述】:
我想在我的 WordPress 页面上自定义我的存档列表,使其显示如下内容:
发布标题 - 2 天前 帖子标题 - 4 天前 等等……
到目前为止,我只设法使用以下代码显示帖子标题:
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 16) ); ?>
我不知道如何继续前进,有什么帮助吗?
【问题讨论】:
我想在我的 WordPress 页面上自定义我的存档列表,使其显示如下内容:
发布标题 - 2 天前 帖子标题 - 4 天前 等等……
到目前为止,我只设法使用以下代码显示帖子标题:
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 16) ); ?>
我不知道如何继续前进,有什么帮助吗?
【问题讨论】:
函数 wp_get_archives 调用函数 get_archives_link 来准备输出。在该函数中完成的最后一步是应用与函数同名的过滤器(即 get_archives_link)。因此,要根据需要进行修改,请定义自己的过滤器函数并将该过滤器添加到您的 functions.php 文件中。
例如,以下代码将向函数 get_archives_link 的输出添加一个类。
function example_get_archives_link($link_html) {
if (is_day() || is_month() || is_year()) {
if (is_day()) {
$data = get_the_time('Y/m/d');
} elseif (is_month()) {
$data = get_the_time('Y/m');
} elseif (is_year()) {
$data = get_the_time('Y');
}
// Link to archive page
$link = home_url($data);
// Check if the link is in string
$strpos = strpos($link_html, $link);
// Add class if link has been found
if ($strpos !== false) {
$link_html = str_replace('<li>', '<li class="current-archive">', $link_html);
}
}
return $link_html;
}
add_filter("get_archives_link", "example_get_archives_link");
您可以在 Codex 中找到有关过滤器的更多信息。
【讨论】: