【问题标题】:latest posts by author作者的最新帖子
【发布时间】:2015-03-26 23:11:43
【问题描述】:

我正在使用 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('latestbyauthor', 'latest_posts_by_author');

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    是的,您可以使用 offset 参数。

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

    例如前 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 个帖子。

    【讨论】:

    • 谢谢,它有效。但我仍然有一个问题,它显示了相同的结果。再次感谢您的帮助
    • 没问题。你期待什么结果,从什么结果?
    猜你喜欢
    • 2017-03-12
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多