I have a custom post type of gallery
, and I'm using a template for the archive of page-student-gallery.php
. My admin settings for posts shown is 5, I'd like it to show 9 on this page which only contains gallery items.
I've been able to manage the query where it can show EITHER 9 posts OR 5 posts with pagination at the bottom, which leads to a 404 error. I've been checking out code for doing this, but everyone seems to have a different way of going about fixing the problem, and none of the solutions are working for me.
My code for the archive page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query(array(
'post_type' => 'gallery',
'paged' => $paged
//'posts_per_page' => 9
));
while( $query->have_posts() ) : $query->the_post(); ?>
<article class="oneThird gallery">...</article>
<?php endwhile; ?>
<ul class="pcount">
<?php wp_pagenavi();
wp_reset_postdata(); ?>
</ul>
My attempts at using pre_get_posts
:
add_action('pre_get_posts', 'show_more_posts');
function show_more_posts($query){
if($query->is_post_type_archive('gallery') && !is_admin() && $query->is_main_query() )
$query->set('post_type', 'gallery');
$query->set('posts_per_page', '9');
};
I'm really confused at this point. What is the best way to implement this kind of setup?
gallery
is used internally by WordPress. I do not say that this definitely your issue, but naming custom objects the same as internal objects can cause issues. So avoid it where you canis_post_type_archive
is true on a post type archive- you have a page namedstudent-gallery
. you also can't target a custom query withis_main_query
, it's not the main query. is there a reason you're using a page with an extra query instead of the built in post type archive?