How to remove “read more” link from custom post type excerpt

Is there a way I can add some kind of pre_get_posts() filter to strip out the “read more” link that appears at the end of the_excerpt() for only 1 certain custom post type that I specify?

If so, can someone please help me out with the code? I’ve been working at it for a while but haven’t gotten anywhere. Any help would be greatly appreciated. Thanks!

Related posts

5 comments

  1. Put the following code in functions.php to show “read more” on all post types except custom_post_type.

    function excerpt_read_more_link($output) {
      global $post;
      if ($post->post_type != 'custom_post_type')
      {
        $output .= '<p><a href="'. get_permalink($post->ID) . '">read more</a></p>';  
      }
      return $output;
    }
    add_filter('the_excerpt', 'excerpt_read_more_link');
    
  2. An easy solution is to put following code inside style.css:

     a.read-more {
        display:none;
     }
    

    This targets <a class="read-more">

  3. What about this? Basically it’s a way to customize the text by adding a callback function to the functions.php file. I’m thinking, however, if you just return a space instead, then it should override it and not display anything.

    // Replaces the excerpt "more" text by a link
    function new_excerpt_more($more) {
       global $post;
       return ' ';
    }
    add_filter('excerpt_more', 'new_excerpt_more');
    

    I got this from The WordPress codex

    Edit:

    This is untested, but what if you do this:

    // Replaces the excerpt "more" text by a link
    function new_excerpt_more($more) {
       global $post;
       if ($post->post_type == 'your-cpt')
       {
          return "&nbsp;";
       }
    }
    add_filter('excerpt_more', 'new_excerpt_more');
    

    To reiterate, I haven’t tested this, but may get you on the right track (i.e. var_dump($post) to see how you can bend it to your will from within the new_excerpt_more function.

  4. function custom_theme_developement_view_product_button(){
        global $product;
        $link = $product->get_permalink();
        echo '<a href="" class="added_to_cart wc-forward" title="View cart"></a>'; 
    }
    add_action( 'woocommerce_after_shop_loop_item', 'custom_theme_developement_view_product_button', 11 );
    

Comments are closed.