显示每个作者的最新文章,如果文章不超过一个月



我有一个出现在每个页面上的作者(Wordpress)列表,因此该列表存在于循环之外。

我设法用他们的名字显示每个作者的图像,但我想获得他们最新的文章标题链接到该文章。文章的标题应该只在文章发布不超过一个月的时候显示。

如有任何帮助,不胜感激。

感谢
<?php
        global $wpdb;
        $query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '1' ORDER BY 'ASC' LIMIT 20";
        $author_ids = $wpdb->get_results($query);
        foreach($author_ids as $author) :
            // Get user data
            $curauth = get_userdata($author->ID);
            // Get link to author page
            $user_link = get_author_posts_url($curauth->ID);
            $post_link = get_permalink($curauth->ID);

            // Set default avatar (values = default, wavatar, identicon, monsterid)
            $main_profile = get_the_author_meta('mainProfile', $curauth->ID);
            $hover_profile = get_the_author_meta('hoverProfile', $curauth->ID);
            $award_profile = get_the_author_meta('awardProfile', $curauth->ID);
    ?>

您可以使用WP_Query为您创建一个新的循环。从3.7版开始,它接受一个很酷的date_query参数。未经测试,但应该可以工作。

编辑:

$args =  array(
    'showposts' => 1,
    'orderby' => 'date',
    'date_query' => array(
        array(
            'after' => array(
                'year'  => date( "Y" ),
                'month' => date( "m", strtotime( "-1 Months" ) ),
                'day'   => date( "t", strtotime( "-1 Months" ) ),
            ),
            'inclusive' => true,
        )
) );
$query = new WP_Query( $args );

然后你可以运行一个常规的循环

// run the loop with $query
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo 'Latest post: ' . get_the_title();
    }
} else {
    // no posts
}

最新更新