Custom taxonomy template
-
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
-
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
termsThe 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.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?
The syntax for custom template tags is different from the standard template tags: use
starsBest,
JoeAh – 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>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)
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’ π
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 7 years, 1 month ago by
Anonymous User 13882600.
-
This reply was modified 7 years, 1 month ago by
Anonymous User 13882600.
-
This reply was modified 7 years, 1 month ago by
The topic ‘Custom taxonomy template’ is closed to new replies.