Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlessandro Maculotti
4,954 PointsWp query pagination and categories
Hi there, I'm new to Wp and I'm trying to developing my first theme. After I add the paginations to my archive page the category filter doesn't work anymore (probably because I override the WP Query).
So this is my archive page: archive.php
<?php
/**
* The template for displaying the archive page (categories)
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package hicetnunc
*/
get_header(); ?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php get_template_part( 'template-parts/blogpost-list' ); ?>
<!-- TODO pagination -->
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();
Here my filter-category.php
<nav id="cat-filter" class="bg-dark nav justify-content-center">
<ul class="nav">
<?php //Filter
$categories = get_categories(array(
'orderby' => 'name',
'parent' => 0,
'order' => 'ASC'
));
foreach($categories as $category):
// Check the current category
$cats = get_categories($categories);
if($cat != $category->term_id) {
$activeClass = '';
}
else{
$activeClass = 'active';
}
?>
<li class="nav-item">
<a href="<?php echo get_category_link($category->term_id); ?>" class="nav-link u-white <?php echo $activeClass; ?>"><?php echo $category->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
</nav>
blogpost-list.php
*This is the problem if I move the query from there the category works but If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...*
blogpost-list.php
<?php
/*QUERY*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( have_posts() ) : the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
What's the best practices? I'd like to have both, how I cannot override the query?
Thanks in advance Alessandro