I need my author archive page to display only the custom post type 'products'. The code I have below is displaying posts in the post type by ALL authors instead of just one.
<?php query_posts( 'post_type=products' ); if (have_posts()) : ?>
<div class="product_list_wrap">
<?php while (have_posts()) : the_post(); ?>
<div class="product_list_single" id="post-<?php the_ID(); ?>">
<a class="product_list_image_link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'product_list_image', array('class' => 'product_list_image', 'alt' => get_the_title()) ); ?>
</a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="product_title"><?php the_title(); ?></a>
<?php if (get_post_meta($post->ID, 'mp_price', true) != '' ) { ?>
<div class="price">$<?php echo number_format(get_post_meta($post->ID, 'mp_price', true),2);?></div>
<?php } ?>
<div class="clear"></div>
</div><!-- end .post -->
<?php endwhile; ?>
<div class="clear"></div>
<?php else : ?>
<div class="user_no_products">
We're sorry, this user doesn't have any products yet.
</div>
</div><!-- end .product_list_wrap -->
<?php endif; wp_reset_query(); ?>
Also, I want to display some information about the author, I have the following code that displays ALL authors, but I don't know how to make it <em>only</em> display the author of the author archive page you're on.
<?php
// Displays user name and email from users with at least one post
$blogusers = get_users_of_blog();
if ($blogusers)
{
foreach ($blogusers as $bloguser)
{
$args = array(
'author' => $bloguser->user_id,
'showposts' => 1,
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
$user = get_userdata($bloguser->user_id);
$out='';
$out.='<div id="about_the_artist">';
$out.='<h2>'. $user->display_name .'</h2>';
$out.='<div id="artist_info">';
$out.='<img src="http://www.gravatar.com/avatar/' . md5( strtolower( trim( $user->user_email) ) ).'?s=90" />';
$out.='<div id="artist_about">';
$out.=$user->description;
if(isset($user->twitter) || isset($user->user_url))
{
$out.='<div id="artist_links">';
if(isset($user->gender) || isset($user->age) || isset($user->location) )
{
$out.='<span>';
if(isset($user->gender))
{
$out.= $user->gender.', ';
}
if(isset($user->age))
{
$out.= $user->age.' years old, ';
}
if(isset($user->location))
{
$out.= $user->location;
}
$out.='</span>';
}
if(isset($user->user_url) && $user->user_url!='')
{
$out.='<span><a href="'. $user->user_url .'" title="Website">My Website</a></span>';
}
if(isset($user->twitter))
{
$out.='<span><a href="http://twitter.com/'. $user->twitter .'" title="Follow '. $user->display_name .' on Twitter" id="twitter_link">@'. $user->twitter .'</a></span> ';
}
$out.='<div class="clear"></div>';
$out.='</div>';
}
$out.='</div>';
$out.='</div>';
$out.='<div class="clear"></div>';
$out.='</div>';
echo($out);
}
}
}
?>
AdamGold answers:
About your first question, go to author.php and replace:
if ( have_posts() )
in:
query_posts( 'post_type=products' ); if (have_posts())
AdamGold comments:
About your second question - Sorry, but I didn't quite understand. Can you please re-explain?
Thanks
Adam
Jake Caputo comments:
I'm not quite sure what you're saying. That's already there.
Jake Caputo comments:
I'd like to display the avatar, bio, and some other information for the author. The code I provided returns ALL authors on the site, not just the one of the archive you're currently viewing.
AdamGold comments:
Instead of your whole foreach loop, try this code:
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
This will get the user data of the current author.
AdamGold comments:
So the code should be something like this:
<?php
// Displays user name and email from users with at least one post
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
$args = array(
'author' => $curauth->ID,
'showposts' => 1,
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
$user = $curauth;
$out='';
$out.='<div id="about_the_artist">';
$out.='<h2>'. $user->display_name .'</h2>';
$out.='<div id="artist_info">';
$out.='<img src="http://www.gravatar.com/avatar/' . md5( strtolower( trim( $user->user_email) ) ).'?s=90" />';
$out.='<div id="artist_about">';
$out.=$user->description;
if(isset($user->twitter) || isset($user->user_url))
{
$out.='<div id="artist_links">';
if(isset($user->gender) || isset($user->age) || isset($user->location) )
{
$out.='<span>';
if(isset($user->gender))
{
$out.= $user->gender.', ';
}
if(isset($user->age))
{
$out.= $user->age.' years old, ';
}
if(isset($user->location))
{
$out.= $user->location;
}
$out.='</span>';
}
if(isset($user->user_url) && $user->user_url!='')
{
$out.='<span><a href="'. $user->user_url .'" title="Website">My Website</a></span>';
}
if(isset($user->twitter))
{
$out.='<span><a href="http://twitter.com/'. $user->twitter .'" title="Follow '. $user->display_name .' on Twitter" id="twitter_link">@'. $user->twitter .'</a></span> ';
}
$out.='<div class="clear"></div>';
$out.='</div>';
}
$out.='</div>';
$out.='</div>';
$out.='<div class="clear"></div>';
$out.='</div>';
echo($out);
}
?>
AdamGold comments:
And the code for the first question:
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
<?php query_posts( 'post_type=products&author_name=' . $curauth->nickname ); if (have_posts()) : ?>
<div class="product_list_wrap">
<?php while (have_posts()) : the_post(); ?>
<div class="product_list_single" id="post-<?php the_ID(); ?>">
<a class="product_list_image_link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'product_list_image', array('class' => 'product_list_image', 'alt' => get_the_title()) ); ?>
</a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="product_title"><?php the_title(); ?></a>
<?php if (get_post_meta($post->ID, 'mp_price', true) != '' ) { ?>
<div class="price">$<?php echo number_format(get_post_meta($post->ID, 'mp_price', true),2);?></div>
<?php } ?>
<div class="clear"></div>
</div><!-- end .post -->
<?php endwhile; ?>
<div class="clear"></div>
<?php else : ?>
<div class="user_no_products">
We're sorry, this user doesn't have any products yet.
</div>
</div><!-- end .product_list_wrap -->
<?php endif; wp_reset_query(); ?>
Jake Caputo comments:
Awesome! That worked. Thanks!
Oleg Butuzov answers:
1) replace
'post_type=products'
by
'post_type=products&author_name=username'
Oleg Butuzov comments:
2) you can try...
global $wp_query;
$user = get_userdata($wp_query->posts[0]->post_author);
Jake Caputo comments:
I don't know what the usernames are going to be, so I don't think that will work.
Jake Caputo comments:
2) this isn't working either, unless i'm putting it in the wrong spot.
Oleg Butuzov comments:
hm what will be the source of the username? in normal WP its a wp_query variable (witch comes from the parsed query...)
for example.. somewhere on site you have link (oh wait this isn't a link)
$rules ['products/author/([^/]+)/page/?([0-9]{1,})/?$'] = 'index.php?post_type=products&author_name=$matches[1]&paged=$matches[3]';
$rules['products/author/([^/]+)/?$'] = 'index.php?post_type=products&author_name=$matches[1]';
this is a part fo rewrite rules array witch work with authors for a custom post type.
so after someone click at /products/jake link it will go to the results page with a search of custom query for post_type procukts and username jake.
however you will need also to have and some links that will posint to the /products/jake
note this isn't custom query, this is a native query for wp (with native paging support), so no need to ask query_posts() each time in template.
Oleg Butuzov comments:
i think first stage for you should be the quering authors posts
and only after that tring to autor data =) (ps, its already should be already in querid_object at wp_query object)
thamiziniyan supa answers:
Use this code to get the current authors posts only,
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
<h2>Posts by <?php echo $curauth->nickname; ?>:</h2>
<ul>
<!-- The Loop -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
<?php the_title(); ?></a>,
<?php the_time('d M Y'); ?> in <?php the_category('&');?>
</li>
<?php endwhile; else: ?>
<p><?php _e('No posts by this author.'); ?></p>
<?php endif; ?>
<!-- End Loop -->
Will tell you the solution for second one
thamiziniyan supa comments:
<?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>
$curauth This variable is for current author.
Use these two codes for your need... and customize it for custom post type.