2012-12-20 113 views
0

我正在构建一个Wordpress网站,我正在努力弄清楚如何修改taxonomy.php以显示自定义类别和自定义分类中的所有帖子,但我被卡住了。这里的情况:如何修改Taxonomy.php以显示自定义帖子类型和自定义分类标准的所有帖子?

我有一个自定义职位类型称为'工作。'在该自定义帖子类型中,我有一个名为“项目类型”的自定义分类,我创建了几个类别,分别称为摄影,视频,印刷设计等。我设置了一个名为taxonomy.php的模板,该模板管理mydomain.com/work以及mydomain .COM /项目类型/打印设计/。问题是这些页面只显示5个最近的帖子。我需要他们将所有帖子显示在适当的类别中(或者在mydomain.com/work的情况下,不管类别如何,所有帖子都是自定义帖子类型)。

我发现this page about query_posts这帮助我意识到我需要添加<?php query_posts(array ('post_type' => 'work', 'posts_per_page' => -1)); ?>。现在工作页面加载正确,但所有类别页面都加载了所有工作帖子(因为我已经将post_type定义为我添加的片段中的工作)。

我该如何概括代码,使其使工作页面显示所有工作帖子,而类别页面仅显示相关类别的帖子?下面是完整的代码:

<?php 

/** 
* Project Type Taxonomy Archive 
*/ 

$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); 
?> 

<h3 style="color:#EEEEEE"><?php echo apply_filters('the_title', $term->name); ?> PROJECTS</h3> 

<ul class="thumbnails"> 
    <div id="work"> 

    <?php if (!empty($term->description)): ?> 
     <div class="archive-description"> 
     <?php echo esc_html($term->description); ?> 
     </div> 
    <?php endif; ?> 
    <?php query_posts(array ('post_type' => 'work', 'posts_per_page' => -1)); ?> 
    <?php if (have_posts()): while (have_posts()): the_post(); ?> 

    <div class="post"> 
     <li class="span4"> 
      <div class="thumbnail"> 
      <a href="<?php the_permalink() ?>"><div class="tint"><?php the_post_thumbnail('featured-thumb'); ?></div></a> 
      <br /> 
      <div class="thumbcaption"> 
       <a href="<?php the_permalink() ?>"><h3><?php the_title() ?></h3></a> 
       <p> <?php the_excerpt(); ?></p> 
       <p><i><?php the_terms($post->ID, 'work_project_type' , ' '); ?></i></p> 
      </div> 
      </div> 
     </li> 
    </div> 

    <?php endwhile; ?> 

    <div class="navigation clearfix"> 
     <div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div> 
     <div class="alignright"><?php previous_posts_link('Next Entries »') ?></div> 
    </div> 

    <?php else: ?> 

    <h2 class="post-title">No Projects in <?php echo apply_filters('the_title', $term->name); ?></h2> 
    <div class="content clearfix"> 
     <div class="entry"> 
      <p>It seems there aren't any projects in <strong><?php echo apply_filters('the_title', $term->name); ?></strong> right now. Check back later, I try to add new projects regularly.</p> 
     </div> 
    </div> 

    <?php endif; ?> 
</div> 
</div> 

+0

任何人有任何想法?有人告诉我看看WP_Query,但我无法弄清楚如何将它用于我需要做的事情...... – mcography

回答

1

一个处理这种方法是不能添加自定义模板。

当您转到与分类法归档wordpress相关的网址时,已将所有帖子从数据库中取出。通过创建单独的模板,尽管您可以更轻松地控制像'posts_per_page'这样的查询参数,但不必进行自己的查询就可以更好。

请考虑做一个pre_get_posts过滤器。像这样的东西。

// if on a certain taxonomy archive page, do not limit the posts 
add_action('pre_get_posts', function($query){ 
    if (isset($query->query_vars['name_of_your_taxonomy'])) { 
     add_filter('post_limits', function($limit){ 
      /* 
      default will be something like this 'LIMIT 0, 10' 
      but we don't want a limit so return empty string 
      */ 
      return ''; 
     }); 
    } 
}); 
相关问题