使用过滤器覆盖 WordPress 父主题中的部分

Override part in WordPress parent theme with filter

不确定这样做是否可行,但我正在尝试覆盖父主题的 template_tags.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">
                <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 } ?>

总的来说,我真正想要add/update的是这一部分:

<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')){

加上最后一个右括号。

这样,父函数遇到function_exists检查时,该函数就会存在。 然后根据自己的需要更改child中的函数即可。