2013-07-04 47 views
0

我想要一个作者在页面的帖子列表。我用作者的帖子在每个页面都是动态的。我使用下面的代码。WordPress的帖子按作者查询

<ul> 
<?php 
global $post; 
$args = array('posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => 'ppm'); 
$myposts = get_posts($args); 
foreach($myposts as $post) : setup_postdata($post); ?> 

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 

<?php endforeach; ?> 
</ul> 

请看看author_name数组,我希望后期显示ppm用户。现在我要做的是动态的。我想从页面的自定义字段获取作者姓名。像

<?php $author_name = get_post_meta($post->ID, 'author_name', true); ?> 
<ul> 
<?php 
global $post; 
$args = array('posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => '$author_name'); 
$myposts = get_posts($args); 
foreach($myposts as $post) : setup_postdata($post); ?> 

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 

<?php endforeach; ?> 
</ul> 

但是,代码不起作用。我如何从页面的自定义字段获取作者姓名?谁能帮忙?

对不起英语不好。

+0

您无法在循环外获得'$ post-> ID'。 – bboy

+0

看到代码http://wordpress.stackexchange.com/questions/36135/how-can-i-list-posts-by-author谢谢, –

+0

我在循环内尝试,仍然无法正常工作。 –

回答

1

这对我有用。感谢萨里姆汗为这个解决方案。

<ul> 
<?php 
global $post; 
$author_name = get_post_meta($post->ID, 'author_name', true); 
$args = array('posts_per_page' => 3, 'post_type'=> 'post', 'author_name' => $author_name); 
$myposts = get_posts($args); 
foreach($myposts as $post) : setup_postdata($post); ?> 

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 

<?php endforeach; ?> 
</ul> 
0
<?php while (have_posts()) : the_post(); ?> 
<?php $author_name = get_post_meta($post->ID, 'author_name', true); ?> 
<?php endwhile; ?> 


<?php 
     $args = array('posts_per_page' => 3, 'post_type'=> 'post','meta_key'=>'author_name','meta_value'=>$author_name); 
     query_posts($args); 
?> 

<ul> 
<?php while (have_posts()) : the_post(); ?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endwhile; ?> 
</ul>