>想知道是否有人可以提供帮助; 这对我来说似乎有点复杂;
顺便说一句,我已经在我的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; ?>
您需要根据
作者元值获取帖子,
因此,请先获取该帖子的虚拟作者姓名,
$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);