Wordpress single post content class customization with if-else function I have the following code, which displays the content for my custom page template &quot;landingpage.php&quot; and for single posts. The site is based on Bootstrap 4.
&lt;?php if(is_page_template( 'landingpage.php' )): ?&gt;

    &lt;div id=&quot;content&quot; class=&quot;site-content&quot;&gt;
        &lt;div class=&quot;container-fluid&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;

        &lt;?php elseif (is_single()): ?&gt;

    &lt;div id=&quot;content&quot; class=&quot;site-content pt-4&quot;&gt;
        &lt;div class=&quot;container-fluid&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;

        &lt;?php else: ?&gt;

    &lt;div id=&quot;content&quot; class=&quot;site-content&quot;&gt;
        &lt;div class=&quot;container&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;
&lt;?php endif; ?&gt;

Is there a more elegant and/or shorter way to achieve this?
The third &lt;div&gt; appears to be the same in all three cases, so that can be abstracted out of the cases.
The outermost &lt;div&gt; appears to have class pt-4 applied only if is_page_template( 'landingpage.php' ) returns a value that evaluates to FALSE and is_single() returns a value that evaluates to TRUE.
And the first nested (i.e. second) &lt;div&gt; has class container-fluid if either of those functions return a value that evaluates to TRUE.
This logic could be rewritten as below. I know it strays away from the familiar wordpress style of using the Alternative syntax for control structures and uses the shortcut syntax (i.e. &lt;?=) of echo but it does add some separation of the logic from the markup
&lt;?php
// default class names
$contentClass = 'site-content';
$containerClass = 'container-fluid';
if (!is_page_template( 'landingpage.php' )) {
    if (is_single()) {
        $contentClass .= ' pt-4';
    }
    else {
        $containerClass = 'container';
    }
}
?&gt;
    &lt;div id=&quot;content&quot; class=&quot;&lt;?= $contentClass; ?&gt;&quot;&gt;
        &lt;div class=&quot;&lt;?= $containerClass; ?&gt;&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;

It could be simplified using ternary operators but some may argue that would detract from readability.

Is this shorter?
yes, only by two lines

is this more elegant?
Well, that is subjective. The code above should be simpler to read. Consider the scenario where the code is updated by multiple people, including programmers and designers. The programmers can have designers update the markup without affecting the business logic. For more on this topic, refer to Mixing PHP and HTML.