属于子类别的某些帖子未显示在此 if 语句中
Some posts belonging to sub-categories are not showing in this if statement
我设法让这段代码半正常工作(它正确地过滤了帖子)但是它只显示了我正在测试的类别的 4 个可用帖子中的 2 个("England")。
实时站点: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(); ?>
in_category 上的 wordpress codex 条目将类别参数描述为:
(int|string|array) (Required) Category ID, name or slug, or array of said.
您没有提供类别结构 and/or post 类别是如何设置的,所以我假设类别本身是在任何 Post 上设置的。 (如果你提供这些,为什么会发生这种情况会更清楚)
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。
编辑:您也可以通过 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;
?>
我设法让这段代码半正常工作(它正确地过滤了帖子)但是它只显示了我正在测试的类别的 4 个可用帖子中的 2 个("England")。
实时站点: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(); ?>
in_category 上的 wordpress codex 条目将类别参数描述为:
(int|string|array) (Required) Category ID, name or slug, or array of said.
您没有提供类别结构 and/or post 类别是如何设置的,所以我假设类别本身是在任何 Post 上设置的。 (如果你提供这些,为什么会发生这种情况会更清楚)
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。
编辑:您也可以通过 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;
?>