【问题标题】:wordpress theme function overridewordpress 主题功能覆盖
【发布时间】:2025-12-30 13:05:09
【问题描述】:

我需要重写这个父主题函数:

    add_action( 'travelify_after_post_content', 'travelify_next_previous_post_link', 10 );
/**
 * Shows the next or previous posts link with respective names.
 */
function travelify_next_previous_post_link() {
    if ( is_single() ) {
        if( is_attachment() ) {
        ?>
            <ul class="default-wp-page clearfix">
                <li class="previous"><?php previous_image_link( false, __( '&laquo; Previous', 'travelify' ) ); ?></li>
                <li class="next"><?php next_image_link( false, __( 'Next &raquo;', 'travelify' ) ); ?></li>
            </ul>
        <?php
        }
        else {
        ?>
            <ul class="default-wp-page clearfix">
                <li class="previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'travelify' ) . '</span> %title' ); ?></li>
                <li class="next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'travelify' ) . '</span>' ); ?></li>
            </ul>
        <?php
        }
    }
}

在我的孩子主题中使用这个:

add_action( 'travelify_after_post_content', 'travelify_next_previous_post_link', 10 );
/**
 * Shows the next or previous posts link with respective names.
 */
function travelify_next_previous_post_link() {
    if ( is_single() ) {
        if( is_attachment() ) {
        ?>
            <ul class="default-wp-page clearfix">
                <li class="previous"><?php previous_image_link( false, __( '&laquo; Previous', 'travelify' ) ); ?></li>
                <li class="next"><?php next_image_link( false, __( 'Next &raquo;', 'travelify' ) ); ?></li>
            </ul>
        <?php
        }
        else {
        ?>
            <ul class="default-wp-page clearfix">
                    <li class="previous"><?php previous_post_link_plus( array('order_by' => 'menu_order', 'loop' => true, 'link' => '%title' ) ); ?></li>
                    <li class="next"><?php next_post_link_plus( array('order_by' => 'menu_order', 'loop' => true, 'link' => '%title' ) ); ?></li>
            </ul>
        <?php
        }
    }
}

我无法找到在我的子主题 functions.php 文件中覆盖它的正确方法。

我试过这个方法:

if ( ! function_exists( 'travelify_next_previous_post_link' ) )
   function travelify_next_previous_post_link() {
     NEW FUNCTION HERE....
   }

这只是给了我一个白屏。

我需要在我的子主题 functions.php 中添加什么来替换该特定功能?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    你可以试试这个

        <?php
        function child_remove_parent_function() {
           remove_action( 'travelify_after_post_content', 'travelify_next_previous_post_link', 10 );
        }
        add_action( 'wp_loaded', 'child_remove_parent_function' );
        ?>
    

    删除要加载的函数。

    或者您可以增加您的子功能优先级 这是为您完成的解决方案。 http://code.tutsplus.com/tutorials/a-guide-to-overriding-parent-theme-functions-in-your-child-theme--cms-22623

    【讨论】:

    • 好的,我添加了 child_remove_parent_function(),它确实删除了 prev/next 链接,但是我现在如何添加我的自定义函数?
    • add_action( 'wp_loaded', 'child_remove_parent_function' ); 应该类似于 add_action( 'travelify_after_post_content', 'name_of_your_custom_function' );