Lorraine
Lorraine

Reputation: 486

Wordpress: Get category from url in index php

I modified the index.php file to display pagination, but now does not work properly when I click on any category, how I can get the category and add it to the query?

<?php get_header(); ?>
<section class="post-list box-siz rounded">
<?php
$temp = $wp_query;
//$wp_query = null;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$tag = ( get_query_var( 'tag' ) ) ? get_query_var( 'tag' ) : null;
$args = array(
    'posts_per_page' => 5,
    'paged' => $paged,
    'tag' => $tag,
    'orderby' => 'date',
    'order' => 'DESC',
    ); 
$wp_query = new WP_Query($args);
?>
<?php if ($wp_query->have_posts()) :  while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

Upvotes: 1

Views: 1357

Answers (1)

Michal S
Michal S

Reputation: 1584

Try to add this to your code:

$thisCat = get_query_var('category_name');
$args = array(
 'posts_per_page' => 5,
 'paged'          => $paged,
 'tag'            => $tag,
 'orderby'        => 'date',
 'order'          => 'DESC',
 'category_name'  => $thisCat
); 

keep in mind, category_name is a slug not a name.

Upvotes: 1

Related Questions