I want to highlight or show the new tag to the particular posts. When the user selects that the post the should be highlighted or show the new tag in the post list, the post should show new tag in the posts list in Front-end.
How I can achieve this?
The New tag means the new image or new text to highlight the post in the post list to draw user attention.
This is my code added in functions.php
:
function wpb_lastvisit_the_title ( $title, $id ) {
if ( is_singular() || get_post_type( $id ) == 'page' ) return $title;
// Check the post with the new tag
$tag_ids = wp_get_post_tags($post->ID, array('fields' => 'ids'));
if ($tag_ids == 'new')
$title .= '<span class="new-article">New</span>';
return $title;
}
add_filter( 'the_title', 'wpb_lastvisit_the_title', 10, 2);
CSS:
.new-article {
background: #feffdd;
padding: 3px;
border: 1px solid #eeefd2;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
margin-left:5px;
font-size: small;
font-weight: bold;
}
Any help is much appreciated.
I want to highlight or show the new tag to the particular posts. When the user selects that the post the should be highlighted or show the new tag in the post list, the post should show new tag in the posts list in Front-end.
How I can achieve this?
The New tag means the new image or new text to highlight the post in the post list to draw user attention.
This is my code added in functions.php
:
function wpb_lastvisit_the_title ( $title, $id ) {
if ( is_singular() || get_post_type( $id ) == 'page' ) return $title;
// Check the post with the new tag
$tag_ids = wp_get_post_tags($post->ID, array('fields' => 'ids'));
if ($tag_ids == 'new')
$title .= '<span class="new-article">New</span>';
return $title;
}
add_filter( 'the_title', 'wpb_lastvisit_the_title', 10, 2);
CSS:
.new-article {
background: #feffdd;
padding: 3px;
border: 1px solid #eeefd2;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
margin-left:5px;
font-size: small;
font-weight: bold;
}
Any help is much appreciated.
Share Improve this question edited Aug 26, 2020 at 7:21 Rahul Pamnani asked Aug 22, 2020 at 20:34 Rahul PamnaniRahul Pamnani 1051 silver badge13 bronze badges 7- What does new tag mean? New taxonomy or what? Can you correct your question? Add visual representation (Screenshots)? – Unbywyd Commented Aug 25, 2020 at 11:09
- @Unbywyd, New tag means the new image or new text to highlight the post in the post list to draw user attention. – Rahul Pamnani Commented Aug 25, 2020 at 18:35
- @Unbywyd, As you have seen in the news website, they use the new image or text to highlight the post in the posts list to draw user attention. – Rahul Pamnani Commented Aug 25, 2020 at 18:37
- I can help you, but I need more information, what is your template, and what type of post (page, post, custom type?) – Unbywyd Commented Aug 25, 2020 at 19:14
- @Unbywyd, I am using the default WordPress post template and default post lists. – Rahul Pamnani Commented Aug 25, 2020 at 20:48
1 Answer
Reset to default 0 +50Ok, to display the latest posts you use default Recent Posts widget.
You can see it here: Appearance -> Widgets
This widget has no settings and is fine for getting started, you can install any plugin which will add a more sophisticated widget to your site, you just need to search for: Recent Posts Widget
I found the following:
Recent Posts Widget With Thumbnails
latest-posts
smart-recent-posts-widget
And more...
Just go to plugins and add it to your site, then select it in widgets and customize accordingly
Edit
Change the post title relative to the date (if a month has not passed since the publication)
function wpb_lastvisit_the_title ( $title, $id ) {
if ( is_singular() || get_post_type( $id ) != 'post' ) return $title;
$p = get_post( $id );
$now = new DateTime();
$date = DateTime::createFromFormat(get_option( 'date_format' ), $p->post_date);
if($date){
$interval = $now->diff($date);
$days = $interval->d;
// If a month has not passed, show the label
if($days <= 30) {
$title .= ' <span class="new-article">New</span>';
}
}
return $title;
}
add_filter( 'the_title', 'wpb_lastvisit_the_title', 10, 2);
By tag:
function wpb_lastvisit_the_title ( $title, $id ) {
if ( is_singular() || get_post_type( $id ) != 'post' ) return $title;
$tag_ids = wp_get_post_tags($id, array('fields' => 'ids'));
// tag with name - new
$tagId = get_term_by('slug', 'new', 'post_tag') -> term_id;
if(in_array($tagId, $tag_ids)) {
$title .= ' <span class="new-article">New</span>';
}
return $title;
}
add_filter( 'the_title', 'wpb_lastvisit_the_title', 10, 2);