Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress remove read more from excerpt

Tags:

php

wordpress

I am having an issue with the Remove_filter() .The remove filter works but not 100% , it wont remove the first 3 dots from Read More,and it wont replace the parent excerpt.

More clearly what I am trying to do, is to remove Read More and change it into [...] .

I want this to be done from child-theme.

Here are the screenshot exemple Before and After.

Parent theme functions.php code

    function new_excerpt_more($more) {
    global $post;
    return '… <a href="'. get_permalink($post->ID) . '">' . 'Read More!   &raquo;'     . '</a>';
   }
    add_filter('excerpt_more', 'new_excerpt_more');

Child-theme functions.php code.

       function child_theme_setup() {
       // override parent theme's 'more' text for excerpts
       remove_filter( 'excerpt_more', 'new_excerpt_more' ); 

      }
     add_action( 'after_setup_theme', 'child_theme_setup' );
    // Replaces the excerpt "Read More" text by a link
     function new_excerpt($more) {
     global $post;
     return '<a class="moretag" href="'. get_permalink($post->ID) . '">[...]                      </a>';
      }
      add_filter('excerpt', 'new_excerpt');
like image 456
Bogdan Laza Avatar asked Dec 14 '22 00:12

Bogdan Laza


2 Answers

You just need to update your code a little bit.

// Changing excerpt more
function new_excerpt_more($more) {
  global $post;
  remove_filter('excerpt_more', 'new_excerpt_more'); 
  return ' <a class="read_more" href="'. get_permalink($post->ID) . '">' . ' do whatever you want to update' . '</a>';
}
add_filter('excerpt_more','new_excerpt_more',11);

The default priority of a hook is 10. It is possible that the parent theme's hook gets added second, meaning both your hook and the parent hook have the same priority but the parent's came last therefore it doesn't reflects changes.

like image 98
Happy Coding Avatar answered Jan 01 '23 05:01

Happy Coding


If you don't want to write a new function and only want it to work in a specific area, you can also try the wp_trim_words() function combined with get_the_content()

Example:

<?php echo wp_trim_words(get_the_content(), 60) ?>

What the above will do is trim the words down on whatever returns from get_the_content() to the number of words you want (in this case, 60).

like image 34
MelanieP Avatar answered Jan 01 '23 05:01

MelanieP