Support » Plugin: WP to Twitter » Custom taxonomy template

  • Resolved inside83

    (@boyansharic)


    Hello

    I created a custom taxonomy, like tags, and I would like to add them to post template when posting to Twitter. It’s called ‘Stars’ and I tried with {{stars}} but it doesn’t get posted to Twitter.

    Please help

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Joe Dolson

    (@joedolson)

    You can access custom taxonomies by creating a custom filter to get them, but they aren’t available by default.

    Here’s an example showing how to set up a custom tag that will fetch taxonomy terms from a taxonomy called ‘your-custom-taxonomy’, using the template tag terms

    The double curly brace syntax is for fetching user meta data, so that won’t work in this case.

    Thank you for your reply but where’s the example? I guess you forgot to paste the link.

    I used plugin called ‘Simple Taxonomy’ to create the custom taxonomy and it worked really well. The taxonomy I created is without hierarchy, so just like the default tags.

    Also, is it possible to put subcategories in post template? Now I use
    #title# ##category# #tags# #url#
    (notice the double hash) to get the category hashtag and it’s great but it only posts the category, not the subcategory AND category must be primary.

    Plugin Author Joe Dolson

    (@joedolson)

    Whoops. Yep, looks like I did:

    https://github.com/joedolson/plugin-extensions/blob/master/wp-to-twitter/custom-taxonomy-terms.php

    It is possible to get subcategories; you can do that via a custom template tag, as well, if you wish. It would be very similar to the above, except you’d use the ‘category’ taxonomy and probably need to append a ‘#’ before each category name.

    I’m sorry to bother you again but could you just help me with this bit.
    I edited the code to:

    
    add_filter( 'wpt_custom_shortcode', 'my_custom_taxonomy_terms', 10, 3 );
    function my_custom_taxonomy_terms( $value, $post_ID, $field ) {
    	if ( $field == 'terms' ) {
    		$terms = get_the_terms( $post_ID, 'stars' );
    		foreach ( $terms as $term ) {
    			$names[] = $term->name;
    		}
    		$value = implode( ' ', $names );
    	}
    	
    	return $value;
    }
    

    Basically, I just replaced ‘your-custom-taxonomy’ with ‘stars’.
    I uploaded and activated new plugin and added #stars# in the template, but it just posted literally #stars#, not the actual names.

    What am I missing?

    Plugin Author Joe Dolson

    (@joedolson)

    The syntax for custom template tags is different from the standard template tags: use stars

    Best,
    Joe

    Plugin Author Joe Dolson

    (@joedolson)

    Ah – the content parser overwrote my info. Use double square braces: [[ and ]]

    I doesn’t work ๐Ÿ™

    My template is
    #title# #stars ##category# #tags# #url#
    and instead of #star only ‘#’ gets posted.

    I know ‘stars’ is the correct term because this is how I get it to show up on post, next to tags:
    <div><?php the_terms( $post->ID, 'stars', '<strong>Stars: </strong>', ', ', ' ' ); ?></div>

    Plugin Author Joe Dolson

    (@joedolson)

    In your code, you’re testing whether the name of the custom template tag is ‘field’, rather than ‘stars’. So, as you have the code written, you’d need to use terms (spaces added)

    Plugin Author Joe Dolson

    (@joedolson)

    Adding spaces didn’t allow the brackets to display either.

    I’m sure you know what I meant, by this point, though…

    Allright ๐Ÿ™‚
    So this is how it works…kinda…

    add_filter( 'wpt_custom_shortcode', 'my_custom_taxonomy_terms', 10, 3 );
    function my_custom_taxonomy_terms( $value, $post_ID, $field ) {
    	if ( $field == 'stars' ) {
    		$terms = get_the_terms( $post_ID, 'stars' );
    		foreach ( $terms as $term ) {
    			$names[] = $term->name;
    		}
    		$value = implode( ' ', $names );
    	}
    	
    	return $value;

    And the template is:

    #title# #double square bracesstarsdouble square braces ##category# #tags# #url#

    but, if there are two or more ‘stars’ I only get ‘#’ in front of the first one and first and last name are not joined, so ‘John Smith’ is not ‘JohnSmith’ ๐Ÿ™

    Plugin Author Joe Dolson

    (@joedolson)

    Yes, the demo doesnโ€™t insert any characters. You can do that by changing the single space in the implode first argument to โ€˜ #โ€™, which will insert those between each term; though youโ€™ll still need the starting hash.

    Youโ€™d need to join terms by manipulating them prior to the implode.

    I DID IT! ๐Ÿ™‚

    Here’s the complete code, if anyone wants it.

    add_filter( 'wpt_custom_shortcode', 'my_custom_taxonomy_terms', 10, 3 );
    function my_custom_taxonomy_terms( $value, $post_ID, $field ) {
    	if ( $field == 'stars' ) {
    		$terms = get_the_terms( $post_ID, 'stars' );
    		foreach ( $terms as $term ) {
    			$names[] = $term->name;
    		}
    		$stripped_terms = str_replace(' ', '', $names);
    		$value = implode( ' #', $stripped_terms );
    	}
    	
    	return $value;
    }

    And one needs to put
    #[[stars]]
    in the template.

    THANK YOU VERY MUCH FOR YOUR HELP!

    • This reply was modified 1 month, 2 weeks ago by  inside83.
    • This reply was modified 1 month, 2 weeks ago by  inside83.
Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.