首页 > 解决方案 > 如何在 Wordpress 中覆盖父模板的页脚?

问题描述

我是 Wordpress 和 PHP 的新手,我尝试了一些东西来尝试覆盖我的页脚,但我找不到方法。我的情况是父母的页脚不可配置,我不知道如何在孩子中覆盖它。

家长代码

footer.php (我要修改的部分在get_template_part( 'template-parts/footer/site', 'info' );

<?php
/**
 * The template for displaying the footer
 *
 * Contains the closing of the #content div and all content after
 */
?>
        </div><!-- .wrapper -->
    </div><!-- .site-content -->

    <?php get_template_part( 'template-parts/footer/footer', 'instagram' ); ?>

    <footer id="colophon" class="site-footer" role="contentinfo">

        <?php get_template_part( 'template-parts/footer/footer', 'widgets' ); ?>

        <div id="site-generator">
            <?php get_template_part( 'template-parts/navigation/navigation', 'footer' ); ?>

            <?php get_template_part( 'template-parts/footer/site', 'info' ); ?>
        </div><!-- #site-generator -->

    </footer><!-- .site-footer -->
</div><!-- .site -->

<?php wp_footer(); ?>
</body>
</html>

网站信息.php

<?php
/**
 * The template used for displaying credits
 *
 */
?>

<?php
/**
 * credits hook
 * @hooked footer_content - 10
 */
do_action( 'credits' );

模板标签.php

<!-- More code before -->

/**
 * Footer Text
 *
 * @get footer text from theme options and display them accordingly
 * @display footer_text
 * @action footer_content
 *
 */
function footer_content() {
    $theme_data = wp_get_theme();

    $default_footer_content = sprintf( _x( 'Copyright &copy; %1$s %2$s. All Rights Reserved. %3$s ', '1: Year, 2: Site Title with home URL, 3: Privacy Policy Link', 'parent-theme' ), esc_html( date_i18n( __( 'Y', 'parent-theme' ) ) ), '<a href="'. esc_url( home_url( '/' ) ) .'">'. esc_html( get_bloginfo( 'name', 'display' ) ) . '</a>', get_the_privacy_policy_link() . $theme_data->get( 'Name' ) . '&nbsp;' . esc_html__( 'by', 'parent-theme' ) . '&nbsp;<a target="_blank" href="' . esc_url( $theme_data->get( 'AuthorURI' ) ) . '">' . esc_html( $theme_data->get( 'Author' ) ) . '</a>' );

    $footer_content =  '
    <div class="site-info">
        <div class="wrapper">';

        $footer_content .= '<div id="footer-left-content" class="copyright">' . $default_footer_content . '</div>';

    $footer_content .=  '
        </div><!-- .wrapper -->
    </div><!-- .site-info -->';

    echo $footer_content;
}
add_action( 'credits', 'footer_content', 10 );

<!-- More code after-->

所以这个footer_content()函数是我想要覆盖的函数。在我的子主题的functions.php中,我尝试在添加以下内容后取消挂钩'footer_content'挂钩以添加我的:

孩子的密码

/**
 * Footer (overrided) 
 */

function remove_parent_footer_function() {
    remove_action('after_setup_theme', 'footer_content');
}
add_action('wp_loaded', 'remove_parent_footer_function');

function child_footer_content() {
    $theme_data = wp_get_theme();
    
    $default_footer_content = sprintf( _x( 'Copyright &copy; %1$s %2$s. All Rights Reserved. %3$s ', '1: Year, 2: Site Title with home URL, 3: Privacy Policy Link', 'parent-theme' ), esc_html( date_i18n( __( 'Y', 'parent-theme' ) ) ), '<a href="'. esc_url( home_url( '/' ) ) .'">'. esc_html( get_bloginfo( 'name', 'display' ) ) . '</a>', get_the_privacy_policy_link() . '' );

    $footer_content =  '
    <div class="site-info">
        <div class="wrapper">';

        $footer_content .= '<div id="footer-left-content" class="copyright">' . $default_footer_content . '</div>';

    $footer_content .=  '
        </div><!-- .wrapper -->
    </div><!-- .site-info -->';

    echo $footer_content;
}
add_action('after_setup_theme', 'child_footer_content');

但结果是“child_footer_content()”被执行,在网站顶部创建了一个包含正确信息的 div,但父页脚没有被修改并继续显示在底部。

知道我做错了什么吗?

标签: phpwordpressfunctionoverridingfooter

解决方案


推荐阅读