Jump to content

Exclude per id from include?


kaz
 Share

Recommended Posts

I have an address in the global settings which is displayed on all pages in the sidebar.

<?php if ($global->businesshours): ?>
    <?php include('businesshours.php'); ?>
<?php endif; ?>

The client asked me to hide the address on only one page, id 1127. I can have an element displayed on a single site via a query, if ($page->id == 1042 ), but can I also exclude a page in an include?

Link to comment
Share on other sites

Hey @kaz you can simply check for the opposite:

<?php if ($global->businesshours && $page->id !== 1127): ?>
    <?php include('businesshours.php'); ?>
<?php endif; ?>

Which means if we have businesshours AND the page id is not 1127 then include ...

Note that there is a difference between !== and != (and also === and ==)

// Using ==
$var1 = "5";
$var2 = 5;

if ($var1 == $var2) {
    echo "Equal"; // This will output "Equal" because values are the same after type juggling.
}

// Using ===
if ($var1 === $var2) {
    echo "Identical";
} else {
    echo "Not identical"; // This will output "Not identical" because types are different (string vs integer).
}

 

  • Like 1
Link to comment
Share on other sites

@bernhard Thanks that works great. And also thanks for the training. I am quietly learning from a book by Christian Wenz and Tobias Hauser, but some parts only become clear when I use :)

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Hello @kaz,

Conditional Check within the Included File (businesshours.php).
try to Modify businesshours.php to check if the current page ID should display the address.

<?php
if (!isset($global->businesshours) || $page->id != 1127) {
    // Display the business hours content
    echo $global->businesshours;
}
?>

Use an if statement before the include to check the page ID

<?php
if ($page->id != 1127) {
    include('businesshours.php');
}
?>

If you have multiple places where you want to conditionally include or exclude the business hours based on the page ID, consider modifying businesshours.php for a reusable solution.
If this is a one-time case for page 1127, using a conditional include might be simpler.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...