0

I'm trying to figure out if it's possible to have a custom field within a conditional statement.
e.g—

if ( is_page( 'about' ) || '29' == $post->post_parent ) {
                //custom field goes here
                }

elseif ( is_page( 'contact' ) || '30' == $post->post_parent ) {
                //custom field goes here
                }

and the custom field to go within:

<?php while(has_sub_field('site_slideshows','option')): ?> 

<div class="slideshow-slide" style="background-image:url(<?php the_sub_field('site_slideshows_slide_image'); ?>)">
</div>

<?php endwhile; ?>

Is it at all possible to put a custom field within a conditional tag? Each custom field will be different obviously, that's just an example of what i want to go custom field goes here.
Any suggestions would be greatly appreciated.

  • I don't see why that shouldn't work. Have you tried it? Does it not work for you, what happens? – jzatt Jun 26 '13 at 14:31
  • @jzatt well replacing the custom field goes here with the custom field code I've given above brings up a syntax error, unexpected > etc etc. I'm not sure if I have to change the format of it, I'm not overly familiar with conditional tags? Any suggestions as to how that could be written? – user1374796 Jun 26 '13 at 14:35
  • @jzatt well replacing the custom field goes here with the custom field code I've given above brings up a syntax error, unexpected > etc etc. I'm not sure if I have to change the format of it, I'm not overly familiar with conditional tags? Any suggestions as to how that could be written? – user1374796 Jun 26 '13 at 14:36
0

If you pasted the exact second block you'll get errors because you are breaking up the closing and opening PHP tags <?php and ?>. Try this instead, or have a look at this for a better understanding, notice the difference ...

if ( is_page( 'about' ) || '29' == $post->post_parent ) {
    //Start of the custom code...
    while(has_sub_field('site_slideshows','option')):
    ?> <!-- Closing the PHP tag -->

        <div class="slideshow-slide" style="background-image:url(<?php the_sub_field('site_slideshows_slide_image'); ?>)">
        </div>

    <?php //Opening the PHP tag again
    endwhile;

} elseif ( is_page( 'contact' ) || '30' == $post->post_parent ) {
    //Start of the custom code...
    while(has_sub_field('site_slideshows','option')):
    ?> <!-- Closing the PHP tag -->

        <div class="slideshow-slide" style="background-image:url(<?php the_sub_field('site_slideshows_slide_image'); ?>)">
        </div>

    <?php //Opening the PHP tag again
    endwhile;
}
| improve this answer | |

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