Myzwer / foothillschurch

Bootcamp II is a wordpress theme (as well as an inside joke) designed to suit the needs of foothillschurch.com. It makes use of webpack, Babel, Sass, Tailwind, Browsersync, PostCSS, ESLint, Stylelint, Prettier and more. It is meant for that site, but if you can use it by all means go for it.
1 stars 1 forks source link

Nullish check for $term and declare it once #66

Closed rain2o closed 2 weeks ago

rain2o commented 1 month ago

In the following code you don't check that $term is null before using it, like $term->term_id. PHP would throw an error if you tried to access a property of null like that.

https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/taxonomy-series.php#L41-L57

Earlier in the file, you already declare this variable in the same way, and check if it's null. https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/taxonomy-series.php#L23-L29

What I would suggest is to declare that variable once, and handle null as needed throughout the layout. Something like this for example.

...
get_header();
// Get the current taxonomy term
$term = get_queried_object();
?>

<div class="bg-salty-gradient">
    <div class="...">
        <div class="content-middle text-center">
            <h1 class="text-white text-3xl md:text-5xl font-bold uppercase">
                <?php echo $term === null ? "Unknown Series" : "Series: " . esc_html($term->name); ?>
            </h1>
        </div>
    </div>
</div>

<div class="bg-white-gradient">
    <div class="md:w-8/12 mx-auto grid grid-cols-12 p-5 gap-4">
        <?php if ($term === null): ?>
            <p>Something has gone terribly wrong, and we don't recognize this series. Etc...</p>
        <?php else:
        // WP_Query arguments
        $args = array(...);
        ...
        <?php endif; ?>
...

And while we're at it, the echo 'there are no posts.'; // no posts found isn't great... maybe make it a little more presentable for the user with a better message? This is just a UX suggestion.

Myzwer commented 2 weeks ago

Ended up killing this entire page, but that's good to know for future reference.