Php 用过滤器覆盖WordPress父主题中的部分
不确定这是否太可能,但我正试图覆盖父主题的template_tags.php文件中的一小部分。我试图将同一个文件复制到我的子主题,但它似乎没有覆盖该文件。所以我想知道我是否需要使用一个过滤器来改变它,但我不知道如何去做 这就是我想要改变的:Php 用过滤器覆盖WordPress父主题中的部分,php,wordpress,hook,add-filter,Php,Wordpress,Hook,Add Filter,不确定这是否太可能,但我正试图覆盖父主题的template_tags.php文件中的一小部分。我试图将同一个文件复制到我的子主题,但它似乎没有覆盖该文件。所以我想知道我是否需要使用一个过滤器来改变它,但我不知道如何去做 这就是我想要改变的: <?php if(has_header_image()){?> <?php $headerImage = get_header_image();
<?php if(has_header_image()){?>
<?php
$headerImage = get_header_image();
if(class_exists('woocommerce')){
if( is_woocommerce() || is_product() || is_cart() || is_shop() ){
$thumbId = get_post_thumbnail_id();
$thumbImage = wp_get_attachment_url( $thumbId );
}
if(!empty($thumbImage)){
$headerImage = $thumbImage;
}
}
?>
<div class="rellax">
<img src="<?php echo esc_url( $headerImage ); ?>">
</div>
<?php } ?>
对这样的事情:
<?php if(has_header_image()){?>
<?php
$headerImage = get_header_image();
if(class_exists('woocommerce')){
if( is_woocommerce() || is_product() || is_cart() || is_shop() ){
$thumbId = get_post_thumbnail_id();
$thumbImage = wp_get_attachment_url( $thumbId );
}
if(!empty($thumbImage)){
$headerImage = $thumbImage;
}
}
?>
<div class="rellax">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo esc_url( $headerImage ); ?>">
<?php endif; ?>
</div>
<?php } ?>
总的来说,我真正想添加/更新的是这一部分:
<div class="rellax">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo esc_url( $headerImage ); ?>">
<?php endif; ?>
</div>
我知道对于这样一件小事来说,这似乎有些过分,但我不想直接在父主题中修改该文件,以防该文件发生更新,并破坏我添加的任何更改
同样,我也不确定这是否可能或者最好的方法是什么
下面是它的完整功能:
if( !function_exists('business_idea_breadcrumbs')){
function business_idea_breadcrumbs(){
$business_idea_option = wp_parse_args( get_option( 'business_idea_option', array() ), business_idea_default_data() );
?>
<section class="sub-header">
<?php if(has_header_image()){?>
<?php
$headerImage = get_header_image();
if(class_exists('woocommerce')){
if( is_woocommerce() || is_product() || is_cart() || is_shop() ){
$thumbId = get_post_thumbnail_id();
$thumbImage = wp_get_attachment_url( $thumbId );
}
if(!empty($thumbImage)){
$headerImage = $thumbImage;
}
}
?>
<div class="rellax">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo esc_url( $headerImage ); ?>">
<?php endif; ?>
</div>
<?php } ?>
<?php if(has_header_image()){?>
<div class="sub-header-overlay">
<?php } ?>
<div class="container sub-header-container">
<div class="row">
<div class="col-md-12 text-center">
<?php
if( function_exists('bcn_display') ){
bcn_display();
}else if( is_front_page() && is_home() ){
echo '<h1 class="page_title">' . __('Home','business-idea') . '</h1>';
}else if( is_archive() ){
the_archive_title( '<h1 class="page_title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
}else{
?>
<h1 class="page_title"><?php single_post_title(); ?></h1>
<?php
}
?>
</div>
</div>
</div>
<?php if(has_header_image()){?>
</div>
<?php } ?>
</section>
<?php
}
}
如我在评论中所述,只有在原始代码中针对您正试图更改的内容有相应的apply_filter调用时,应用过滤器才会起作用。 在这种情况下,您必须将整个函数复制到子主题中并删除
if( !function_exists('business_idea_breadcrumbs')){
加上最后一个括号
这样,当父函数运行到函数_exists check时,函数将存在。
然后根据您的需要更改子函数。您可以发布主题中的完整函数定义吗?是不是有点像“如果”!函数\u以前是否存在“函数名”?由于没有apply_filter调用,您不能简单地过滤输出,您必须在调用它的模板中重写该函数,或者通过再次定义它来重写该函数。@user127091-Ahhh,是的。好的,我更新了我的帖子。谢谢好的,酷。看起来这很有效。是的,这是一件我从来没有真正确定的事情,那就是在父/子主题关系中工作时,到底在哪里更改和更新内容。谢谢