admin管理员组文章数量:1419185
I have a crashing jQuery slider that i have been asked to fix. Currently it is loading 128 slides (causing crashed of course), but we only want to see 6.
I tried using jQuery to remove()
7th and higher li
elements, but the slider still loaded empty slides for those removed. I think the best solution is in the template code anyway.
These slides are pulled from posts who have a checkbox created from Advanced Custom Fields named "Featured Item". Asking or advising the client to uncheck the posts when they don't want them displayed is not an option.
Can you help me change the code to only pull the 6 most recent? Also, i would love to also have a script to uncheck that box right now for items 7 and above.
Thank you!
/*
* Template name: Blog Landing
*/
global $faar_opt, $wp_query;
$acf_fields = get_fields();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$categories = get_field('categories_of_blog', $post->id);
// pretty sure this is what i need to modify, as it has 'featured_item' (the checkbox)
$args = array(
'post_type' => 'post',
'posts_per_page' => 8,
'category__in' => $categories,
'meta_query' => array(
array(
'key' => 'featured_item',
'value' => '1'
)
),
);
$related_posts = new WP_Query($args);
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'paged' => $paged,
'orderby' => 'date',
'category__in' => $categories
);
if(isset($wp_query->query_vars['blog_tag'])){
$args['tag'] = $wp_query->query_vars['blog_tag'];
$current_tag = get_term_by('slug', $wp_query->query_vars['blog_tag'], 'post_tag');
}
if(isset($wp_query->query_vars['blog_author'])){
$current_author = get_user_by('login', urldecode($wp_query->query_vars['blog_author']));
$args['author'] = $current_author->ID;
}
$all_posts = new WP_Query($args);
// each li is a slide, which is pulling 128, and needs to only be most recent 6
<ul class="slides blog-slides">
<?php
while($related_posts->have_posts()): $related_posts->the_post();
$author = get_user_by('id', $post->post_author);
?>
<li>
// slide content (removed for simplicity. Tell me if you need it.
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
I have a crashing jQuery slider that i have been asked to fix. Currently it is loading 128 slides (causing crashed of course), but we only want to see 6.
I tried using jQuery to remove()
7th and higher li
elements, but the slider still loaded empty slides for those removed. I think the best solution is in the template code anyway.
These slides are pulled from posts who have a checkbox created from Advanced Custom Fields named "Featured Item". Asking or advising the client to uncheck the posts when they don't want them displayed is not an option.
Can you help me change the code to only pull the 6 most recent? Also, i would love to also have a script to uncheck that box right now for items 7 and above.
Thank you!
/*
* Template name: Blog Landing
*/
global $faar_opt, $wp_query;
$acf_fields = get_fields();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$categories = get_field('categories_of_blog', $post->id);
// pretty sure this is what i need to modify, as it has 'featured_item' (the checkbox)
$args = array(
'post_type' => 'post',
'posts_per_page' => 8,
'category__in' => $categories,
'meta_query' => array(
array(
'key' => 'featured_item',
'value' => '1'
)
),
);
$related_posts = new WP_Query($args);
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'paged' => $paged,
'orderby' => 'date',
'category__in' => $categories
);
if(isset($wp_query->query_vars['blog_tag'])){
$args['tag'] = $wp_query->query_vars['blog_tag'];
$current_tag = get_term_by('slug', $wp_query->query_vars['blog_tag'], 'post_tag');
}
if(isset($wp_query->query_vars['blog_author'])){
$current_author = get_user_by('login', urldecode($wp_query->query_vars['blog_author']));
$args['author'] = $current_author->ID;
}
$all_posts = new WP_Query($args);
// each li is a slide, which is pulling 128, and needs to only be most recent 6
<ul class="slides blog-slides">
<?php
while($related_posts->have_posts()): $related_posts->the_post();
$author = get_user_by('id', $post->post_author);
?>
<li>
// slide content (removed for simplicity. Tell me if you need it.
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
Share
Improve this question
asked Jul 22, 2019 at 18:19
Ben BlueBen Blue
1171 silver badge8 bronze badges
4
|
1 Answer
Reset to default 0After i posted the question, i was told the staging site which i was working on, and the production site's code was out of sync.
The answer ended up being simple:
'posts_per_page' => 8,
(which was actually working but was out of sync with repos / server)
was actually set to 'posts_per_page' => -1
, which i changed to 'posts_per_page' => 6,
and that did it!
And for pulling the most recent, i didn't realize it does by default, which i found that answer here.
本文标签: phpHow can i limit the number of posts to the most recent 6 in my query
版权声明:本文标题:php - How can i limit the number of posts to the most recent 6 in my query? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304497a2652564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$related_posts = new WP_Query($args);
line, set'posts_per_page' => 6,
and beloweul
tag replace all$related_posts->
to$all_posts->
. – nmr Commented Jul 22, 2019 at 18:29'orderby' => 'date'
), from newer to older. – nmr Commented Jul 22, 2019 at 19:55