簡體   English   中英

使用過濾器覆蓋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 } ?>

總的來說,我真正想要添加/更新的全部是以下部分:

<div class="rellax">
                    <?php if ( has_post_thumbnail() ): ?>
                        <?php the_post_thumbnail(); ?>
                    <?php else : ?>
                        <img src="<?php echo esc_url( $headerImage ); ?>">
                    <?php endif; ?>
                </div>

我知道對於這么小的事情來說似乎有些矯kill過正,但是我不想直接在父主題中修改文件,以防萬一文件有更新,並且炸掉了我添加的所有更改。

同樣,不確定是否可行或最佳方法。

這是其中包含的全部功能:

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檢查中時,該函數將存在。 然后只需根據您的需要更改孩子的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM