elementor自定义查询
· 阅读需 3 分钟
要在Elementor中查询最近一个月的帖子,可以使用你已经设置的Query ID,并在相应的代码中定义查询逻辑。以下是如何实现这一目标的详细步骤:
信息
原来如此简单!elementor的自定义查询,等于把“elementor可视化”与WordPress代码结合起来了。
1. 添加自定义查询逻辑
将以下代码添加到你的WordPress主题的functions.php文件,或者通过插件添加:
add_action( 'elementor/query/sedexthismonth', function( $query ) {
// 获取当前日期
$current_date = date( 'Y-m-d' );
// 获取一个月前的日期
$date_one_month_ago = date( 'Y-m-d', strtotime( '-1 month' ) );
// 设置查询参数
$query->set( 'post_type', 'post' );
$query->set( 'posts_per_page', 10 ); // 每页显示10篇文章,可以根据需要调整
$query->set( 'date_query', array(
array(
'after' => $date_one_month_ago,
'before' => $current_date,
'inclusive' => true,
),
));
});