Wordpress 如何自定义/更改循环查询
我们有一个分为两列的页面。每列都有一个循环,该循环使用来自不同类别的帖子填充该列 我们想要的是: A列-显示类别为X和Y的帖子 B列–显示类别为X的帖子(也有类别为Y的帖子除外) 问题是: 我们不能从B栏中排除X类和Y类的帖子——我们的想法是在B栏中只显示X类帖子 根据我们的研究,我们假设我们必须定制B列中的循环,但我们不知道如何访问它Wordpress 如何自定义/更改循环查询,wordpress,loops,filter,Wordpress,Loops,Filter,我们有一个分为两列的页面。每列都有一个循环,该循环使用来自不同类别的帖子填充该列 我们想要的是: A列-显示类别为X和Y的帖子 B列–显示类别为X的帖子(也有类别为Y的帖子除外) 问题是: 我们不能从B栏中排除X类和Y类的帖子——我们的想法是在B栏中只显示X类帖子 根据我们的研究,我们假设我们必须定制B列中的循环,但我们不知道如何访问它 我们这里缺少什么?这很容易理解。定义新的自定义查询(自定义循环)时,引用了大量反对它的参数 在您的情况下,我们对类别参数感兴趣 类别参数 显示与特定类别关联的帖
我们这里缺少什么?这很容易理解。定义新的自定义查询(自定义循环)时,引用了大量反对它的参数 在您的情况下,我们对类别参数感兴趣 类别参数 显示与特定类别关联的帖子
- cat(int)-使用类别id
- 类别名称(字符串)–使用类别段塞
- 类别和(数组)–使用类别id
- 类别(数组)-使用类别id
- 类别不在(数组)–使用类别id
- 来源@
category\uu和
参数中的category\uu和not\u。我们将使用get\u category\u by\u slug
通过category slug获取category id
这是我们的最终结果。我已经添加了注释,您可以添加更多的参数来查看完整列表。修改并享受乐趣
<?php
// Column A
$x = get_category_by_slug( 'category_x_slug' )->term_id; // ... get category x ID
$y = get_category_by_slug( 'category_y_slug' )->term_id; // ... get category y ID
$args = [
'post_type' => 'post'
'posts_per_page' => 6,
'category__in' => [ $x, $y, ], // ... Column A: post with category x, category y
];
$query = new WP_Query( $args ); // ... loop start
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post();
// ... post template start
the_title( '<h1>', '</h1>' );
the_excerpt();
// ... post template end
endwhile;
else:
echo "No posts yet!";
endif; // ... loop end
wp_reset_postdata(); ?>
<?php
// Column B
$x = get_category_by_slug( 'category_x_slug' )->term_id;
$y = get_category_by_slug( 'category_y_slug' )->term_id;
$args = [
'post_type' => 'post'
'posts_per_page' => 6,
'category__in' => [ $x, ], // ... Column B: post with category x
'category__not_in' => [ $y, ], // ... Without post with category y
];
$query = new WP_Query( $args ); // ... loop start
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post();
// ... post template start
the_title( '<h1>', '</h1>' );
the_excerpt();
// ... post template end
endwhile;
else:
echo "No posts yet!";
endif; // ... loop end
wp_reset_postdata(); ?>
您使用的主题是什么?如何添加这些列?你试过什么?您是否尝试过任何自定义查询?非常感谢@AmarineDialog的回复。我能理解代码的作用。非常清楚(感谢您提供的详细信息和评论)。但据我所知,我们正在创造一个新的页面。我打算编辑现有的查询(由WordPress生成)。我不明白在哪里插入代码来编辑WordPress已经创建的列。是否只有创建一个新页面才有可能?我可以编辑现有的吗?我在Wordpress方面没有太多经验:(请帮忙:)