2

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!

2
  • Sorry, I'm not clear from your question. Do you want to pass a value for $hssHeading into heroSectionSmall.php or do you want to define $hssHeading inside heroSectionSmall.php and then be able to access it from outside? Jul 17, 2019 at 15:31
  • Sorry for not beeing more clear. When I'm calling the include function I would like to give a parameter which changes the $hssHeading so I can manually change the content of the h1. Am I on the wrong path here? I am trying to use content parts to avoid redundancies. The section is almost on every page but I need to edit the title manually on each page. Can you follow?
    – Jeffrey
    Jul 17, 2019 at 17:16

1 Answer 1

0

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.

2
  • Perfect! The first changes worked fine. But since you recommended to work with an action I am trying to make this happen. Would I have to write the conditional logic for every page inside the function? Would this a better solution than declaring it on the actual page? Thanks! function the_hssHeading( $current_heading ) { if (is_page( 'Page Title' ) ): $current_heading = "Site 1"; else(is_page( 'Page Title 2' ) ); $current_heading = "Site 1"; endif; echo $current_heading; }
    – Jeffrey
    Jul 18, 2019 at 6:01
  • It may be worth it to just look into creating a custom meta box ( Or using ACF, Metabox IO, or something similar ) which will allow the user to set this subtitle. Then you can grab it as post meta in the function: global $post; $title = get_post_meta( $post->ID, '_subtitle_meta', true );
    – Howdy_McGee
    Jul 18, 2019 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.