admin管理员组

文章数量:1391960

I want to add a "New" badge to the titles of recent post's widgets. whenever I add any new post from that time to 5 days this "New" badge should show after that it should disappear. this badge is a "new.gif" file. I also added an image for your reference.

I want to add a "New" badge to the titles of recent post's widgets. whenever I add any new post from that time to 5 days this "New" badge should show after that it should disappear. this badge is a "new.gif" file. I also added an image for your reference.

Share Improve this question asked Feb 3, 2020 at 13:24 Harvinder SinghHarvinder Singh 32 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

This should sort you out. I've commented as much as possible :) Place the code below in to your functions.php file.

function your_recent_posts_by_category_with_badge() {
    // WP_Query arguments
    $args = array(
        'category_name' => 'catname-1, catname-2', 
        //'cat' => 5,
        //'tag' => 'tagname',
        //'tag_id' => 5, 
        'posts_per_page' => 10 
    );

    // Set today's date
    $the_date_today = date('r');

    // Create WP_Query instance
    $the_query = new WP_Query( $args );

    // Check for posts
    if ( $the_query->have_posts() ) {

        // Set up HTML list & CSS classes for styling
        echo '<ul class="recentcategorybadgeposts widget_recent_entries">';

        // Loop through each post
        // Default is latest posts
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            // Compare post date from todays date
            $each_post_date = get_the_time('r');
            $difference_in_days = round( ( strtotime( $the_date_today ) - strtotime( $each_post_date) ) / ( 24 * 60 * 60 ), 0 );

            // Condition set for 5 days
            if ( $difference_in_days >= 5 ) {
                // If less than 5 days show IMG tag
                echo '<li><a href="' . get_the_permalink() .'">' . get_the_title() .'</a><img src="new.gif"></li>';
            } else { 
                // Else no show
                echo '<li><a href="' . get_the_permalink() .'">' . get_the_title() .'</a></li>';
            }

        } // end while

        // Close list
        echo '</ul>';
    } // endif

    // Reset Post Data
    wp_reset_postdata();
}

// Add a shortcode to use anywhere within your website
add_shortcode('recentcategorybadgeposts', 'your_recent_posts_by_category_with_badge');

// Enable shortcodes in text widgets
// Add a 'Text Widget' to your sidebar then in the input field, paste [recentcategorybadgeposts]
add_filter('widget_text', 'do_shortcode');

You could write your own plugin to create a unique Recent Posts widget as found here: https://themefuse/how-to-create-a-recent-posts-wordpress-plugin/

In the part titled 'Plugin Functionality' on the tutorial, change the code to:

<ul>
    <?php
    global $post;
    $today = date('r');
    $args = array( 'numberposts' => $dis_posts);
    $myposts = get_posts( $args );

    foreach( $myposts as $post ) : setup_postdata($post); 

        $articledate = get_the_time('r');
        $difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);

        if ($difference >= 5) { ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><img src="new.gif"></li>
        <?php } else { ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php } 

    endforeach; ?>
</ul>

本文标签: How to add badge quotnewquot to the post title which are showed in recent post widgets in wordpress