【问题标题】:Wordpress Get author PostsWordpress 获取作者帖子
【发布时间】:2014-11-16 12:27:38
【问题描述】:

想知道是否有人可以提供帮助;对我来说似乎有点复杂;

基本上我已经在我的wordpress网站中添加了这个功能,所以人们可以更改作者的名字;

add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );

function guest_author_name( $name ) {
global $post;

$author = get_post_meta( $post->ID, 'author_name', true );

if ( $author )
$name = $author;

return $name;
}  

但是现在我想添加一些代码来显示当前帖子作者的帖子列表,但它返回的是原始作者而不是顶部函数将其更改为的作者;下面是我用来做这个的函数,这可以改变吗?

function get_related_author_posts() {
    global $authordata, $post;
    $authors_posts = get_posts( array( 'author' => $author_name->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
    $output = '<div class="morepost"><h3>More posts from this author</h3>';
    foreach ( $authors_posts as $authors_post ) {
        $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
    }
    $output .= '</ul></div>';
    return $output;
}

然后这个;

<?php echo get_related_author_posts(); ?>

知道它有点复杂,如果有人能帮忙就太好了

D

这是我到目前为止想出的,但有人可以告诉我这段代码哪里出了问题

<?php $author = get_post_meta( $post->ID, 'author_name', true ); $args
= array(
     'meta_query' => array (
           array(
              'key' => 'author_name',
            'value' => $author
        )
        ),    'post__not_in' => array( $post->ID ),   'posts_per_page' => 3 ); ?> <?php if ( $wp_query->have_posts() ) : ?> <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> DO something

<?php endwhile; ?> NOT POSTS <?php endif; ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您需要根据作者元值获取帖子,

    所以首先获取该帖子的虚拟作者姓名,

    $author = get_post_meta( $post->ID, 'author_name', true );
    

    现在根据这个值获取所有帖子,

    $args = array(
        'meta_query' => array(
            array(
                'key' => 'author_name',
                'value' => $author
            )
        ),
        'post__not_in' => array( $post->ID )
        'posts_per_page' => 3
    );
    $posts = get_posts($args);
    

    【讨论】:

    • 我可以将它添加到侧边栏吗?
    • 将此添加到您的函数get_related_author_posts 并在您想要的任何地方调用它。
    猜你喜欢
    • 1970-01-01
    • 2011-03-12
    • 2013-10-11
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多