Suppose I want to show posts from one certain author on the main index of a wordpress website, how would I go about this ?? Below is a loop from the twenty thirteen theme:
<?php
$curauth = (isset($_GET['liamhodnett'])) ? get_user_by('liamhodnett', $author) : get_userdata(intval($author));
?>
<?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; ?>
basically, all you need to do is set $curauth
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
example for an author page: http://codex.wordpress.org/Author_Templates
<?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 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 -->
</ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Edit
I will explain what $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
does
isset($_GET['author_name']) ?
checks whether a parameter with the username is present in the URL, like: www.myexamplewebsite.com/author/danieltulp it is a shorthand if/else statement
if URL has an username the code will try to get it with get_user_by('slug', $author_name)
if it doesn't it will try to get it with the get_userdata Wordpress function with get_userdata(intval($author))
of course, you are not refering to the user in the URL, so you just need to set the currentauth like:
$curauth = (is_home()) ? "liamhodnett" : (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
Edit 2 If all else fails (haven't tested the code above, so could very well be possible), make your own database call with get_posts() :
$args = array(
'author' => 1 // this should be your user ID, or use 'author_name' => 'liamhodnett'
);
// get my posts 'ASC'
$myposts = get_posts( $args );
Then use the $mypost array for your loop:
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata();?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.