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.

  • Do you want to target specific posts by modifying/adding code to your custom loop code or you're happy to use a custom function? – Brad Dalton Jun 16 '15 at 17:20
  • I use that query to combine the content of multiple pages into one (front-page.php), so I can have a onepaged layout. Thus for clarity I would like to use conditions inside my template file (front-page.php) rather than catering conditions inside the functions.php :). – physalis Jun 16 '15 at 17:24
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
}
  • Why not use if ( is_single( '7' ) ) { – Brad Dalton Jun 16 '15 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. – Brad Dalton Jun 16 '15 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 Jun 16 '15 at 17:22
  • What happens when you do var_dump( $post->ID ); inside the loop. Cannot see why you get a 500 error – Pieter Goosen Jun 16 '15 at 17:30
  • 1
    It was a missing (), no more than that - thanks again & until next time :). – physalis Jun 16 '15 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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