Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi Beginner3000

    Try it with this in your (child) theme’s functions.php file

    add_filter( 'related_posts_by_taxonomy', 'rpbt_add_title_filter', 10, 4 );
    
    function rpbt_add_title_filter( $results, $post_id, $taxonomies, $args ) {
    
    	if( isset( $args['type'] ) && ( 'shortcode' === $args['type'] ) ){
    
    		// Add a filter to the title for the shortcode
    		add_filter( 'the_title', 'add_taxonomy_to_title', 10, 2 );
    
    		// Remove the filter after displaying related posts
    		add_filter( 'related_posts_by_taxonomy_after_display', 'rpbt_remove_filter' );
    	}
    
    	return $results;
    }
    
    function add_taxonomy_to_title( $title, $id ) {
    
    	// Taxonomy names used by individual post
    	$taxonomy_names = get_post_taxonomies( $id );
    
    	// Get taxonomy titles for the names found
    	if ( class_exists( 'Related_Posts_By_Taxonomy_Defaults' ) ) {
    
    		$defaults = Related_Posts_By_Taxonomy_Defaults::get_instance();
    		$taxonomies = $defaults->taxonomies;
    
    		foreach ( $taxonomy_names as $key => $name ) {
    			$taxonomy_names[ $key ] = isset( $taxonomies[ $name ] ) ? $taxonomies[ $name ] : $name;
    		}
    	}
    
    	// Add taxonomy titles to the title
    	if ( !empty( $taxonomy_names ) ) {
    		$taxonomy_names = implode( ', ', $taxonomy_names );
    		$title = $title . ' (' . $taxonomy_names . ')';
    	}
    
    	return $title;
    }
    
    // Remove the filter
    function rpbt_remove_filter() {
    	remove_filter( 'the_title', 'add_taxonomy_to_title', 10, 2 );
    }

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

    Thread Starter Beginner3000

    (@beginner3000)

    Tnx Keesie,

    unfortunately this code didnt work on my site. It breaks the whole site. I thinks there is a syntax error somewhere. However, I couldn’t find it myself.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Strange, there are no errors on my test site. Can you paste your functions.php file (with the code above) in a pastebin and post the link to it here.
    https://pastebin.com/

    Thread Starter Beginner3000

    (@beginner3000)

    Hi Keesie,

    my apologies, your code did work. I probably made a mistake with the copy paste. However, I havent explained correctly what I meant the first time. What I would like is to see is the related taxonomy instead of the taxonomy label itself. E.g. The related post is based on the taxonomy “Country”. Instead of showing the label “Country” I would like to see the taxonomy item “Germany” as they have both share the taxonomy Germany.

    Is that possible without much customisation? It is not really a big thing for me so please do not spend a lot of time in this, it is more a feature request that makes your plugin more useful.

    Btw, I really like your plugin. It is way better then most other related posts plugins.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi Beginner3000

    Remove the code you’ve pasted and replace it with this:

    add_filter( 'related_posts_by_taxonomy', 'rpbt_add_title_filter', 10, 4 );
    
    function rpbt_add_title_filter( $results, $post_id, $taxonomies, $args ) {
    	global $rpbt_related_args;
    
    	if ( isset( $args['type'] ) && ( 'shortcode' === $args['type'] ) ) {
    		$names_args = array(
    			'include'           => $args['related_terms'],
    			'fields'            => 'id=>name',
    		);
    
    		// Get the term names for the related terms used in the query;
    		$terms = get_terms( $taxonomies, $names_args );
    
    		$rpbt_related_args = $args;
    		$rpbt_related_args['term_names'] = $terms;
    		$rpbt_related_args['taxonomies'] = $taxonomies;
    
    		// Add a filter to the title for the shortcode.
    		add_filter( 'the_title', 'rpbt_add_terms_to_title', 10, 2 );
    
    		// Remove the filter after displaying related posts.
    		add_filter( 'related_posts_by_taxonomy_after_display', 'rpbt_remove_filter' );
    	}
    
    	return $results;
    }
    
    function rpbt_add_terms_to_title( $title, $id ) {
    	global $rpbt_related_args;
    
    	// Get the terms for the current post ID
    	$terms = wp_get_object_terms( $id, array_map( 'trim', (array) $rpbt_related_args['taxonomies'] ), array( 'fields' => 'ids' ) );
    
    	// Remove terms not assigned to current post ID.
    	$terms = array_values( array_intersect( $rpbt_related_args['related_terms'], $terms ) );
    	$names = '';
    
    	// Create term names string
    	if ( $terms ) {
    		foreach ( $terms as $term ) {
    			if ( isset( $rpbt_related_args['term_names'][$term] ) ) {
    				$names .= $rpbt_related_args['term_names'][$term] . ', ';
    			}
    		}
    		$names = trim( $names, ', ' );
    		$names = !empty( $names ) ? ' (' . $names . ')' : '';
    	}
    
    	return $title . $names;
    }
    
    // Remove the filter
    function rpbt_remove_filter() {
    	remove_filter( 'the_title', 'rpbt_add_terms_to_title', 10, 2 );
    }

    Thread Starter Beginner3000

    (@beginner3000)

    Just tried it. Although there is no error, none of the taxonomy terms are displayed.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Can you paste and submit the content of your functions.php file in a pastebin and post a link to it here.

    https://pastebin.com/

    Thread Starter Beginner3000

    (@beginner3000)

    Check this link

    https://pastebin.com/xFm8U2Jy

    I’m using the following shortcode:

    <?php echo do_shortcode(‘ [related_posts_by_tax post_types=”travel” image_size=”thumb-small” format=”thumbnails” columns=”2″ order=”RAND” title=”” posts_per_page=”4″ taxonomies=”country”] ‘); ?>

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Aha, it doesn’t work for the format “thumbnails” yet.

    Try it with the development version 2.1-RC1.
    https://downloads.www.ads-software.com/plugin/related-posts-by-taxonomy.zip

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Show taxonomy label’ is closed to new replies.