Support » Plugin: Related Posts by Taxonomy » Extra word appearing at end of related posts excerpt

  • Resolved outofhouse

    (@outofhouse)


    Hi – I have created a new template as per your instructions. However, the excerpt appears to be including the word “Related” at the end of each related grids item – at the end of each excerpt. I have searched my code but cannot find where this is coming from.
    example at: https://bournhall.ooh-websites.co.uk/a-very-special-fathers-day-thanks-to-bourn-hall/ (see related articles at the bottom of the page).

    Here’s my functions.php code:

    /**Add related posts section**/
    add_filter( 'the_content', 'add_related_posts_after_post_content' );
    function add_related_posts_after_post_content( $content ) {
     
        //check if it's a single post page.
        if ( is_single() ) {
     
            // check if we're inside the main loop
            if ( in_the_loop() && is_main_query() ) {
     
                // add your own attributes here (between the brackets [ ... ])
                $shortcode = '[related_posts_by_tax posts_per_page="3" title="Related Articles" image_size="large" format="thumbnail_excerpt"]';
     
                // add the shortcode after the content
                $content = $content . $shortcode;
            }
        }
     
        return $content;
    }
    
    /*hook in a new related posts grid*/
    add_filter( 'related_posts_by_taxonomy_template', 'rpbt_thumbnail_exerpt_format_template', 10, 3 );
     
    // Return the right template for the thumbnail_excerpt format
    function rpbt_thumbnail_exerpt_format_template( $template, $type, $format ) {
        if ( isset( $format ) && ( 'thumbnail_excerpt' === $format ) ) {
            return 'related-posts-thumbnail-excerpts.php';
        }
        return $template;
    }
    
    // Create new format thumbnail_excerpt for use in widget and shortcode
    add_action( 'wp_loaded', 'rpbt_thumbnail_excerpt_format', 11 );
     
    function rpbt_thumbnail_excerpt_format() {
     
        if ( !class_exists( 'Related_Posts_By_Taxonomy_Defaults' ) ) {
            return;
        }
     
        $defaults = Related_Posts_By_Taxonomy_Defaults::get_instance();
     
        // Add the new format .
        $defaults->formats['thumbnail_excerpt'] = __( 'Thumbnail with excerpt' );
    }
    
    // Return posts with post thumbnails for the thumbnail_excerpt format.
    add_filter( 'related_posts_by_taxonomy_shortcode_atts', 'rpbt_thumbnail_exerpt_args' ); // shortcode
    add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_thumbnail_exerpt_args' ); // widget
     
    function rpbt_thumbnail_exerpt_args( $args ) {
        if (  'thumbnail_excerpt' === $args['format'] ) {
            $args['post_thumbnail'] = true;
        }
     
        return $args;
    }

    and here’s the code from my related-posts-thumbnail-excerpts.php file:

    <?php if ( $related_posts ) : ?>
    
     
        <?php foreach ( $related_posts as $post ) :
            setup_postdata( $post );
     
        // Check if size was set in widget or shortcode
        $size = isset( $rpbt_args['size'] ) ? $size : 'large';
    ?>
     
            <div class="ooh-related-posts-grid">
            <!-- post thumnail -->
            <div class="ooh-related-posts-grid-thumb">
            <?php if ( has_post_thumbnail() ) : ?>
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( $size ); ?></a>
                <div class="ooh-related-posts-grid-date"><?php the_modified_date(); ?></div>
                 <!-- title -->
            <div class="ooh-related-posts-grid-title">
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
            </div>
                </div>
            <?php endif; ?>
     
           
     
            <!-- excerpt -->
            <div class="ooh-related-posts-grid-excerpt">
            <?php the_excerpt(); ?></div>
            </div>
        <?php endforeach; ?>
        
     
     
    <?php else : ?>
    <p><?php _e( 'No related posts found', 'related-posts-by-taxonomy' ); ?></p>
    
    <?php endif ?>

    Any help much appreciated!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter outofhouse

    (@outofhouse)

    Sorry here’s the full functions.php file code (didn’t swipe it all first time round!)

    /**Add related posts section**/
    add_filter( 'the_content', 'add_related_posts_after_post_content' );
    function add_related_posts_after_post_content( $content ) {
     
        //check if it's a single post page.
        if ( is_single() ) {
     
            // check if we're inside the main loop
            if ( in_the_loop() && is_main_query() ) {
     
                // add your own attributes here (between the brackets [ ... ])
                $shortcode = '[related_posts_by_tax posts_per_page="3" title="Related Articles" image_size="large" format="thumbnail_excerpt"]';
     
                // add the shortcode after the content
                $content = $content . $shortcode;
            }
        }
     
        return $content;
    }
    
    /*hook in a new related posts grid*/
    add_filter( 'related_posts_by_taxonomy_template', 'rpbt_thumbnail_exerpt_format_template', 10, 3 );
     
    // Return the right template for the thumbnail_excerpt format
    function rpbt_thumbnail_exerpt_format_template( $template, $type, $format ) {
        if ( isset( $format ) && ( 'thumbnail_excerpt' === $format ) ) {
            return 'related-posts-thumbnail-excerpts.php';
        }
        return $template;
    }
    
    // Create new format thumbnail_excerpt for use in widget and shortcode
    add_action( 'wp_loaded', 'rpbt_thumbnail_excerpt_format', 11 );
     
    function rpbt_thumbnail_excerpt_format() {
     
        if ( !class_exists( 'Related_Posts_By_Taxonomy_Defaults' ) ) {
            return;
        }
     
        $defaults = Related_Posts_By_Taxonomy_Defaults::get_instance();
     
        // Add the new format .
        $defaults->formats['thumbnail_excerpt'] = __( 'Thumbnail with excerpt' );
    }
    
    // Return posts with post thumbnails for the thumbnail_excerpt format.
    add_filter( 'related_posts_by_taxonomy_shortcode_atts', 'rpbt_thumbnail_exerpt_args' ); // shortcode
    add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_thumbnail_exerpt_args' ); // widget
     
    function rpbt_thumbnail_exerpt_args( $args ) {
        if (  'thumbnail_excerpt' === $args['format'] ) {
            $args['post_thumbnail'] = true;
        }
     
        return $args;
    }
    Thread Starter outofhouse

    (@outofhouse)

    Update – I have tried turning other plugins off but this makes no difference.

    Thread Starter outofhouse

    (@outofhouse)

    Ok – SOLVED – seems like Jetpack was adding this to any excerpts. Have disabled it and all is well again.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi outofhouse

    Sorry for the late reply. Is this a setting in Jetpack or did you have to disable Jetpack?

    Thread Starter outofhouse

    (@outofhouse)

    Hi

    I couldn’t see any settings in JetPack to avoid this so have disabled it (I did have to switch off JetPack’s social icons to stop them displaying in the excerpt). I use another plugin (Essential Grid) to display grids of posts on this site and JetPack wasn’t injecting anything after the excerpts for these grids.

    It seems the templating option I used to design around your plugin was allowing JetPack to add share buttons and extra info (although I haven’t checked if it’s doing the same for your standard widgets and shortcodes that don’t use custom templating. The only way to stop it was to disable Jet pack.

    Might be worth having a look – I presume it should be an easy enough thing to recreate if there is an issue there. It would be nice to use JetPack at some point in the future as I was using it’s security and backup features.

    Thanks
    Rob

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Thanks for the reply.

    When I find the time I will look into it. The problem with the_exerpt() and the_content() is that any plugin or theme can add content to it. The only feasible way (I think) is to replace them (in the template) with your own functions.

    Anyway, I will look into it and let you know when I’ve got a solution for this problem.

    • This reply was modified 5 years, 10 months ago by keesiemeijer.
    Thread Starter outofhouse

    (@outofhouse)

    thanks

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Oops, sorry this fell of my radar.

    I will work on it this week.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Ok, try this.

    Add this in your theme’s functions.php

    
    // Adding our own default filters for the excerpt.
    add_filter( 'rpbt_the_excerpt',     'wptexturize'      );
    add_filter( 'rpbt_the_excerpt',     'convert_smilies'  );
    add_filter( 'rpbt_the_excerpt',     'convert_chars'    );
    add_filter( 'rpbt_the_excerpt',     'wpautop'          );
    add_filter( 'rpbt_the_excerpt',     'shortcode_unautop');
    add_filter( 'rpbt_get_the_excerpt', 'wp_trim_excerpt'  );

    And in the related-posts-thumbnail-excerpts.php replace:

    
    <?php the_excerpt(); ?>

    with

    
    <?php echo apply_filters('rpbt_the_excerpt',  get_the_excerpt() ); ?>

    Now you should be able to use JetPack again.

    Thread Starter outofhouse

    (@outofhouse)

    Sorry – that wasn’t meant to be a sarcastic thanks! Cheers for having a look at this.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    No worries, I din’t think it was 🙂

    Let me know if this solves your issue

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Extra word appearing at end of related posts excerpt’ is closed to new replies.