0

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.

enter image description here

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.

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. 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. 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. Commented Aug 25, 2020 at 20:48

1 Answer 1

0
+50

Ok, 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:

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);
17
  • I know that, but I want that, if I want to highlight the post that is old 2 months and show new image to that post in the post list. Commented Aug 26, 2020 at 6:22
  • You cannot do this with a standard widget, or use a plugin which suits your requirements as a last resort, create your own shortcode (programming knowledge required) with the output of post list as you like
    – Unbywyd
    Commented Aug 26, 2020 at 6:27
  • In short, use a ready-made plugin or write your own, sorry, but there are no more answers to your very global question
    – Unbywyd
    Commented Aug 26, 2020 at 6:29
  • Please check the image. Okay, But we can write the code for it. Like we can add the new in the tags and when the post has the new tag, we can add the new image before or after the title in the post list. Commented Aug 26, 2020 at 6:37
  • 1
    ok, I see you also have errors in the code, I will correct
    – Unbywyd
    Commented Aug 26, 2020 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.