更改子主题内的父主题函数时的最佳实践是什么?

时间:2018-06-19 作者:Mr.Brown

因此,我不能完全确定完成这项任务的最佳方式到底是什么。我尝试了一些事情,只是为了看看效果如何,但我不知道我如何完成这项任务是最合适的方式,所以我希望一些比我聪明得多的人能启发我,希望。。。

简而言之,“我是孩子”主题似乎聚合使用函数,以便为主题布局执行不同的代码块。

我需要对其进行一些小修改的代码包含在父主题内的多个函数中,我通过反复试验找到的唯一方法是复制需要从父主题更改的函数,然后通过稍微附加函数名称来重新声明它们。

这很好,但我不知道这是不是一个坏习惯,所以我想与社区再次确认一下,处理这类事情的最佳方式是什么。

所有真正被改变的是Im删除标题中标记的头像。

父主题的原始函数集:

/**
 * This function contains large post title markup.
 * It is used in \'flexia_post_large_title\' method.
 *
 * @since  v0.0.5
 */
function flexia_post_large_title_markup() {
    $thumbnail = \'\';
        if (function_exists(\'has_post_thumbnail\')) {
            if ( has_post_thumbnail() ) {
                 $thumbnail = wp_get_attachment_url( get_post_thumbnail_id() );
            }
        }
    ?>
    <header class="page-header single-blog-header" <?php if ( ! empty( $thumbnail ) ) : ?>
    style="background-image: url(\'<?php echo esc_attr($thumbnail); ?>\');" <?php endif; ?> <?php if ( empty( $thumbnail ) ) : ?>
    style="background-image: url(\'<?php echo esc_attr(get_header_image()); ?>\');" <?php endif; ?>>
        <div class="header-inner">
            <div class="header-content">
                <?php the_title( \'<h1 class="blog-title">\', \'</h1>\' ); ?>
                <?php flexia_post_large_title_author_avatar_markup(); ?>
            </div>
        </div>
        <div class="header-overlay"></div>
    </header>
    <?php
}

/**
 * This function contains large post title author avatar markup.
 * It is used in \'flexia_post_large_title_markup\' method.
 *
 * @since  v0.0.5
 */
function flexia_post_large_title_author_avatar_markup() {

    if( class_exists( \'CMB2_Bootstrap_230\' ) ) {
        global $post;
        $post_title_header_meta = get_post_meta( $post->ID, \'_flexia_post_meta_key_header_meta\', true );
        $post_title_header_post_author = get_post_meta( $post->ID, \'_flexia_post_meta_key_header_author_meta\', true );
        if( $post_title_header_meta == \'yes\' || $post_title_header_meta == NULL ) {
            if( $post_title_header_post_author == \'yes\' || $post_title_header_post_author == NULL ) {
            ?>
            <div class="blog-author">
                <div class="author-avatar">
                    <?php echo get_avatar( get_the_author_meta( \'ID\' ), \'flexia-thumbnail-avatar\' ); ?>
                    <div class="author-body">
                        <h4 class="author-heading"><?php the_author(); ?></h4>
                    </div>
                </div>
            </div>
            <?php
            }else {
                return false;
            }
        }else {
            return false;
        }
    }else {
        ?>
        <div class="blog-author">
            <div class="author-avatar">
                <?php echo get_avatar( get_the_author_meta( \'ID\' ), \'flexia-thumbnail-avatar\' ); ?>
                <div class="author-body">
                    <h4 class="author-heading"><?php the_author(); ?></h4>
                </div>
            </div>
        </div>
        <?php
    }
}
/**
 *
 */
function flexia_post_large_title_header_meta_markup() {
    ?>
    <header class="entry-header single-blog-meta single-post-meta-large">
        <?php
        if ( \'post\' === get_post_type() ) : ?>
        <div class="entry-meta">
            <?php flexia_updated_on(); ?>
        </div><!-- .entry-meta -->
        <?php
        endif; ?>
    </header><!-- .entry-header -->
    <?php
}

/**
 * This function will show/hide large post title
 *
 * @since  v0.0.5
 */
function flexia_post_large_title() {
    $flexia_single_posts_layout = get_theme_mod(\'flexia_single_posts_layout\', \'flexia_single_posts_layout_large\');
    if( class_exists( \'CMB2_Bootstrap_230\' ) ) {
        global $post;
        $post_title = get_post_meta( $post->ID, \'_flexia_post_meta_key_page_title\', true );
        $post_title_header_meta = get_post_meta( $post->ID, \'_flexia_post_meta_key_header_meta\', true );
        if( $post_title == \'default\' || $post_title == NULL ) {
            if( $flexia_single_posts_layout == \'flexia_single_posts_layout_large\' ) {
                flexia_post_large_title_markup();
                if( $post_title_header_meta == \'yes\' || $post_title_header_meta == NULL ) {
                    flexia_post_large_title_header_meta_markup();
                }
            }else {
                return false;
            }
        }elseif( $post_title == \'large\') {
            flexia_post_large_title_markup();
            if( $post_title_header_meta == \'yes\' || $post_title_header_meta == NULL ) {
                flexia_post_large_title_header_meta_markup();
            }
        }else {
            return false;
        }
    }else {
        if( $flexia_single_posts_layout == NULL || $flexia_single_posts_layout == \'flexia_single_posts_layout_large\' ) {
            flexia_post_large_title_markup();
            flexia_post_large_title_header_meta_markup();
        }
    }

}
为了对放置在子主题中的标记进行一些简单的更改,我粗略地修改了代码functions.php:

(我把我从原来的功能中删除的部分注释掉了,这样你就可以更容易地看到我删除了什么)

// This function contains large post title markup.
// It is used in \'flexia_post_large_title\' method.

function hbps_flexia_post_large_title_markup() {
    $thumbnail = \'\';
        if (function_exists(\'has_post_thumbnail\')) {
            if ( has_post_thumbnail() ) {
                 $thumbnail = wp_get_attachment_url( get_post_thumbnail_id() );
            }
        }
    ?>
    <header class="page-header single-blog-header" <?php if ( ! empty( $thumbnail ) ) : ?>
    style="background-image: url(\'<?php echo esc_attr($thumbnail); ?>\');" <?php endif; ?> <?php if ( empty( $thumbnail ) ) : ?>
    style="background-image: url(\'<?php echo esc_attr(get_header_image()); ?>\');" <?php endif; ?>>
        <div class="header-inner">
            <div class="header-content">
                <?php the_title( \'<h1 class="blog-title">\', \'</h1>\' ); ?>
            </div>
        </div>
        <div class="header-overlay"></div>
    </header>
    <?php
}

// Change blog header titles by removing Avatar and including By: before Author

// This function contains large post title author avatar markup.
// It is used in \'flexia_post_large_title_markup\' method.

function hbps_flexia_post_large_title_author_avatar_markup() {

    if( class_exists( \'CMB2_Bootstrap_230\' ) ) {
        global $post;
        $post_title_header_meta = get_post_meta( $post->ID, \'_flexia_post_meta_key_header_meta\', true );
        $post_title_header_post_author = get_post_meta( $post->ID, \'_flexia_post_meta_key_header_author_meta\', true );
        if( $post_title_header_meta == \'yes\' || $post_title_header_meta == NULL ) {
            if( $post_title_header_post_author == \'yes\' || $post_title_header_post_author == NULL ) {
            ?>
            <div class="blog-author">
                <div class="author-avatar">

                        <!-- DELETE AVATAR IN HEADER -->
<!--                    <?php echo get_avatar( get_the_author_meta( \'ID\' ), \'flexia-thumbnail-avatar\' ); ?> -->

                    <div class="author-body">
                        <h4 class="author-heading">By: <?php the_author(); ?></h4>
                    </div>
                </div>
            </div>
            <?php
            }else {
                return false;
            }
        }else {
            return false;
        }
    }else {
        ?>
        <div class="blog-author">
            <div class="author-avatar">

                    <!-- DELETE AVATAR IN HEADER -->
<!--                <?php echo get_avatar( get_the_author_meta( \'ID\' ), \'flexia-thumbnail-avatar\' ); ?> -->

                <div class="author-body">
                    <h4 class="author-heading">By: <?php the_author(); ?></h4>
                </div>
            </div>
        </div>
        <?php
    }
}

function hbps_flexia_post_large_title_header_meta_markup() {
    ?>
    <header class="entry-header single-blog-meta single-post-meta-large">
        <?php
        if ( \'post\' === get_post_type() ) : ?>
        <div class="entry-meta">
            <?php flexia_updated_on(); ?>
        </div><!-- .entry-meta -->
        <?php
        endif; ?>
    </header><!-- .entry-header -->
    <?php
}

// This function will show/hide large post title

function hbps_flexia_post_large_title() {
    $flexia_single_posts_layout = get_theme_mod(\'flexia_single_posts_layout\', \'flexia_single_posts_layout_large\');
    if( class_exists( \'CMB2_Bootstrap_230\' ) ) {
        global $post;
        $post_title = get_post_meta( $post->ID, \'_flexia_post_meta_key_page_title\', true );
        $post_title_header_meta = get_post_meta( $post->ID, \'_flexia_post_meta_key_header_meta\', true );
        if( $post_title == \'default\' || $post_title == NULL ) {
            if( $flexia_single_posts_layout == \'flexia_single_posts_layout_large\' ) {
                hbps_flexia_post_large_title_markup();
                if( $post_title_header_meta == \'yes\' || $post_title_header_meta == NULL ) {
                    hbps_flexia_post_large_title_header_meta_markup();
                }
            }else {
                return false;
            }
        }elseif( $post_title == \'large\') {
            hbps_flexia_post_large_title_markup();
            if( $post_title_header_meta == \'yes\' || $post_title_header_meta == NULL ) {
                hbps_flexia_post_large_title_header_meta_markup();
            }
        }else {
            return false;
        }
    }else {
        if( $flexia_single_posts_layout == NULL || $flexia_single_posts_layout == \'flexia_single_posts_layout_large\' ) {
            hbps_flexia_post_large_title_markup();
            hbps_flexia_post_large_title_header_meta_markup();
        }
    }

}

1 个回复
最合适的回答,由SO网友:Akshat 整理而成

您可以更改函数的名称以及所需的修改,并将其复制到子主题的函数文件中,然后在原始文件中搜索add_filteradd_action 在子主题中,您可以使用remove_filterremove_action 以及您可以使用的add_filteradd_action 使用新功能。简而言之,您可以简单地找到add_actionadd_filter 对于这些修改过的函数,请复制它们并将其从“添加”重命名为“删除”,然后再次复制add_actionadd_filter 从原始主题开始,这一次可以替换函数的名称。

更新:对于这个特定主题,您不需要add_filterremove_filter 因为函数flexia_post_large_title_author_avatar_markup 在第174行直接调用,可能它没有在任何其他地方使用,因此它没有连接到任何操作,第二个函数也是如此

结束

相关推荐

对WooCommerce的Functions.php更改不起作用

我正在尝试将其添加到函数中。php。在我的另一个站点上的测试实验室中,它可以很好地添加测试信息。但在这个网站上,我试图把它添加到什么都没有发生的情况下。该网站正在使用最新的woocommerce运行一个建筑节俭主题。有什么想法吗?我基本上只是想在每个产品页面上添加一行类似于免责声明但无法添加的文本。add_action(\'woocommerce_before_add_to_cart_form\',\'print_something_below_short_description\');