2011-09-11 21 views
0

author.phpのWordPressのテンプレートを編集して、特定のカテゴリからの投稿のみを投稿者に表示しようとしています。これまでは、カテゴリを取得するquery_posts関数を試してきましたが、作成者ではありません。私がそれを行う方法によっては、これまで投稿が表示されていないか、そのカテゴリのすべての投稿が著者に関係なく表示されます。1人の投稿者と同じページの1つのカテゴリの投稿を動的に表示しますか?

これは私がwordpress.orgの管理者によって引用されたのに適したコードですが、私にとってはうまくいかず、他の例も見つかりません。それがなぜ機能しないのでしょうか?あなたの助けを前もってありがとう。

も試み
//Gets author info to display on page and for use in query 
<?php 
    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); 
?> 

//Queries by category and author and starts the loop 
<?php 
    query_posts('category_name=blog&author=$curauth->ID;'); 
    if (have_posts()) : while (have_posts()) : the_post(); 
?> 

    //HTML for each post 

<?php endwhile; else: ?> 
    <?php echo "<p>". $curauth->display_name ."hasn't written any articles yet.</p>"; ?> 
<?php endif; ?> 

============ ============

<?php 
    new WP_Query(array('category_name' => 'blog', 'author' => $curauth->ID)); 
?> 

しかし、これはそれがない、どちらか動作しません。カテゴリー別ではなく、著者による投稿をフィルタリングしてください!私は間違って何をしていますか?

ありがとうございます!

答えて

2

このタスクはpre_get_postsフィルタを使用して実行できます。このようにして、カテゴリ以外にも投稿者を除外することもできます。

// functions.php  
    add_action('pre_get_posts', 'wpcf_filter_author_posts'); 
    function wpcf_filter_author_posts($query){ 
    // We're not on admin panel and this is the main query 
    if (!is_admin() && $query->is_main_query()) { 
     // We're displaying an author post lists 
     // Here you can set also a specific author by id or slug 
     if($query->is_author()){ 
     // Here only the category ID or IDs from which retrieve the posts 
     $query->set('category__in', array (2));   
     } 
    } 
    } 
1

このように私はちょうど、ループが始まった後、私はif(in_category('blog'))のチェックを使用して解決この同じ問題を、持っていた:もちろん

if (have_posts()) : while (have_posts()) : the_post(); 
if(in_category('blog')) { 
?> 

    <!-- Loop HTML --> 

<?php } endwhile; else: ?> 
    <p><?php _e('No posts by this author.'); ?></p> 

<?php endif; ?> 

$のcurauthチェックはこの前に来ます。

関連する問題