I have setup some custom taxonomies like:
C10
C9.x
C8.x
C7.x
C6.x
C5.5
While I can use a plugin to sort the order of these on the back end so that they appear in the correct order (as shown above) for adding a new custom post, when I go to display it in a layout, it is always displayed (if all fields are checked) as:
C10, C5.5, C6.x, C7.x, C8.x, C9.x
How do I get the front end to display them in the same order I have them on the back end?
I've tried custom slugs with two letters and a number, but the display always seems to be taken from the text value.
Thanks for the help!
The shortcode wpv-post-taxonomy implements alphabetical sorting. You can specify ascending or descending order, but not another sorting algorithm. When numbers are included in alphabetical sorting, you can get some unexpected results because C10 will rank lower than C5 in true alphabetical order. One simple way to address this is to use preceding zeros like C05 instead of C5. Otherwise, you're probably going to need a custom code solution that parses the text and strips out the leading character, then sorts the terms in numeric order, or sorts them by a custom field somehow.
The documentation for wp_get_object_terms can be found at the link below:
https://codex.wordpress.org/Function_Reference/wp_get_object_terms
Thanks for the information ... I guess.
Prepending a 0 to the numbers is simply not an option for me as that would change the meaning of the taxonomies to something irrelevant to the content.
It would seem that the best order of action would be if your wpv-post-taxonomy shortcode supported passing a sort order as an optional parameter.
Then developers could simply pass one of the valid arguments for the Codex function you referenced to it and the problem would be resolved. In my case I could easily use custom slug names that would sort in the expected order.
Unfortunately I have no idea how to implement what you suggested as a code solution (or what you referred me to in the codex) in the Layouts Editor.
Adding some flexibility to the shortcode is a great idea. I'll pass that along to our developers as a possible improvement. I can't guarantee they'll decide to implement it, so in the meantime I have a custom shortcode you can use to sort terms by slug and display a link to the term archive:
add_shortcode( 'terms_ordered_by', 'terms_ordered_by_func');
function terms_ordered_by_func($atts) {
$post_id = $atts['postid'];
$tax = $atts['tax'];
$i = 0;
$return = '';
$args = array('orderby' => 'slug', 'order' => 'ASC');
$post_terms = wp_get_object_terms( $post_id, $tax, $args );
if ( ! empty( $post_terms ) ) {
if ( ! is_wp_error( $post_terms ) ) {
foreach( $post_terms as $term ) {
$return .= ($i ? ', ' : '') . '<a href="' . get_term_link( $term->slug, $tax ) . '">' . esc_html( $term->name ) . '</a>';
$i++;
}
}
}
return $return;
}
Use the custom shortcode on your site as follows:
[terms_ordered_by postid="12345" tax="your-taxonomy-slug"]
Replace 12345 with the ID of a post, and replace your-taxonomy-slug with the slug of the taxonomy that contains the C# terms. If you would like to make the post ID dynamic depending on where you place the shortcode, then you can use the wpv-post-id shortcode instead of hard-coding the post ID:
[terms_ordered_by postid="[wpv-post-id]" tax="your-taxonomy-slug"]
Thanks for the code!
I created a child theme and added the code to a functions.php file.
Then I called like this in the Toolset Layouts Editor:
Supported Versions: [terms_ordered_by postid="[wpv-post-id]" tax="supported-version"]
The code DOES display the taxonomies in the correct order (once I changed the slugs to be a sequence like sv1, sv2, sv3, etc).
BUT there is one problem.
It breaks the HTML output in the layout.
If I insert the regular shortcode for the taxonomy from the button I get this:
Supported Versions: [wpv-post-taxonomy type="clarion-version"]
That works correctly and the output comes after the words "Supported Versions".
But if I use the shortcode for the new function the output is generates into the upper left corner of the layout area instead of after the words "Supported Versions".
Any idea why or how to fix it?
Thanks!
Shortcodes are not supposed to output (echo) anything directly, they should return a value to the function.
Try this.
add_shortcode( 'terms_ordered_by', 'terms_ordered_by_func');
function terms_ordered_by_func($atts) {
$post_id = $atts['postid'];
$tax = $atts['tax'];
$links = array();
$args = array('orderby' => 'slug', 'order' => 'ASC');
$post_terms = wp_get_object_terms( $post_id, $tax, $args );
if ( ! empty( $post_terms ) ) {
if ( ! is_wp_error( $post_terms ) ) {
foreach( $post_terms as $term ) {
$links[] = '<a href="' . get_term_link( $term->slug, $tax ) . '">' . esc_html( $term->name ) . '</a>';
}
}
}
return implode(",", $links);
}
Thanks - that works perfectly!
I also appreciate the tip about shortcodes not outputting directly (which makes perfect sense).
Ah yes, error on my part. I've updated the code for future reference.
Christian's code was great but had conflicts with passing in a dynamic post_id. I resolved by resolving the post_id in the php.
add_shortcode( 'terms_ordered_by', 'impreza_child_terms_ordered_by_func');
function impreza_child_terms_ordered_by_func($atts) {
// $post_id = $atts['postid'];
$post_id = do_shortcode('[wpv-post-id]');
$tax = $atts['tax'];
$seperator = $atts['seperator'];
$i = 0;
$return = '';
$args = array('orderby' => 'slug', 'order' => 'ASC');
$post_terms = wp_get_object_terms( $post_id, $tax, $args );
if ( ! empty( $post_terms ) ) {
if ( ! is_wp_error( $post_terms ) ) {
foreach( $post_terms as $term ) {
$return .= ($i ? $seperator : '') . esc_html( $term->name );
$i++;
}
}
}
return $return;
}