你的代码没有错。然而,在我看来,以稍微不同的方式处理这项任务会更好。
Usage
在您的功能中。php:
/* This function returns the related posts in an array of objects. You can set how many items you want retrieve.If not set, the number of items will be set to "1". */
function get_related_author_posts( $items = 1 ) {
global $authordata, $post;
$authors_posts = get_posts(
array(
\'author\' => $authordata->ID,
\'post__not_in\' => array( $post->ID ),
\'posts_per_page\' => $items,
\'post_type\' => \'artistblog\'
)
);
return $authors_posts;
}
在模板文件中:
<?php
$related_posts = get_related_author_posts( 3 );
foreach( $related_posts as $post ) : setup_postdata( $post ); ?>
<div class="artist-blog-thumb">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'medium\' ); ?></a>
</div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="excerpt">
<p><?php the_excerpt(); ?></p>
</div>
<?php endforeach; wp_reset_postdata(); ?>
如果你卡住了,请告诉我。