我期待着在WP网站主页的某个地方显示一个图例,上面写着:
Last updated: XX/XX
从最近的帖子中抓取日期,但不显示任何内容,只显示日期。
有什么快速的方法可以做到这一点吗?
我期待着在WP网站主页的某个地方显示一个图例,上面写着:
Last updated: XX/XX
从最近的帖子中抓取日期,但不显示任何内容,只显示日期。
有什么快速的方法可以做到这一点吗?
我会在后期保存时保存一个选项:
add_action(
\'save_post\',
function($id,$p) {
if (
(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
|| (defined(\'DOING_AJAX\') && DOING_AJAX)
|| ($p->post_status === \'auto-draft\')
) {
return;
}
update_option(\'_last_site_update\',$p->post_date);
},
1,2
);
并使用@G-M提供的函数变体检索它:function my_last_updated( $format = \'\' ) {
$last = get_option(\'_last_site_update\');
if ( empty($last) ) return;
if ( empty($format) ) {
$format = get_option(\'date_format\');
}
return mysql2date($format,$last);
}
echo my_last_updated();
这样您可以:将繁重的工作推到管理端,消除了完整post查询的不必要工作(via
wp_get_recent_posts
),a very simple get_option
query,
有不同的方法可以做到这一点。
在我看来,最简单的方法是wp_get_recent_posts
检索最后一篇文章并打印文章修改日期。
将其包装到函数中,使其具有灵活性和可重用性。在您的functions.php
您可以放置:
function my_last_updated( $format = \'\' ) {
$lasts = wp_get_recent_posts( array(\'numberposts\'=>1, \'post_status\'=>\'publish\') );
if ( empty($lasts) ) return;
$last = array_pop($lasts);
if ( empty($format) ) $format = get_option(\'date_format\');
return mysql2date( $format, $last[\'post_modified\'] );
}
然后您可以这样使用它:<p>Last updated: <?php echo my_last_updated() ?>.</p>
论点$format
让您为日期选择不同的日期格式,请参见here 选择一个。如果未传递任何内容,则使用WP options中设置的格式。我有一个如下查询:$args = arguments for the query $query = new WP_Query($args); while ($query->have_posts()): $query->the_post(); $post_id = get_the_ID(); echo my_function($post_id); endwhile; wp_reset_query(); endif; 然后在my_func