I have the standard blog post format and gallery format. I have excluded all the gallery posts from the index.php page so only standard posts show up there. However, the gallery format posts are still being counted for pagination and there is just a blank space on the pagination pages where there would be a gallery post format. How can those be excluded from pagination?
<?php echo paginate_links(); ?>
I tried this but it didn't help:
<?php
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
) ,
'operator' => 'NOT IN',
) ,
)
);
?>
<?php echo paginate_links($args); ?>
This is the query that displays the standard blog posts:
<?php if (have_posts() ): ?>
<?php while (have_posts() ): the_post(); ?>
<?php $format = get_post_format( $post_id ); ?>
<?php if($format != 'gallery'): ?>
<div class="blog" style="width: 350px"><a href="<?php esc_url(the_permalink()); ?>" class="w-inline-block"><?php the_post_thumbnail(); ?></a>
<div class="gallery-info"><a href="<?php esc_url(the_permalink()); ?>" class="linkgallery"><?php the_title(); ?></a></div>
<?php $the_category = get_the_category(); ?>
<div class="gallery-info gallery-category"><?php foreach($the_category as $category): ?><?php echo $category->cat_name . ' '; ?>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have the standard blog post format and gallery format. I have excluded all the gallery posts from the index.php page so only standard posts show up there. However, the gallery format posts are still being counted for pagination and there is just a blank space on the pagination pages where there would be a gallery post format. How can those be excluded from pagination?
<?php echo paginate_links(); ?>
I tried this but it didn't help:
<?php
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
) ,
'operator' => 'NOT IN',
) ,
)
);
?>
<?php echo paginate_links($args); ?>
This is the query that displays the standard blog posts:
<?php if (have_posts() ): ?>
<?php while (have_posts() ): the_post(); ?>
<?php $format = get_post_format( $post_id ); ?>
<?php if($format != 'gallery'): ?>
<div class="blog" style="width: 350px"><a href="<?php esc_url(the_permalink()); ?>" class="w-inline-block"><?php the_post_thumbnail(); ?></a>
<div class="gallery-info"><a href="<?php esc_url(the_permalink()); ?>" class="linkgallery"><?php the_title(); ?></a></div>
<?php $the_category = get_the_category(); ?>
<div class="gallery-info gallery-category"><?php foreach($the_category as $category): ?><?php echo $category->cat_name . ' '; ?>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Share
Improve this question
edited Apr 29, 2019 at 8:58
user10980228
asked Apr 29, 2019 at 8:41
user10980228user10980228
1691 silver badge14 bronze badges
4
|
2 Answers
Reset to default 0You are getting into a bit of a mess by simply skipping posts in the loop if they are not the correct format. You have a couple of options.
Firstly, you can perform the filter using a hook in your theme functions.php
. Have a look at pre_get_posts, which allows you to alter the query and remove these gallery posts. Then, in your template you can use the loop and pagination as normal. You can use a check for a specific page or template when applying this hook.
Second option is to call query_posts() in your template or create a new WP_Query to override the main query. You will need to use get_query_var( 'paged')
to get the current pagination, for example:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
),
'operator' => 'NOT IN',
)
)
);
$query = new WP_Query($args);
?>
<?php if ($query->have_posts()): ?>
<?php while ($query->have_posts()): ?>
<?php $query->the_post(); ?>
// Loop
<?php endwhile; ?>
<?php echo paginate_links(array(
'total' => $query->max_num_pages,
'current' => $paged
)); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Un-tested code but that should get you started.
As @SallyCJ mentioned in the commentary, you can change the query by filter pre_get_posts. There is no need to perform another WP_Query
.
add_action('pre_get_posts', 'se336587_exclude_gallery_format');
function se336587_exclude_gallery_format( $query )
{
if ( $query->is_main_query() /* here is additional condition (see below) */ )
{
$query->query_vars['tax_query'][] = [
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => ['post-format-gallery'],
'operator' => 'NOT IN',
];
}
}
IF additional condition
&& is_home()
- the filter will be applied on the homepage,&& is_category()
- the posts on the category page will be filtered,&& (is_home() || is_category())
- in both of the above-mentioned places.
paginate_links()
may not be using the query where you've applied this: "I have excluded all the gallery posts from the index.php". So if it's the main query, you could trywp_reset_query(); echo paginate_links();
. And in the updated question, that args is for theWP_Query
class and not thepaginate_links
function. – Sally CJ Commented Apr 29, 2019 at 8:56wp_reset_query()
but still have same result<?php wp_reset_postdata(); echo paginate_links(); ?>
– user10980228 Commented Apr 29, 2019 at 9:06if($format != 'gallery')
to visually exclude the posts. You should use thepre_get_posts
hook, or create a new WordPress query ($query = new WP_Query(...);
) to exclude the posts and have the correct pagination output. – Sally CJ Commented Apr 29, 2019 at 9:09