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 |1 Answer
Reset to default 0Currently, 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
版权声明:本文标题:php - Can I change a variable in a content part while calling it? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745314199a2653073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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