简体   繁体   中英

Wordpress Category Filter not working

I am completely not sure if I am just being blind but not 100% used to wordpress as yet and would love some assistance with this.

I have my blog posts page showing all the blog posts it also has the categories in the sidebar but when a category is selected it changes the URI with the /categories/events but does not limit the posts content to what should be in the category?

 <?php get_header(); ?> <div class="container posts"> <div class="row"> <div class="col-xs-8 col-md-8"> <?php $args = array ( 'post-type' => 'post' ); $post_query = new WP_Query($args); if ($post_query->have_posts()) while ($post_query->have_posts()) { $post_query->the_post(); ?> <div class="post"> <?php the_post_thumbnail( 'large' ); echo '<a href="' . get_permalink() . '">'; the_title('<h2>', '</h2>'); echo '</a>'; the_excerpt(); echo do_shortcode( "[icon name='fa-calendar-o']" ) . " "; the_date("d F"); ?> </div> <?php } ?> </div> <div class="col-xs-4 col-md-4"> <?php get_sidebar(); ?> </div> </div> </div> 

Any ideas? This is a full custom WP Theme from start to finish so I may be missing something very simple and if someone knows what that is I would greatly appreciate it.

Before loading your index.php page, WordPress will have set up a WP_Query object for you according to the page URL/parameters. So on the main blog posts page, it will be a query that finds all posts; on the Foo category page it'll be a query that finds all posts in category Foo.

In your existing code, you're ignoring that pre-set-up query object and creating a new one, $post_query , that always just grabs all posts. That's why the results aren't changing when viewing a Category.

You should dump your specially-created $post_query object and use the basic WordPress " The Loop " code instead:

    if (have_posts())
      while (have_posts()) {
        the_post();
        ...

This will use the WP_Query object that's been set up for you by WordPress. Typically you don't create your own WP_Query object unless you're doing something a little out of the ordinary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM