user295006
user295006

Reputation: 3

how to determine is_home in wordpress if query a category?

I need to query a category in home page. in index.php file I used this script

$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));

Then in the header.php file I need to change the title

<title>

<?php



if ( is_home() ) {

    echo 'My site name' ;

} elseif (is_404()) {

    echo '404 Not Found';

} elseif (is_category()) {

    echo ' Category' . wp_title('',0).' | My site name' ;

}
?>

The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.

How I can fix it? Thanks you!

Upvotes: 0

Views: 821

Answers (1)

Aurimas
Aurimas

Reputation: 2493

I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.

What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.

The code should look like this:

$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

Upvotes: 2

Related Questions