简体   繁体   English

Wordpress获取作者帖子

[英]Wordpress Get author Posts

Wonder if someone can help; 想知道是否有人可以提供帮助; its seems a bit complicated to me; 对我来说似乎有点复杂;

Baisically I've added this function in my wordpress site, so people can change the author's name; 通常,我已经在我的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;
}  

But now I want to add some code to show a list of posts from the current posts author but its returning the original author not the author that the top function changed it to; 但是现在我想添加一些代码来显示当前帖子作者的帖子列表,但是它返回原始作者,而不是top函数将其更改为的作者; below is the function I'm using to do this, is this possible to change? 以下是我用来执行此操作的功能,是否可以更改?

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;
}

then this; 然后这个

<?php echo get_related_author_posts(); ?>

Know its a bit complicated, if anyone can help that be great 知道这有点复杂,如果有人能帮上大忙

D d

This is what Ive come up with so far but could someone show me where I'm going wrong with this code 这是我到目前为止提出的,但是有人可以告诉我这段代码在哪里出问题

<?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; ?>

You need to get post based on the author meta value, 您需要根据作者的meta值获取帖子,

So get the virtual author name first for that post, 因此,首先获取该帖子的虚拟作者姓名,

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

Now get all posts based on this value, 现在,根据此值获取所有帖子,

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM