我试图在帖子标题后添加“最近更新”。我发现了一些有效的代码,但由于插件和一些主题编码,它会影响the_title
不应该。这个问题在这里讨论https://pippinsplugins.com/use-the_title-and-the_title_attribute-correctly/ - 我已经做了一些更正/修复,但有些插件超出了我的能力。
现有代码(信用http://www.themesandco.com/snippet/adding-an-update-status-to-posts-in-wordpress/) 是
// add label to updated posts
add_filter(\'the_title\' , \'skips_add_update_status\');
function skips_add_update_status($html) {
//First checks if we are in the loop and we are not displaying a page
if ( ! in_the_loop() || is_page() )
return $html;
//Instantiates the different date objects
$created = new DateTime( get_the_date(\'Y-m-d g:i:s\') );
$updated = new DateTime( get_the_modified_date(\'Y-m-d g:i:s\') );
$current = new DateTime( date(\'Y-m-d g:i:s\') );
//Creates the date_diff objects from dates
$created_to_updated = date_diff($created , $updated);
$updated_to_today = date_diff($updated, $current);
//Checks if the post has been updated since its creation
$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;
//Checks if the last update is less than n days old. (replace n by your own value)
$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 14 ) ? true : false;
//Adds HTML after the title
$recent_update = $has_recent_update ? \'<span class="label label-default">Recently updated</span>\' : \'\';
//Returns the modified title
return $html.\' \'.$recent_update;
}
我想通过使用
post_class
然后,我将使用CSS以帖子标题为目标,并添加一个伪元素。我的问题是我不确定如何调整代码以插入。。。
$classes[] = \'update\';
return $classes;
大概是代替
//adds html...
现行法规的后续章节。
有人能帮忙吗?
这是我的起始代码,但它为所有帖子添加了“更新”类,而不仅仅是过去14天更新的帖子,我想这是因为没有与逻辑计算相关的链接。如果有更好的替代方法,我愿意接受。
function skips_add_update_status($classes) {
//Instantiates the different date objects
$created = new DateTime( get_the_date(\'Y-m-d g:i:s\') );
$updated = new DateTime( get_the_modified_date(\'Y-m-d g:i:s\') );
$current = new DateTime( date(\'Y-m-d g:i:s\') );
//Creates the date_diff objects from dates
$created_to_updated = date_diff($created , $updated);
$updated_to_today = date_diff($updated, $current);
//Checks if the post has been updated since its creation
$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;
//Checks if the last update is less than n days old. (replace n by your own value)
$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 14 ) ? true : false;
//add update class
$classes[] = \'update\';
return $classes;
}
add_filter(\'post_class\' , \'skips_add_update_status\');
最合适的回答,由SO网友:mrwweb 整理而成
你说得对。您的代码没有使用任何逻辑来确定是否添加CSS类。尝试以下操作:
function skips_add_update_status($classes) {
//Instantiates the different date objects
$created = new DateTime( get_the_date(\'Y-m-d g:i:s\') );
$updated = new DateTime( get_the_modified_date(\'Y-m-d g:i:s\') );
$current = new DateTime( date(\'Y-m-d g:i:s\') );
//Creates the date_diff objects from dates
$created_to_updated = date_diff($created , $updated);
$updated_to_today = date_diff($updated, $current);
//Checks if the post has been updated since its creation
$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;
//Checks if the last update is less than n days old. (replace n by your own value)
$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 14 ) ? true : false;
// check whether $has_recent_update is true
if( $has_recent_update ) {
//add update class
$classes[] = \'update\';
}
return $classes;
}
add_filter(\'post_class\' , \'skips_add_update_status\');
我删除了是否更新过的检查,因为您似乎没有使用它。
在此基础上,我认为注意最近的帖子比内容更具代表性。因此,您可以使用CSS完成全部工作。使用一些虚构的类,因为我不知道HTML是什么样子,可能是这样的:
.update .post-title:before {
content: "[Updated!]";
}
根据您的造型要求,您可能需要一些更别致的东西:
.update .post-title:before {
content: "[Updated!]";
display: inline-block;
padding: 2px;
background-color: red;
color: white;
}