Dirty Bird Design Dirty Bird Design - 2 months ago 7
PHP Question

How to combine two if statements in PHP for WP

I need to run a function (show a modal) if a page DOES have a particular field value AND is not a certain page. It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it is not the certain page. They work individually like this:

//if page has field value:

<?php
$values = get_field( 'recast_video' );
if ( ($values) ) {
get_template_part('template-parts/popIn', 'none');
}
?>

//if it is this page, do not show:

<?php
if ( (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>


How do I combine the two so that it shows (get_template_part) if the field has the value and is not the specified page

//this fails

<?php
$values = get_field( 'recast_video' );
if ( ($values) || (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>

Answer

First thing to do is look for the common condition. No matter what, you don't want it to be a certain page, right? So first check that it's not that page:

$result = (!is_page('trading-education-webinars')) ? : ;

It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it is not the certain page.

Then why check for the field value at all? No need, as long as it's not the page we checked for.

$result = (!is_page('trading-education-webinars')) ? get_template_part('template-parts/popIn', 'none') : return false;

You can change return false; to whatever you want to happen if the page IS 'trading-education-webinars'

EDIT: Clarifying the conditional:

  • Page X never gets served "content" (modal)
  • If NOT page X and has $values, show content
  • If NOT page X and NOT has $values, show some other content

$values = get_field( 'recast_video' );

$x = get_template_part('template-parts/popIn', 'none');

$y = 'some other content';

$return = (!is_page('trading-education-webinars')) ? (($values) ? $x; : $y ) : return false );
Comments