Livi17
Livi17

Reputation: 1660

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

I'm trying to change the "read more" link in a child theme.

From the codex, I've added the following to the functions.php file of the parent theme (twentyseventeen)

// 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');

...and it does what I'd like it to do.

However, I don't want it to be wiped out if the theme is ever updated. So I've created a child-theme.

However when I add the code to the child theme functions.php file, it does not work.

My child-theme functions.php file looks like this:

<?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');

Upvotes: 0

Views: 812

Answers (1)

Livi17
Livi17

Reputation: 1660

I was able to get this to work with some information from this question:

Wordpress functions.php child theme [Resolved]

In my child-theme functions PHP, I checked to see if the original twentyseventeen_excerpt_more() function has executed, and then give my alternative readmore() function a higher priority. With the higher priority (15), add_filter( 'excerpt_more', 'readmore', 15 ); my function runs first, and the original read more function is ignored in the parent theme.

This is what the function looks like in my child-theme functions.php file:

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 );
}

BTW, the above could also be inserted into a plugin .php file if you would prefer.

Upvotes: 2

Related Questions