php WordPress:如何仅显示特定类别的帖子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1515753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:54:08  来源:igfitidea点击:

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

phpwordpresslistfiltercategories

提问by Jannis

I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..

我对 WordPress 还很陌生,但已经花了大约 50 个小时来研究它,尝试各种方法,并且感觉我现在已经很好地掌握了它。

However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.

然而,我根本无法工作的一件事是让一个页面吐出某个类别的帖子列表。

Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters

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

I have a post that if I go to it directly works correctly, but does not show up on this page.

我有一个帖子,如果我直接访问它可以正常工作,但不会显示在此页面上。

The post direct link.

帖子直接链接。

The category id is '3' while the category name is 'typographic-posters'.

类别 ID 为“3”,而类别名称为“印刷海报”。

I have a custom page template for the typographic-posters page that looks like this:

我有一个用于排版海报页面的自定义页面模板,如下所示:

<?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(); ?>

Using this code however the page only shows gets the header, sidebar and nothing else..

使用此代码但是页面只显示获取标题、侧边栏和其他任何内容..

If someone could help me out that would really help me get a handle on this filtering of wordpress categories.

如果有人可以帮助我,那真的会帮助我处理这种 wordpress 类别的过滤。

Thanks for reading,

谢谢阅读,

Jannis

詹尼斯

回答by Karthik Viswanathan

in_categorywill only work outside of the loop on a single page. I suggest using the query_postsfunction to solve this problem. You may use query_posts('cat=3')or query_posts('category_name=typographic-posters')to get the posts you are looking for.

in_category只会在单个页面上的循环之外工作。我建议使用该query_posts函数来解决这个问题。您可以使用query_posts('cat=3')query_posts('category_name=typographic-posters')来获取您正在寻找的帖子。

Once obtained, just use the normal WordPress loop to access these posts.

获得后,只需使用普通的 WordPress 循环即可访问这些帖子。

回答by Eimantas

The easiest way is to create a file called category-3.phpand use the standard code from normal index.phpor category.phpfile. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.

最简单的方法是创建一个名为的文件,category-3.php并使用 normalindex.phpcategory.phpfile.js 中的标准代码。Wordpress 将只负责从 id=3 的类别及其子类别中获取帖子。

回答by Mike

in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.

Once obtained, just use the normal WordPress loop to access these posts.

in_category 只能在单个页面上的循环之外工作。我建议使用 query_posts 函数来解决这个问题。您可以使用 query_posts('cat=3') 或 query_posts('category_name=typographic-posters') 来获取您要查找的帖子。

获得后,只需使用普通的 WordPress 循环即可访问这些帖子。

This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.

这很有效,但请确保您进入“设置”>“阅读”并将帖子页面设置为 -- Select -- 选项,否则它将覆盖此查询并转储所有最近的帖子,而不管类别如何。

回答by FAISAL

I have filtered post by category Id using the method below:

我使用以下方法按类别 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; 

回答by Tomer Lichtash

Simply add before the loop:

只需在循环前添加:

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

This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).

这将强制循环显示类别 3 (cat=3) 中的 5 个帖子 (showposts=5)。

回答by Michael

I would 2nd Eimantas' suggestion. The Template Hierarchywill use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.

我会第二个 Eimantas 的建议。该模板层次将使用该类别3.php以显示帖子在该类别中。通常,您只需将主题的 index.php 或 category.php 复制到 category-3.php 并根据您需要的任何自定义调整该模板。加上类别模板将更好地支持帖子的分页。

But if you need to stick with a Page to display those posts, also see the Page of Posts example.

但是,如果您需要坚持使用页面来显示这些帖子,请参阅帖子页面示例。

回答by Adam

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

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

Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.

只是为了让您知道这些答案的来源……您还可以使用 query_posts 执行更多有趣的功能。

回答by Damien MATHIEU

This plugin could also help you if you want to be able to change the displayed categories without going through the code : http://wordpress.org/extend/plugins/advanced-category-excluder/

如果您希望无需通过代码即可更改显示的类别,此插件也可以为您提供帮助:http: //wordpress.org/extend/plugins/advanced-category-excluder/

回答by Abacus

thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need

感谢您分享您的想法,这是一个很棒的想法。通常,您只需将主题的 index.php 或 category.php 复制到 category-3.php 并根据您需要的任何自定义调整该模板