【发布时间】:2023-03-06 11:36:02
【问题描述】:
我在循环中排除了一个类别(效果很好),现在我想在下面的这个循环中调用相同的类别。 (这样它就会显示在页面底部。)我希望一双新的眼睛能帮助我告诉我这里发生了什么。它没有显示任何东西。我正在使用“新的 WP_Query”来引入单一类别...这里是:
<?php // Begin Recent Sold loop of 3 ?>
<?php $args = new WP_Query('cat=367'); // category 367
while($args->have_posts()) : $args->the_post(); ?>
<div class="category">
<h2 class="artist-name">
<?php echo '<a href="' . get_category_link( $category->cat_ID ) . '">' . $category->name . '</a> <a class="viewall" href="' . get_category_link( $category->cat_ID ) . '">VIEW ALL</a>'; ?></h2>
<ul class="subcats">
<?php
$cats = wp_list_categories('orderby=name&title_li=&use_desc_for_title=0&depth=1&echo=0&show_count=1&child_of='.$category->cat_ID);
if (!strpos($cats,'No categories') ){
echo $cats;
}
?>
</ul>
<div class="row clearfix">
<?php $query = array(
'post_type' => 'work',
'posts_per_page' => '3',
'orderby' => 'post_title',
'order' => 'ASC',
'cat' => $category->cat_ID,
);
query_posts($query);
if ( have_posts() ) while ( have_posts() ) : the_post();
$rows = get_field('images');
if(get_the_post_thumbnail()){
echo '<div class="four column work"><a href="'.get_permalink().'">';
echo get_the_post_thumbnail($post_id, 'medium');
echo '</a></div>';
} else {
if($rows)
{
foreach($rows as $row)
{
echo '<div class="four column work"><a href="'.get_permalink().'">';
echo '<img src="'. $row['image']['sizes']['medium'] . '" class="shadowed forced" alt="'.$row['image']['alt'].'">';
echo '</a></div>';
} ?>
<?php }
}
?>
<?php endwhile; wp_reset_query(); ?>
</div><!-- .row -->
</div><!-- .category -->
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php // End Recent Sold loop ?>
【问题讨论】:
-
Don't use
query_posts()-- 你最好的选择可能是为你的第二个循环创建第二个WP_Query。 -
@PatJ 谢谢,所以在数组之后使用第二个 WP_Query 而不是 query_posts()?
-
是的,类似于
$second = new WP_Query( $query );。此外,您需要在第二个循环中使用$second->have_posts()、$second->the_post()等代替have_posts()、the_post()等。 -
感谢@PatJ - 但由于某种原因,这也不起作用。出于某种原因,整个循环都没有与该网站交谈。
-
@PatJ - 我想通了 - '367' ); $categories = get_categories( $args ); $parent_categories = ''; foreach ( $categories as $category ) { ?> 感谢您的帮助!
标签: php arrays wordpress categories args