像这样的方法应该会奏效:
// Assuming you\'ve got $author_id set
// and your post type is called \'your_post_type\'
$args = array(
\'author\' => $author_id,
\'post_type\' => \'your_post_type\',
);
$author_posts = new WP_Query( $args );
if( $author_posts->have_posts() ) {
while( $author_posts->have_posts() ) {
$author_posts->the_post();
// title, content, etc
$author_posts->the_title();
$author_posts->the_content();
// you should have access to any of the tags you normally
// can use in The Loop
}
wp_reset_postdata();
}
参考
WP_Query
class
使用作者模板文件,可以在作者模板内执行此操作:
author.php
-- 此文件属于主题的目录
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<!-- This sets the $curauth variable -->
<?php
$curauth = (isset($_GET[\'author_name\'])) ?
get_user_by(\'slug\', $author_name) :
get_userdata(intval($author));
?>
<h2>About: <?php echo $curauth->nickname; ?></h2>
<dl>
<dt>Website</dt>
<dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
<dt>Profile</dt>
<dd><?php echo $curauth->user_description; ?></dd>
</dl>
<h2>Posts by <?php echo $curauth->nickname; ?>:</h2>
<ul>
<!-- The Loop -->
<?php
// Assuming your post type is called \'your_post_type\'
$args = array(
\'author\' => $curauth->ID,
\'post_type\' => \'your_post_type\',
);
$author_posts = new WP_Query( $args );
if( $author_posts->have_posts() ) {
while( $author_posts->have_posts() ) {
$author_posts->the_post();
// title, content, etc
the_title();
the_content();
// you should have access to any of the tags you normally
// can use in The Loop
}
wp_reset_postdata();
}
?>
<!-- End Loop -->
</ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
这个
author.php
模板代码无耻地抄袭自
the Codex, 而且可能应该被视为一个起点,而不是最终产品。