首页 > 解决方案 > 如何覆盖父 WordPress 主题中的函数

问题描述

我正在使用Flaxseed作为父级构建一个子主题。此主题包括几个“内容样式”,可以使用主题 > 定制器进行选择。这些样式中的每一个在父 functions.php 文件中都有一个函数,它将 CSS 和 wordpress 内容呈现为一个。我想编辑此处的一些循环函数,特别是将 the_excerpt 更新为 the_content,但我想以适合我的子主题的方式进行。

由于这段代码在父functions.php 中,而且主题定制器专门通过名称调用它,所以我不能简单地添加一个新函数,也不能使用相同的函数名覆盖它。

我最好的猜测是,在我的子function.php中加载一个同名的新函数之前,我需要以某种方式从父functions.php中删除这个函数,但我似乎无法弄清楚如何。

下面是函数加载到模板中的模板文件中的代码:

    <?php for ($i = 1; $i < 8; $i++)
    if (get_theme_mod('flaxseedpro_fa_'.$i) != 'none') {
        flaxseedpro_display_featured_area( 
            get_theme_mod('flaxseedpro_fa_'.$i), 
            get_theme_mod('flaxseedpro_fa_title'.$i), 
            get_theme_mod('flaxseedpro_fa_cat'.$i)
        );
    }   
?>

这里,$i 变量是在 Theme > Customizer 屏幕中设置的值。该文件和代码可以作为子主题的一部分轻松修改。

下面是来自父functions.php的两个代码片段,它们选择了适当的特色区号:

function flaxseedpro_display_featured_area($style, $fa_title, $cat) {
    if (is_home()) :
        switch ($style) {
            case 'carousel':
                flaxseedpro_carousel($fa_title, $cat);
                break;
            case 'style1':
                flaxseedpro_featured_style1($fa_title, $cat);
                break;
            case 'style2':
                flaxseedpro_featured_style2($fa_title, $cat);
                break;
            case 'style3':
                flaxseedpro_featured_style3($fa_title, $cat);
                break;
            case 'style4':
                flaxseedpro_featured_style4($fa_title, $cat);
                break;
            default:
                break;
        }
    endif;
}

这导致了以下几个功能:

function flaxseedpro_featured_style2($fa_title, $cat) {
    ?>
    <div class="featured-style1 featured-style2">
        <?php if ('' !== $fa_title) : ?>
            <h2 class="featured-section-title container">
                <?php echo esc_html($fa_title); ?>
            </h2>
        <?php endif; ?>
        <div class="featured-posts-inner container">
            <?php
            $args = array(
                'posts_per_page' => 5,
                'cat' => $cat,
            );
            $fa_posts = new WP_Query($args);
            if ($fa_posts->have_posts()) :
                $counter = 0;
                while ($fa_posts->have_posts()) :
                    $fa_posts->the_post();
                    $counter++;
                    if (1 === $counter) {
                        ?>
                        <div class="feat-col1 md-6 sm-6">
                            <div class="md-12 pr-0 pl-0">
                                <article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
                                    <div class="item-container mb-3">
                                        <?php if (has_post_thumbnail()) : ?>    
                                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium-large', array('class' => 'featured-post-thumbnail-primary')); ?></a>
                                        <?php else : ?>
                                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>"></a>
                                        <?php endif; ?>
                                    </div>
                                    <div class="post-title-parent">
                                        <a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        <div class="post-author">
                                            <?php esc_html_e('By', 'flaxseed-pro'); ?> <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>" title="<?php echo esc_attr(get_the_author()); ?>"><?php the_author(); ?></a>&nbsp;&nbsp;<i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?>
                                        </div>
                                        <div class="entry-excerpt body-font mb-3"><?php the_excerpt(); ?></div>
                                    </div>
                                    <a href="<?php the_permalink(); ?>" class="theme-button"><?php _e('Read More','flaxseed-pro') ?></a>
                                </article>
                            </div>
                        </div>
                        <?php
                    } else {
                        ?>
                        <div class="feat-col2 md-6 sm-6">
                            <article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
                                <div class="md-4 xs-4">
                                    <div class="item-container">
                                        <?php if (has_post_thumbnail()) : ?>    
                                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium', array('class' => 'featured-post-thumbnail-secondary')); ?></a>
                                        <?php else : ?>
                                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>"></a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <div class="md-8 xs-8">
                                    <div class="post-title-parent">
                                        <a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        <br><small><?php esc_html_e('By', 'flaxseed-pro'); ?> <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>" class="url fn n" title="<?php echo esc_attr(get_the_author()); ?>"><?php the_author(); ?></a>&nbsp;&nbsp;<i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></small>
                                    </div>
                                </div>
                            </article>
                        </div>
                        <?php
                    }
                endwhile;
            endif;
            wp_reset_postdata();
            ?>

        </div>
    </div>
    <?php
}

这是我一直试图覆盖但无法找到解决方案作为子主题的一部分的代码。我找到的所有答案似乎都需要一个钩子,我无法从这段代码中辨别出来。

标签: wordpresswordpress-themingthemescustom-wordpress-pageswordpress-rest-api

解决方案


这解决了。

我更改了在我的模板文件中调用的函数名称 (flaxseedpro_display_featured_area >> flaxseedpro_display_featured_area2),然后重新定义了切换函数以将 'style3' 替换为新的 'style5'。然后使用更新的 CSS 代码添加了新的“style5”功能。


推荐阅读