I'm trying to define values for certain meta tags on pages of my website, however, I don't necessarily want to define them on every single page.
<?php if ($_SERVER['SCRIPT_NAME'] != '/index.html'): ?>
<meta name="description" content="<?php echo $page_info; ?>"/> }
<?php endif; ?>
As you can see, I have that set to parse on every page but index.html, however, I also want to create the following condition:
Will only parse if $page_info is defined, if not it will not parse anything at all in the php if.
Are you perhaps looking for the isset() function?
<?php if ( isset( $page_info ) && $php_info ): ?>
<meta name="description" content="<?php echo $page_info; ?>"/> }
<?php endif; ?>
Check out the docs for some more info on isset().
isset— Determine if a variable is set and is not NULL
The conditional expression I'm using here checks to see that a variable called $page_info exists and that it is not a "falsy" value. A falsy value is anything that, when converted to a boolean could be considered a false value such as (taken from the docs):
One other suggestion I can make is to not explicitly test for the page you want to exclude. Rather build an array of all the pages you want to exclude and then use an in_array() call to detect if it is in the list:
$exclude_url = array(
'/index.html',
'/about.html'
'/faq.html'
);
if ( !in_array( $_SERVER[ 'SCRIPT_NAME' ], $exclude_url ) ){
// page should NOT be excluded!
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With