简体   繁体   English

我如何只显示特定类别的帖子?

[英]How do i only display posts with a certain category?

So I'm building a website for my new business.所以我正在为我的新业务建立一个网站。

An I want to only run the following code if the post have a category of 'certain-category'.如果帖子的类别为“特定类别”,我只想运行以下代码。

How do I do this?我该怎么做呢? I tried a number of things.我尝试了很多东西。 but they do not work..``但它们不起作用..``

<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>

    <?php if ( has_post_thumbnail() ) : ?>
            <div class="entry-thumb">
                <a href="<?php the_permalink(); ?>" class="H1-posts"  title="<?php the_title(); ?>" >
                    <?php the_post_thumbnail('moesia-thumb'); ?>
                </a>
            </div>
        <?php endif; ?>


        <?php if (has_post_thumbnail()) : ?>
            <?php $has_thumb = ""; ?>
        <?php else : ?>
            <?php $has_thumb = ""; ?>
        <?php endif; ?>

        <div class="post-content <?php echo $has_thumb; ?>">
            <header class="entry-header">
                <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

                <?php if ( 'post' == get_post_type() ) : ?>
                
                <?php endif; ?>
            </header><!-- .entry-header -->

            <div class="entry-summary">
                <?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
                    <?php the_content(); ?>
                <?php else : ?>
                    <?php the_excerpt(); ?>
                <?php endif; ?>
            </div><!-- .entry-content -->

Use has_category()使用has_category()

if(has_category('certain-category', get_the_ID())):
    // Do something
endif;

WordPress now has a native block which does this for you if you want the easy route the block is called Post and Page Grid it allows you to select what category of posts or pages it will show and you can select what information is shown eg Title, thumbnail, excerpt etc WordPress 现在有一个本机块,如果您想要简单的路线,它会为您执行此操作,该块称为 Post 和 Page Grid 它允许您 select 它将显示什么类别的帖子或页面,您可以 select 显示什么信息,例如标题,缩略图、摘录等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM