属于子类别的某些帖子未在此if语句中显示

问题描述 投票:0回答:1

我设法使此代码正常运行(它正确过滤了帖子),但是只显示了我正在测试的类别(“英格兰”)的4个帖子中的2个。

实时站点:https://medievalbritain.com/category/type/medieval-castles/

英格兰的分类页面,其中显示了我所缺少的其他城堡:https://medievalbritain.com/category/locations/england/

if我做错什么了吗?页面中显示多少个帖子的limit?这可能是主题限制,有没有办法覆盖它?

这是我的代码:

<h3 class="archive-post-title">Castles from England</h3>

<!-- start content container for England -->
<div class="row">

    <?php
        // The Loop 
      if ( have_posts() ) : while ( have_posts() ) :
        the_post();
        if (in_category('England')):
    ?>

    <div class="archive-post-box col-sm-2 col-md-3 col-lg-4">
          <div>
            <?php the_post_thumbnail(); ?>
          </div>
          <h2>
              <a href="<?php the_permalink() ?>" rel="bookmark">
                <?php the_title(); ?>
              </a>
          </h2>
          <p class="archive-post-excerpt">
              <?php the_excerpt(); ?>
          </p>
    </div>

    <?php
          endif;
        endwhile;
      endif;
    ?>

</div>
<!-- end content container for England-->

注意我:我不是开发人员。

注二:我将主题Futurio与Elementor一起使用。在我创建的代码上方是:

<?php get_header(); ?>

<?php futurio_generate_header( true, true, true, false, false ); ?>

    <div class="container-fluid category-custom-header">
        <header class="container text-left">
         <h1>Medieval Castles</h1>
     <?php
     // Display optional category description
        if ( category_description() ) : ?>
       <div class="archive-meta"><?php echo category_description(); ?></div>
     <?php endif; ?>
        </header><!-- .archive-page-header -->
    </div>


<?php futurio_breadcrumbs(); ?>

<?php futurio_content_layout(); ?>
php wordpress
1个回答
0
投票

in_category上的wordpress Codex条目将类别参数描述为a:

((int | string | array)(必填)类别ID,名称或子句或表示的数组。

您未提供类别结构和/或帖子类别的设置方式,所以我只是假设类别本身已设置在任何帖子上。 (如果您提供了这些,将会更清楚为什么会发生这种情况)

in_category函数还可以接收一个post参数,该参数应该是当前的Post ID(可以通过Wordpress循环内的get_the_ID()函数检索该参数。

一旦in_category函数具有post参数,它将确保您对post进行检查,仅此而已。

    <?php
      // The Loop 
      if ( have_posts() ) : while ( have_posts() ) :
        the_post();
        if (in_category('England', get_the_ID())):
    ?>

如果不确定类别名称,您还可以提供类别的ID。

© www.soinside.com 2019 - 2024. All rights reserved.