admin管理员组文章数量:1334342
I'm trying to have a page where a user can view all of the custom post types after they click on an author from a previous page, but I'm having no luck with anything I can find in the built-in PHP functions with Wordpress.
Is this some easy to query? I haven't found much online about it.
I'm trying to have a page where a user can view all of the custom post types after they click on an author from a previous page, but I'm having no luck with anything I can find in the built-in PHP functions with Wordpress.
Is this some easy to query? I haven't found much online about it.
Share Improve this question asked Jul 22, 2013 at 20:38 m0ngr31m0ngr31 1511 gold badge1 silver badge2 bronze badges 2- Is it one specific post type, or do you mean "anything that isn't a WordPress core type"? – Pat J Commented Jul 22, 2013 at 20:45
- It's just one specific custom post type. – m0ngr31 Commented Jul 22, 2013 at 20:48
2 Answers
Reset to default 6Something like this should work:
// 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();
}
Reference
WP_Query
class
Using an Author Template file
You can do this inside an Author Template:
author.php
-- this file belongs in your theme's directory
<?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(); ?>
This author.php
template code is shamelessly cribbed from the Codex, and should probably be considered a starting point, not an end product.
Use pre_get_posts
to add your custom post type to your author.php template
In your functions.php, add the following code. This will add your custom post type to the main query so that it will appear on your author page
function wpse107459_add_cpt_author( $query ) {
if ( !is_admin() && $query->is_author() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'YOUR_CUSTOM_POST_TYPE' ) );
}
}
add_action( 'pre_get_posts', 'wpse107459_add_cpt_author' );
With this, there are no need to make any changes to your template files :-)
本文标签: Getting a list of custom posts by author
版权声明:本文标题:Getting a list of custom posts by author 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742270785a2444264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论