我一直在为 WordPress 类别存档页面开发 AJAX“加载更多”功能,虽然客户端只需要 Yoast SEO 插件设置的“主要类别”,但这个功能略有不同。
我让它在页面加载时正常工作,但是当用户单击“加载更多”按钮时,页面会加载所有类别的帖子,而不仅仅是主要类别。它还加载尚未分配到页面类别的帖子。
我在functions.php中使用这个函数来获取类别ID:
//get cat ID
function getCurrentCatID(){
global $wp_query;
if(is_category()){
$cat_ID = get_query_var('cat');
}
return $cat_ID;
}
$current_category_ID
在 category.php 中使用这个找到变量:
$current_category_ID = getCurrentCatID();
我在 category.php 上使用以下内容来获取页面加载的前 10 个帖子:
$args = array(
'meta_key' => '_yoast_wpseo_primary_category',
'meta_value' => $current_category_ID,
'posts_per_page' => 10
);
$query = new WP_Query($args);
//output loop
在functions.php中,然后我使用以下内容将新循环输出到页面,问题是我似乎无法再次将类别ID作为变量获取。
// prepare our arguments for the query
$args = unserialize( stripslashes( $_POST['query'] ) );
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
$args['post_status'] = 'publish';
$args['meta_key'] = '_yoast_wpseo_primary_category';
$args['meta_value'] = $cat_ID; //requires variable to be dynamic, hardcoding it works fine
$args['posts_per_page'] = 10;
query_posts( $args );
// new loop
$_POST['query']
我在第二个函数中使用include$cat_ID = $_POST['query']; $cat_ID = $_GET['page_id'];
或尝试了各种方法(太多无法回忆)$cat_ID = $_GET['page_id'];
。
当我return
将变量设置为 functions.php 时,我总是得到值0
。
对此的任何建议将不胜感激。
谢谢。