我想将我的主页帖子偏移5.我在顶部有一个小部件,显示前5个。
我在functions.php中使用它来获取偏移量。它的工作原理。问题是小部件不再从头开始打印帖子了。以下所有代码
add_action('pre_get_posts', 'myprefix_query_offset', 1);
function myprefix_query_offset(&$query) {
//Before anything else, make sure this is the right query...
if (!$query->is_home()) {
return;
}
//First, define your desired offset...
$offset = 5;
//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');
//Next, detect and handle pagination...
if ($query->is_paged) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars['paged'] - 1) * $ppp );
//Apply adjust page offset
$query->set('offset', $page_offset);
} else {
//This is the first page. Just use the offset...
$query->set('offset', $offset);
}
}
小部件使用它,当它发生时,它会移回偏移数据集的前5个。它使用get_featured_post
变量。
$get_featured_posts = new WP_Query(array(
'posts_per_page' => 5,
'post_type' => 'post',
'ignore_sticky_posts' => true
)
);
任何帮助将不胜感激 感谢。
答案 0 :(得分:0)
我认为代码中的问题是它不适用于正确的查询。
add_action('pre_get_posts', 'myprefix_query_offset', 1);
function myprefix_query_offset(&$query) {
//Before anything else, make sure this is the right query...
if (!in_the_loop () || !is_main_query ()) {
return;
}
//First, define your desired offset...
$offset = 5;
//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');
//Next, detect and handle pagination...
if ($query->is_paged) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars['paged'] - 1) * $ppp );
//Apply adjust page offset
$query->set('offset', $page_offset);
} else {
//This is the first page. Just use the offset...
$query->set('offset', $offset);
}
}
你可以尝试上面的代码,让我知道这是否有效。
由于