簡體   English   中英

WordPress:如何只顯示某個類別的帖子?

[英]WordPress: How to display only posts that are in a certain category?

我是WordPress的新手,但花了大約50個小時的時間來研究它,嘗試一下這樣的事情,感覺我現在已經有了很好的處理能力。

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

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

我有一個帖子,如果我直接使用它可以正常工作,但不會顯示在此頁面上。

帖子直接鏈接。

類別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

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

獲得后,只需使用普通的WordPress循環即可訪問這些帖子。

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

獲得后,只需使用普通的WordPress循環即可訪問這些帖子。

這非常有效,但請確保您進入設置>閱讀並將帖子頁面設置為 - 選擇 - 選項,否則它將覆蓋此查詢並轉儲所有最近的帖子,無論其類別如何。

最簡單的方法是創建一個名為category-3.php的文件,並使用普通index.phpcategory.php文件中的標准代碼。 Wordpress將負責僅從id = 3的類別及其子類別中獲取帖子。

我已使用以下方法按類別ID過濾了帖子:

               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; 

只需在循環之前添加:

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

這將強制循環顯示來自類別3(cat = 3)的5個帖子(showposts = 5)。

我會提出第二個Eimantas的建議。 模板層次結構將使用category-3.php來顯示該類別中的帖子。 通常,您只需將主題的index.php或category.php復制到category-3.php,然后針對您需要的任何自定義調整該模板。 此外,類別模板將更好地支持帖子的分頁。

但是如果您需要堅持使用頁面來顯示這些帖子,請參閱帖子頁面示例。

http://codex.wordpress.org/Template_Tags/query_posts

只是讓你知道這些答案來自哪里......你可以用query_posts做更多有趣的功能。

如果您希望能夠在不通過代碼的情況下更改顯示的類別,此插件也可以幫助您: http//wordpress.org/extend/plugins/advanced-category-excluder/

謝謝你分享你的想法是一個偉大的想法。 通常,您只需將主題的index.php或category.php復制到category-3.php,並調整該模板以進行所需的任何自定義

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM