I am using a content part for title sections on various sites.
I would still like to be able to change the title manually on each page. For this particular case I can‘t use dynamic functions like the_title(). Since the title is the only thing I am changing, I still would like to pull in the content part.
My content part file looks like this:
<?php
$hssHeading = "A Title";
?>
<section class="heroSectionSmall">
<div class="sectionIntro">
<h1><?php echo $hssHeading ?></h1>
<div class="sectionIntro__underline"></div>
</div>
</section>
When I’m calling the content part I am trying to accomplish something like this:
include( locate_template( 'cp/heroSectionSmall.php', false, false ) );
$hssHeading = "A new different Title"
Can I call a content part and change a value for a variable inside the content part?
Thanks a lot!
Currently, you have your variable defined below the include which would be too late. If you defined the variable above the include it should be accessible by whatever file is included.
$hssHeading = "A new different Title";
include( locate_template( 'cp/heroSectionSmall.php', false, false ) );
<!-- heroSectionSmall.php -->
echo $hssHeading;
Maybe a better solution though is to simple create a function or call an action to output this heading which will allow you to modify it with conditionals or hooks:
<h1><?php the_hssHeading( $hssHeading ); ?></h1>
<!-- functions.php -->
/**
* Display HSS Heading
*
* @param String $current_heading
*
* @return void
*/
if( ! function_exists( 'the_hssHeading' ) ) {
function the_hssHeading( $current_heading ) {
echo $current_heading . ' | Foobar';
}
}
Or via filter hook:
echo apply_filters( 'theme_hss_heading', $hssHeading );
<!-- functions.php -->
/**
* Modify the HSS Heading
*
* @param String $current_heading
*
* @return void
*/
function hssheading_modifications( $current_heading ) {
return sprintf( '<h1 class="%1$s">%2$s</h1>', 'foobar', $current_heading );
}
By using a filter hook or function you’ll open yourself up to customizing this section all in one place. Additionally you allow child themes to modify or override this functionality pretty easily.