2009-10-04 163 views
3

我是一個非常新的WordPress,但已花了大約50個小時研究它,嘗試了一些事情,並有這樣的感覺,我現在有一個相當不錯的處理..WordPress:如何只顯示某個類別的帖子?

但是,有一件事我根本無法工作就是讓頁面吐出某個類別的帖子列表。

這是我的例子:http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters

我有一個帖子,如果我去給它直接工作正常,但此頁面上顯示不出來。

The post direct link.

類別ID爲 '3',而類別名稱爲 '印刷,海報'。

我對印刷,海報頁,看起來像這樣的自定義頁面模板:

<?php 
/* 
Template Name: Typographic Posters 
*/ 
?> 

<?php get_header(); ?> 
<?php get_sidebar(); ?> 

<?php if (in_category('3')): ?> 
<div class="post"> 

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 


    <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
    <div class="post-description"> 
    <h2><?php the_title(); ?></h2> 
    <?php the_content(); ?> 
    </div> 
    <?=get_image('flutter-image');?> 
    </div> 


    <?php endwhile; else: ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
    <?php endif; ?> 

</div> 
<?php endif; ?> 

<?php get_footer(); ?> 

使用此代碼然而在網頁只能說明得到頭,側邊欄,並沒有別的..

如果有人能幫助我,那真的會幫助我處理對wordpress類別的過濾。

感謝您的閱讀,

Jannis

回答

11

in_category只能在單個頁面的循環外部工作。我建議使用query_posts函數來解決這個問題。您可以使用query_posts('cat=3')query_posts('category_name=typographic-posters')獲取您要查找的帖子。

一旦獲得,只需使用普通的WordPress循環來訪問這些帖子。

+0

謝謝,這很好地工作。不能相信我沒有設法找到我自己:)再次感謝。 – Jannis 2009-10-04 07:20:00

1

最簡單的方法是創建一個名爲category-3.php文件,並從正常index.phpcategory.php文件使用標準的代碼。 Wordpress將負責僅從id = 3的類別獲取帖子,並且它是子類別。

0

在循環之前只需添加:

<?php query_posts="cat=3&showposts=5"> 

這將強制循環來從3類顯示5個(showposts = 5)(貓= 3)。

0

我會第二Eimantas的建議。 Template Hierarchy將使用category-3.php來顯示該類別中的帖子。通常,您可以將主題的index.php或category.php複製到category-3.php,然後根據需要調整該模板以進行任何自定義。此外,類別模板將更好地支持帖子的分頁。

但是如果你需要堅持使用一個頁面來顯示那些帖子,也看到了Page of Posts example.

+0

這確實看起來像一個很好的方式去,雖然我不完全確定,如果我想頁面slug成爲domain.com/category-3.php或這是可定製的東西? – Jannis 2009-10-07 20:16:38

+0

使用默認的固定鏈接,url將是http:// youdomain /?cat = 8 – Michael 2009-10-09 14:37:51

-1

謝謝分享您的想法的一個偉大的想法。通常,您可以將主題的index.php或category.php複製到category-3.php並針對您需要的任何自定義調整該模板

1

in_category只能在單個頁面的循環外部工作。 I 建議使用query_posts函數來解決這個問題。您可以使用query_posts('cat = 3')或 query_posts('category_name = typographic-posters')來獲取 正在尋找的帖子。

一旦獲得,只需使用普通的WordPress循環來訪問這些 的帖子。

這個工作出色的,但要確保你進入設置>閱讀和帖子頁面設置爲 - 選擇 - 選項,否則它將覆蓋此查詢和轉儲所有最新的帖子出現,無論類別。

1

我曾嘗試使用下面的方法過濾通過分類標識後:

   query_posts('cat=1&showposts=3'); 
       if (have_posts()) : while (have_posts()) : 

       // if(1) { 
        //echo the_category_ID(); 
       the_post(); 
       /** 
       * The default post formatting from the post.php template file will be used. 
       * If you want to customize the post formatting for your homepage: 
       * 
       * - Create a new file: post-homepage.php 
       * - Copy/Paste the content of post.php to post-homepage.php 
       * - Edit and customize the post-homepage.php file for your needs. 
       * 
       * Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part 
       */ 

       $is_post_wrap++; 
        if($is_post_wrap == '1') { 
         ?><div class="post-wrap clearfix"><?php 
        } 
        get_template_part('post', 'homepage'); 

        if($is_post_wrap == '3') { 
         $is_post_wrap = 0; 
         ?></div><?php 
        } 



      endwhile; 

      else : 
       get_template_part('post', 'noresults'); 
      endif; 
相關問題