Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

WordPress按文章修改时间排序和显示最新更新日期[0]

Posted on 2023年11月14日 by

经常性的更新已有网页,但是wordpress的显示规则一般是按照发布日期进行排序,这样对于显示重要经常更新的文档很不友好,需要将其改成按照更新日期进行排序的方式。按照一下代码可以实现。

  • 文章排序

修改function.php

// sort by modify time
function order_posts_by_mod_date($orderby) 
{   
    if  (is_home() || is_archive() || is_feed()) 
    {     
        $orderby = "post_modified_gmt DESC";   
    }   
    return $orderby; 
} 
add_filter('posts_orderby', 'order_posts_by_mod_date', 999);

 

  • 显示最近更新日期和版权协议

方式一:修改代码(推荐)

在主题编辑器=>function.php,添加如下代码:

function wpb_last_updated_date( $content ) {
$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
$custom_content = ''; 
if ($u_modified_time >= $u_time + 86400) { 
$updated_date = get_the_modified_time('Y/m/d');
$custom_content .= '<p class="last-updated">Last updated on '. $updated_date  .' CC BY-SA 4.0 </p>';  
} 
 
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

在style.css增加如下代码:

.last-updated {
    color: #db7c22;
background: #fff4b9;
border: 1px solid #eac946;
overflow: hidden;
margin: 10px 0;
padding: 15px 15px 15px 35px;
font-size: 14px;
}

效果如下:

 

方式二:安装Last Updated Shortcode这款插件

然后使用方法就是直接在写文章的时候插入短代码[lastupdated]就可以了,像下图这样。

效果如下图:

  • 补充说明php关于时间格式的问题

参考资料:

  1. https://blog.csdn.net/qq_37640597/article/details/104771874
  2. https://blog.naibabiji.com/skill/wordpress-xian-shi-zui-hou-geng-xin-shi-jian.html
  3. https://www.php.net/manual/en/function.date.php
  4. https://www.php.net/manual/en/datetime.format.php
  5. https://wordpress.org/support/article/formatting-date-and-time/

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT