In WordPress, the PHP code itself generally doesn't have direct awareness of frontend HTML section IDs like #sd-blurbs-2 or #sd-blurbs-3 because PHP runs server-side before HTML is sent to the browser. In other words, PHP generates the HTML output, but doesn't "know" about specific sections once the page is rendered.
However, you can control what text appears inside each section by placing your PHP code inside those sections in your theme templates (or in widgets, page builders, or wherever your HTML for those sections is defined).
How to approach this:
You want:
- Section
#sd-blurbs-2: show "How It Works"
- Section
#sd-blurbs-3: show "Why Choose Us?"
If you have control over the theme/template files, for example something like this:
<div id="sd-blurbs-2">
<?php if ( is_page('pickup-and-delivery') ) { ?>
<h2 class="display-5 cmg-text-dark mb-3 fw-bold">How It Works</h2>
<?php } ?>
</div>
<div id="sd-blurbs-3">
<?php if ( is_page('pickup-and-delivery') ) { ?>
<h2 class="display-5 cmg-text-dark mb-3 fw-bold">Why Choose Us?</h2>
<?php } ?>
</div>
This is the simplest way since you are outputting the content explicitly inside the sections where you want the text.
What if you have only one PHP snippet and want it to output differently based on the section?
If you want to reuse one PHP snippet and have it detect the section where your code is printed, that's not straightforward in PHP because PHP does not have access to the IDs of the sections unless you make your PHP code aware of the context.
Possible solutions:
- Output the sections differently in PHP in your template
Modify your template so the section wraps the PHP and knows what text to insert:
<div id="sd-blurbs-2">
<?php if ( is_page('pickup-and-delivery') ) { ?>
<h2 class="display-5 cmg-text-dark mb-3 fw-bold">How It Works</h2>
<?php } ?>
</div>
<div id="sd-blurbs-3">
<?php if ( is_page('pickup-and-delivery') ) { ?>
<h2 class="display-5 cmg-text-dark mb-3 fw-bold">Why Choose Us?</h2>
<?php } ?>
</div>
- Pass a variable or parameter to your snippet
If the snippet is included multiple times (like via get_template_part(), shortcode, widget, or block), you can pass a parameter to decide what to show.
Example with a shortcode:
function display_section_title($atts) {
$atts = shortcode_atts(array(
'section' => ''
), $atts, 'section_title');
if (!is_page('pickup-and-delivery')) return '';
if ($atts['section'] === 'sd-blurbs-2') {
return '<h2 class="display-5 cmg-text-dark mb-3 fw-bold">How It Works</h2>';
} elseif ($atts['section'] === 'sd-blurbs-3') {
return '<h2 class="display-5 cmg-text-dark mb-3 fw-bold">Why Choose Us?</h2>';
}
return '';
}
add_shortcode('section_title', 'display_section_title');
Then in your content (or template), place:
<div id="sd-blurbs-2">
<?php echo do_shortcode('[section_title section="sd-blurbs-2"]'); ?>
</div>
<div id="sd-blurbs-3">
<?php echo do_shortcode('[section_title section="sd-blurbs-3"]'); ?>
</div>
- Use JavaScript to replace the text after page load (less preferred because it's better to generate final HTML server-side)
Summary
- PHP runs BEFORE page loads, so it can't detect the HTML wrappers (#ids).
- You should control code placement inside those wrappers and put the conditional text in each section.
- Passing parameters (via shortcodes, template parts, widgets) is a clean way to reuse code with different outputs.
- If you're editing theme files, simply place the code inside those divs.
If you want, I can help tailor the PHP code based on your theme setup or how you're embedding your code!