2

I am having a custom query that calls in pages in an array like so:

<?php
$args = array(
    'post__in' => array( 2, 7, 9, 11, 13 , 15, 17 ),
    'post_type' => 'page',
    'orderby' => 'menu_order',
    'order' => 'ASC'
);
$the_query = new WP_Query( $args ); 
while ( $the_query->have_posts() ) : $the_query->the_post();
?>

Is it still possible to target single pages of the array to have page specific alterations of the code, i.e.

<?php if(is_page(7)) { ?>
    // do something
<?php } else { ?>
    // do something else
<?php } ?>

inside this query or do I have to query every page in a a separate query?

I am using this query to put together multiple pages into one template (front-page.php) in order to have a onepaged layout, i.e. all content on one page, so I would like to use conditions inside the loop for clarity in my code.

4

1 Answer 1

4

You would rather want to use the post ID inside the loop to target your pages. is_page() simply checks whether the current page is actually a page or a specific page if a value is passed

Example

if ( $post->ID === 7 ) {
    // Do something for page id 7
} else {
    // Do something for other pages
}
9
  • Why not use if ( is_single( '7' ) ) { Commented Jun 16, 2015 at 16:37
  • My solution relates to use of code in a custom function. Not sure he specifically wants a solution which is added to his custom loop code. Commented Jun 16, 2015 at 17:19
  • I yes, I completely missthought the use of conditional tags for pages which I use so often - of course I cannot use it here, since the actual page I am on is clearly some other page (front-page.php in my case), so it does not work through my conditions. However, the example you provided causes a 500 error on my testing site ($post->ID === 7), even though it seems like it is right to me..
    – physalis
    Commented Jun 16, 2015 at 17:22
  • What happens when you do var_dump( $post->ID ); inside the loop. Cannot see why you get a 500 error Commented Jun 16, 2015 at 17:30
  • 1
    It was a missing (), no more than that - thanks again & until next time :).
    – physalis
    Commented Jun 16, 2015 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.