2013-10-29 51 views
0

假设我想显示一个特定作者在wordpress主网站的主索引上的帖子,我该如何解决这个问题?下面是从二十13主题的循环:显示与某些作者相关的WordPress的帖子

<?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; ?> 

回答

1

基本上,所有你需要做的是设置$ curauth

<?php 
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); 
?> 

例如,对于作者网页: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(); ?> 

编辑

我将解释什么$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));确实

isset($_GET['author_name']) ?检查与用户名的参数是否存在于URL,如:www.myexamplewebsite.com/author/danieltulp it is a shorthand if/else statement

如果URL有用户名密码会尝试与get_user_by('slug', $author_name)

得到它,如果它不将尝试与get_userdata Wordpress功能得到它get_userdata(intval($author))

0123当然

,你是不是指的在URL中的用户,那么你只需要设置currentauth这样的:

$curauth = (is_home()) ? "liamhodnett" : (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); 

编辑2 如果一切都失败(没有测试上面的代码,所以很可能是可能的),使自己的数据库调用与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); 

然后使用$ mypost阵列为你的厕所p:

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();?> 
+0

感谢您的回复@DaniëlTulp但是重要的是这些特定的帖子只显示在主索引上,还有其他建议吗?再次感谢 –

+0

如果(is_home()){$ curauth = ...} http://codex.wordpress.org/Function_Reference/is_home –

+0

Hi @DaniëlTulp我已经编辑了上面的循环示例,但是我仍然可以看到所有作者的帖子在主页上,而不仅仅是一个作者,在这种情况下,'liamhodnett',我是否设置了正确的变量你认为?再次感谢 –

相关问题