作者的最新帖子

时间:2015-03-26 23:11:43

标签: php wordpress plugins

我使用WP插件显示作者的最新帖子,插件将帖子显示为项目符号列表,我需要将其显示在两列中;通过编辑插件的PHP代码可行吗?

function latest_posts_by_author( $array ) {
global $post;

$defaults = array(
     'author' => '',
     'show' => 5,
     'excerpt' => 'false',
     'post_type' => 'post'
);

// Sets default author if in loop or on single page.    
if( in_the_loop() || is_single() ){
    $author_id=$post->post_author;
    $defaults['author'] = get_the_author_meta( 'user_login', $author_id );
}

// Overrides defaults with shortcode settings and separates into individual varaibles.
extract( shortcode_atts( $defaults, $array ) );

// Checks to make sure an author has been set.
if( !empty( $author ) ){

    // Checks to see if there are multiple authors set.
    $comma = strpos( $author, ',' );
    if( $comma === false ) {

        // Gets the author data for a single author.
        $author_data = get_user_by( 'login', $author );
        if( !empty( $author_data ) ) {
            $args = array(
                'author' => $author_data->ID,
                'posts_per_page' => $show,
                'post__not_in' => array($post->ID),
                'post_type' => $post_type
            );
        }

    } else {

        // Gets the author data for multiple authors.
        $authors = explode( ',', $author  );
        $author_data = '';
        foreach( $authors as $author_login ){
            $user = get_user_by( 'login', $author_login );
            $author_data .= $user->ID . ',';
        }

        $args = array(
            'author' => $author_data,
            'posts_per_page' => $show,
            'post__not_in' => array($post->ID)
        );
    }

    // Gets posts form database
    $author_query = new WP_Query( $args );

    // Displays posts if available
    if( $author_query ) {
        $html = '';
        $html = apply_filters( 'latestbyauthor_list_before', $html );
        $html .= '<ul class="latestbyauthor">';
        while ( $author_query->have_posts() ) : $author_query->the_post();
            $html .= '<li>';
            $html = apply_filters( 'latestbyauthor_link_before', $html );

            // Displays a link to the post, using the post title
            $html .= '<a href="' . get_permalink() . '" title="' . get_the_title() . '">';
            $html = apply_filters( 'latestbyauthor_title_before', $html );
            $html .= apply_filters( 'latestbyauthor_title', get_the_title() );
            $html = apply_filters( 'latestbyauthor_title_after', $html );
            $html .= '</a>';
            $html = apply_filters( 'latestbyauthor_link_after', $html );

            // Displays the post excerpt if "excerpt" has been set to true
            if($excerpt == 'true'){
                $html .= '<p>' . apply_filters( 'latestbyauthor_excerpt', get_the_excerpt() ) . '</p>';
            }

            $html .= '</li>';
        endwhile;
        $html .= '</ul>';
        $html = apply_filters( 'latestbyauthor_list_after', $html );
    }

    // Resets Post Data
    wp_reset_postdata();

    // Returns the results
   return $html;
}

} add_shortcode(&#39; latestbyauthor&#39;,&#39; latest_posts_by_author&#39;);

1 个答案:

答案 0 :(得分:0)

是的,您可以使用偏移参数。

基本上做2个单独的查询,第一个显示你喜欢的许多,然后下一个使用'offset'=&gt; x在参数中将下一个偏移5。

e.g。对于前5:

$defaults = array( 'author' => '', 'show' => 5, 'excerpt' => 'false', 'post_type' => 'post' );

第二个:

$defaults = array( 'author' => '', 'show' => 5, 'excerpt' => 'false', 'post_type' => 'post', 'offset'=> 5 );

https://codex.wordpress.org/Template_Tags/get_posts#Posts_list_with_offset

编辑:为了使它工作,您可以复制整个函数并创建一个具有不同函数名和短代码名称的新函数。在第二个只是偏移它你喜欢这种方式你可以调用两个短代码,你将有2个列表,每个5个帖子。