2014-03-07 26 views
1

在我的博客存档页面上,如果我点击一个月,它会带我到一个页面,显示我当月创建的所有帖子(显然)。有没有过滤该页面的方法,以便它只显示我的某个类别的帖子?仅显示一个类别的月度存档(Wordpress)

archive.php

<?php if (have_posts()) : ?> 

    <div class="rightColumn"> 
     <?php 
       while (have_posts()) : the_post(); 
        get_template_part('content', get_post_format()); 
       endwhile; 
        // Previous/next page navigation. 
        twentyfourteen_paging_nav(); 
       else : 
        get_template_part('content', 'none'); 
       endif; 
      ?> 
    </div> 

<?php 
get_footer(); 

感谢。

回答

2

我最终找到了适用于我的解决方案:

function only_show_blog_posts($query) { 
    // Only modify the main loop query 
    // on the front end 
    if ($query->is_main_query() && ! is_admin()) { 
     // Only modify date-based archives 
     if (is_date()) { 
     // Only display posts from category ID 1 
     $query->set('cat', '12'); 
     } 
    } 
} 
add_action('pre_get_posts', 'only_show_blog_posts'); 
1

尝试使用pre_get_posts钩,沿着线的东西:

function filter_by_category($query) { 
    if ($query->is_archive() && $query->is_main_query() && basename($_SERVER['PHP_SELF']) == 'archive.php') { 
     $category_id = get_cat_ID('THE_CATEGORY_NAME'); //change to the actual name of the category you are filtering with 
     $query->set('cat', $category_id); 
    } 
} 
add_action('pre_get_posts', 'filter_by_category'); 

你可以将这些代码到你的functions.php文件

您可以找到有关pre_get_posts钩更多信息here

+0

这段代码的ONY问题它,做它的网站宽,当我去的网页我的其他类别没有职位出现。有没有一种方法只针对Archive.php页面? – Chris

+1

您可以尝试在if语句中添加'&& basename($ _SERVER ['PHP_SELF'])=='archive.php'' – zoranc

+0

我应该在哪里添加该代码? – Chris

0

那个简单的钩子对我也有帮助。我修改的功能一点从$ _GET获得类别ID:

function only_show_blog_posts($query) { 
    if ($query->is_main_query() && ! is_admin()) { 
     $catId = (int)$_GET['catId']; 
     if (is_date() && is_int($catId)) 
     $query->set('cat', $catId); 
    } 
} 
相关问题