0

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?

6
  • 2
    You cannot have a post type called gallery. Commented Dec 16, 2015 at 17:39
  • @PieterGoosen I'm finding a lot of other users are registering custom post types called 'gallery', and they're working. Is there a source behind this? Commented Dec 16, 2015 at 17:50
  • 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 can Commented Dec 16, 2015 at 17:52
  • is_post_type_archive is true on a post type archive- you have a page named student-gallery. you also can't target a custom query with is_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?
    – Milo
    Commented Dec 16, 2015 at 18:01
  • @PieterGoosen I'm re-creating a design for a site that uses gallery as the post type and I need to use this just to transfer over the old data. Commented Dec 16, 2015 at 18:01

1 Answer 1

0

user5477769 reports in the comments of the original post, that the problem has been solved:

I was following Custom Post Type UI instructions but it seems it led me astray. Creating a archive-gallery.php with a vanilla Loop, along with removing is_main_query has solved all problems, and the pages now show with 9 posts and correct pagination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.