【问题标题】:Show Post Title and how many days ago it got published in Wordpress显示帖子标题以及它在 Wordpress 中发布的天数
【发布时间】:2017-01-10 20:42:19
【问题描述】:

我想在我的 WordPress 页面上自定义我的存档列表,使其显示如下内容:

发布标题 - 2 天前 帖子标题 - 4 天前 等等……

到目前为止,我只设法使用以下代码显示帖子标题:

<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 16) ); ?>

我不知道如何继续前进,有什么帮助吗?

【问题讨论】:

    标签: wordpress count archive


    【解决方案1】:

    函数 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 中找到有关过滤器的更多信息。

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多