Wordpress child-theme functions.php Read-More excerpt link

时间:2017-06-28 13:41:29

标签: php wordpress-theming

我试图改变"阅读更多"链接儿童主题。

在codex中,我已将以下内容添加到父主题的functions.php文件中(二十七)

// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
   global $post;
   return '&nbsp;&nbsp;&nbsp;<a class="moretag" href="'. 
   get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

......它做了我喜欢做的事。

但是,如果主题更新,我不希望它被删除。 所以我创造了一个儿童主题。

但是,当我将代码添加到子主题functions.php文件时,它不起作用。

我的child-theme functions.php文件如下所示:

<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
    function chld_thm_cfg_parent_css() {
        wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array(  ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );

if ( !function_exists( 'child_theme_configurator_css' ) ):
    function child_theme_configurator_css() {
        wp_enqueue_style( 'chld_thm_cfg_separate', trailingslashit( get_stylesheet_directory_uri() ) . 'ctc-style.css', array( 'chld_thm_cfg_parent','twentyseventeen-style' ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css' );

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/custom_script.js',
        array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

// END ENQUEUE PARENT ACTION



// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
       global $post;
    return '&nbsp;&nbsp;&nbsp;<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

1 个答案:

答案 0 :(得分:2)

我能够通过这个问题获得一些信息:

Wordpress functions.php child theme [Resolved]

在我的子主题函数PHP中,我检查了原来的 twentyseventeen_excerpt_more()函数是否已执行,然后将我的替代 readmore()函数更高一些优先。优先级较高的(15)add_filter( 'excerpt_more', 'readmore', 15 );我的函数首先运行,而原始的read more函数在父主题中被忽略。

这就是我的child-theme functions.php文件中的函数:

if ( ! function_exists ( 'twentyseventeen_excerpt_more' ) ) {
        function readmore( $link ) {
            if ( is_admin() ) {
                return $link;
            }

            $link = sprintf( '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
                esc_url( get_permalink( get_the_ID() ) ),
                /* translators: %s: Name of current post */
                sprintf( __( '[Read More...]<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ), get_the_title( get_the_ID() ) )
            );
            return ' &hellip; ' . $link;
        }
        add_filter( 'excerpt_more', 'readmore', 15 );
}
顺便说一下,如果您愿意,也可以将上面的内容插入到插件.php文件中。