【问题标题】:Some posts belonging to sub-categories are not showing in this if statement属于子类别的某些帖子未显示在此 if 语句中
【发布时间】:2019-11-14 12:46:24
【问题描述】:

我设法让这段代码正常工作(它可以正确过滤帖子),但是它只显示了我正在测试的类别(“英格兰”)的 4 个可用帖子中的 2 个。

现场直播:https://medievalbritain.com/category/type/medieval-castles/

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

我的if 有问题吗?一个页面中显示的帖子数量是否有限制?这可能是一个主题限制,有没有办法覆盖它?

这是我的代码:

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

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

注意二:我在 Elementor 中使用 Futurio 主题。我创建的代码上面是这样的:

<?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 categories


    【解决方案1】:

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

    (int|string|array) (必填)Category ID, name or slug, or array of said.

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

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

    一旦 in_category 函数具有 post 参数,它将确保您检查帖子而不是其他任何内容。

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

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

    编辑:您也可以通过 WP_Query 查询数据库,然后只抓取类别中的那些,而不是事后过滤它们:

    <?php
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => 'England',
        'posts_per_page' => -1,
    );
    $posts = new WP_Query($args);
    
    if ($posts->have_posts()) :
        while ($posts->have_posts()) :
            $posts->the_post();?>
            <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
        endwhile;
    endif;
    ?>
    
    

    【讨论】:

    • 谢谢。我试着把它改成这个,我仍然只得到 2 个结果(和以前一样)。结构是:一侧是类型(城堡/城镇),另一侧是位置(苏格兰/英格兰/威尔士/北爱尔兰)。帖子属于每个帖子。在这种情况下,我只想显示来自英格兰的城堡。
    • @Yisela 我在我的答案上添加了一个编辑,它利用了 QP_Query。这样您就可以查询给定类别中的帖子,而不是通过它们进行过滤。
    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2019-10-25
    • 1970-01-01
    • 2011-11-13
    • 2017-07-28
    相关资源
    最近更新 更多