我想显示与Projektyętrz类别的所有帖子。我使用这个查询来实现这一点,但是它只显示了很少的帖子(我添加了8,它只显示了6。我想总是显示每个帖子)。
我对每一个类别都有这个问题,它总是只显示部分的帖子。
(预先谢谢:)
<div id="menu1" class="tab-pane fade">
<div class="container">
<div class="row">
<?php $args = array( 'post_type' => 'Realizacje'); ?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
$num = $loop->post_count;
$cat_id = get_cat_ID( 'Projekty wnętrz' );
if (has_category( $cat_id )) {
?>
<div class="col-md-4">
<div class="realisation">
<!-- <div class="gallery">
<?php
$images = get_field('galeria');
$size = 'medium';
if( $images ): ?>
<div id="lightgallery<?php echo $count; ?>">
<?php foreach( $images as $image ): ?>
<div class="gal">
<img src="<?php echo $image; ?>"/>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div> -->
<div class="galeria">
<div class="lightg" id="lightgallery<?php echo $count; ?>">
<a href="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="relative-box"><img class="first_image"src="<?php echo get_the_post_thumbnail_url(); ?>"/>
<div class="demo-gallery-poster">
<i class="fa fa-search" aria-hidden="true"></i>
</div></div>
<div class="gallery-title"><h5 class="gallery-t"><?php the_title(); ?></h5></div>
</a>
<?php
$count=$count+1;
$images = get_field('galeria');
$size = 'large';
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image; ?>"><img src="<?php echo $image; ?>"/></a>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php } ?>
<?php endwhile; ?>
<?php else: ?>
<h5>Brak realizacji</h5>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
</div>发布于 2022-10-14 04:24:31
请检查下面的完整代码。我测试过了,效果很好。
<div id="menu1" class="tab-pane fade">
<div class="container">
<div class="row">
<?php $args = array(
'post_type' => 'Realizacje',
'posts_per_page' => -1,
'cat' => $cat_id //add category id if you want filter the post by category othrwise remove
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
$num = $loop->post_count;
?>
<div class="col-md-4">
<div class="realisation">
<div class="galeria">
<div class="lightg" id="lightgallery<?php echo $count; ?>">
<a href="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="relative-box"><img class="first_image"src="<?php echo get_the_post_thumbnail_url(); ?>"/>
<div class="demo-gallery-poster">
<i class="fa fa-search" aria-hidden="true"></i>
</div></div>
<div class="gallery-title"><h5 class="gallery-t"><?php the_title(); ?></h5></div>
</a>
<?php
$count=$count+1;
$images = get_field('galeria');
$size = 'large';
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image; ?>"><img src="<?php echo $image; ?>"/></a>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endwhile;
else: ?>
<h5>Brak realizacji</h5>
<?php endif;
wp_reset_postdata(); ?>
</div>
</div>发布于 2022-10-13 22:13:08
您没有使用任何分页参数,因此根据每个页面值设置在后端设置的帖子数量,您可能只从查询中得到10-12个帖子。
在此基础上,您使用has_category( $cat_id )来排除post内容,不应该这样做,应该在类别参数中传递类别ID /片段,不应该使用名称,您必须使用类别ID或片段。
因为您需要类别中的所有帖子,所以需要将posts_per_page设置为-1,-1将显示所有带有查询中使用的筛选器的帖子。
在应用上述更改后,代码将如下所示:
<?php
$args = array(
'post_type' => 'Realizacje',
'posts_per_page' => -1,
'cat' => 101, // You need to change 101 with the category ID that you want to use.
);
/**
* Note: If you want to use category slug then you will
* have to use the 'category_name' param instead of using
* 'cat' param and have to use category slug, not the name.
* If you have to use multiple categories then you'll have
* to use the 'category__in' param instead of using the 'cat' param
* 'category__in' will accept an array of category ids.
*/
$loop = new WP_Query( $args );
?>https://stackoverflow.com/questions/74062021
复制相似问题