Bojana Šekeljić
Bojana Šekeljić

Reputation: 1056

is_page() is not executed

I am making infinite scroll page. I created a page template where posts of certain category are loaded. Everything works fine with a single page and single category.

But I have three pages with this page template and each page should load articles from a specific category. I am using the is_page() if elseif block to determine on which page visitor is. But is_page() is not executed.

Here is the loop:

$cat = '';
if(is_page(703)){
    $cat = 4;
} elseif (is_page(706)) {
    $cat = 21;
}
    $args = array(
        'cat' => $cat,
        'paged' => $paged
        );
    $infinite_news_query = new WP_Query($args);
    if ( $infinite_news_query -> have_posts() ) : while ( $infinite_news_query -> have_posts() ) : $infinite_news_query -> the_post(); 
 <?php endwhile; ?>

 <?php else : ?>
 <?php endif; ?>
 <?php wp_reset_postdata(); 
?>

This code displays all the posts, regardless of category, and $cat is empty inside the loop.

What am I doing wrong?

Thanks!

Upvotes: 0

Views: 396

Answers (2)

danyo
danyo

Reputation: 5856

Change the following:

$cat = '';
if(is_page(703)){
    $cat = 4;
} elseif (is_page(706)) {
    $cat = 21;
}

to:

if(is_page('703')){
    $cat = 4;
} else { if(is_page('706')) {
    $cat = 21;
}

Also removing $cat = '';

Upvotes: 1

Ivan Han&#225;k
Ivan Han&#225;k

Reputation: 2306

Look at it like this, at first you have the variable $cat set to empty value, respectively, to false , zero or 0.

Then you set a loop, with only two states, leaving out a default value. So, what is going to happen, when your page ID is changed?

You get all the posts displayed.

In other words, I do not see anything bad in the code. I would rather recommend you to use is_page('slug') than with id.

Upvotes: 0

Related Questions