admin管理员组

文章数量:1419583

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!

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!

Share Improve this question edited Jul 17, 2019 at 19:11 Jeffrey asked Jul 17, 2019 at 15:22 JeffreyJeffrey 235 bronze badges 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? – Jacob Peattie Commented 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 Commented Jul 17, 2019 at 17:16
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: phpCan I change a variable in a content part while calling it