Shortcode
-
Newbie question – I know it involves php and/or shortcode and am having trouble finding it in the forum. What would I use to call (and display) the specific custom taxonomies in single post that are associated with that single post?
Ex: colours taxonomy
Single post has blue and red checked out of several colours
Post displays <u>Blue</u> <u>Red</u>
-
@nicoleamanda,
I took a look at the plugin Taxonomy Images by Michael Fields and Ben Huson. I hope that that is the one you mean. They essentially require you to have some code to create the display.I have tried this by using this code – which I have created as a plugin that you install.
- Create a folder called
taxonomy-image-integration
in your plugins directory. -
Create in it a file called
taxonomy-image-integration.php
with content:
<?php /** Plugin Name: Taxonomy Image Integration Description: Taxonomy Image Integration. Version: 1.0 Author: Neil James License: GPL3 */ function tii_post_terms( $atts ) { $tax = shortcode_atts( array( 'tax' => '', ), $atts ); global $post; if ( ! isset( $post->post_type ) ) { return; } if ( 'colours' === $tax['tax'] ) { echo apply_filters( 'taxonomy-images-list-the-terms', '', array( 'before' => '<div class="colours-class">', 'after' => '</div>', 'before_image' => '<span> ', 'after_image' => '</span>', 'image_size' => 'detail', 'post_id' => $post->ID, 'taxonomy' => 'colours', ) ); } } add_shortcode( 'tii_post_terms', 'tii_post_terms' );
You can activate it and put the shortocde
tii_post_terms tax=colours
where you wish.My experience was that it worked but the images came out at the top of the post no matter where I put the shortcode (with the Block Editor). Your experience may be different with WPBakery.
This may give you some pointers – but I’m not sure that I can help further. You should ask within the plugin support forums.
It does highlight that I should think about adding some logic to manipulate the output more directly though there are some issues to think about in the integration with the underlying WordPress processing called.
Hope this is of use,
Neil JamesOkay, once I get this working you will bw my favourite person ever. So it’s likely still my end that it’s not working because I have it noted as “color” some places and “colour” others so I’ve tried to modify it myself but still not working. The slug is color so I took out the u everywhere in the code but still not working. Thoughts?
@nicoleamanda,
You need to put your taxonomy slug (be it color or colour or colours) identically in 3 places:- In the plug-in code here:
if ( 'colours' === $tax['tax'] ) {
and here:
'taxonomy' => 'colours',
- In youe shortcode text
[tii_post_terms tax=colours]
Note that I have taken your previous comment that you don’t need to surround the shortcode parameter with quote marks within WPBakery.
I will be explicit on a couple of points.
By putting this text as a sub-directory within the plugin directory and naming the filw as a .php file, WP will treat it as a plugin. It should show up in the plugins list – and need to be activated just as a standard one does.
Clearly Taxonomy Images also needs to be activated for it to work.
The shortcode must be executed for it to work. It must be given the correct parameter to work. [in contrast to my plug-in shortcode which will output for all relevant custom taxonomies if no parameter is given.]
Some extra thoughts. The code has a parameter ‘image_size’ set to ‘detail’ (as given in the plug-in example). It worked for me, but is not a standard image size. You could try changing it to ‘thumb’. I don’t know if it defaults to this – or I was just lucky,
When I had a post with two colour terms, the images were abutted one to the other.
That is why I added
to the ‘before_image’ in the code – to put a non-breaking space between the images.Good Luck,
Neil JamesOh my gosh you are my absolute favourite right now. It just needed the activation (I’m so new at this). Is there a place I can donate to your plug-ins?
Darn, hiccup. Wasn’t working, was still my manually entered ones showing up. But would still love to donate for all your help while I try to figure out my issue ;P
@nicoleamanda,
Sorry that it hasn’t worked properly – but the images may come out at top or bottom.Your pages have a lot of output, and you might miss it. Simplest is often to view the page source (often Ctrl+U) and then use search to find the text
colours-class
(or whatever you have in thebefore
parameter in the code). If it’s in the output anywhere, then it’s working.Very generous to offer a donation. if you would wish to, please give to our Lions Club via https://www.helloasso.com/associations/lions-club-yvelines-heraldic/formulaires/1. It’s a small club and we raise money generally for handicapped children.
Regards,
Neil JamesWonderful, will do. Don’t want you to do more work than you’ve already done for me, but I’ve just been playing around and was curious if its even possible to have a php in your original plugin > includes files that would turn the default taxonomies “included in content” to images?
ex, where is class-simpletaxonomyrefreshed-widget isI can play if its a yes, you’ve already done more than enough ??
Ignore that, lol it would be cool but for some reason when I have anything set to taxnonomies in content (even if its content not excerpt) it shows up in my excerpt and thats a whole new bucket of worms.
@nicoleamanda,
One more try.Replace all the code in
taxonomy-image-integration.php
with:<?php /** Plugin Name: Taxonomy Image Integration Description: Taxonomy Image Integration. Version: 1.0 Author: Neil James License: GPL3 */ add_filter( 'term_links-colours', 'tii_colours' ); /** * Function to disable embeds * * @return string[] */ function tii_colours( $links ) { global $post; $rlinks = array(); if ( ! isset( $post->post_type ) ) { return $rlinks; } $rlinks[] = apply_filters( 'taxonomy-images-list-the-terms', '', array( 'before' => '<div class="colours-class">', 'after' => '</div>', 'before_image' => '<span>', 'after_image' => '</span> ', 'image_size' => 'detail', 'post_id' => $post->ID, 'taxonomy' => 'colours', ) ); return $rlinks; }
Ensure that the plugin is activated.
Whenever the post terms are output, instead of them being text, they will be the images.
This will be in the content or excerpt code as well as the shortcode staxo_post_terms or even the block equivalent.
If you were to want it for another taxonomy then you need to copy the entire text and replace colours everywhere with the new slug.
It might be that you are getting terms in excerpt due to how WPBakery generates excerpts – but that’s a pure guess on my part.
Hope this is of use,
Neil JamesYou are absolutely fabulous. The only problem I’m having with the base version of Taxonomy Images Refreshed is even if I have a taxonomy set to
Display Terms with Posts > Content
it makes my excerpt blank rather than the post text. I know this is another issue all together and if this is my only problem, I can find a work around ?? Thank you for everything!!!
Oh man, I need to stop….lol I’m messing things up.
Or learn how to read. Tired mom brain. Thanks again! This is amazing.
-
This reply was modified 2 years, 8 months ago by
nicoleamanda.
-
This reply was modified 2 years, 8 months ago by
nicoleamanda.
May I bother you once more? I feel like the answer is no, but worth an ask. I’d REALLY love a way to reorder the taxonomies in the order that they appear in /wp-admin/admin.php?page=staxo_settings list (and I hope with that, the other than they appear in the default full tag list. Or would I have to recreate them in the order that I want them?
-
This reply was modified 2 years, 8 months ago by
nicoleamanda.
@nicoleamanda,
Thanks for your question.Could I ask you to please raise this as a new support request with a title something like “Changing the display order of Custom Taxonomies” since the question is likely to be of more general interest than “Shortcode”.
Thanks in advance,
Neil - Create a folder called
- The topic ‘Shortcode’ is closed to new replies.