Function below returns in alphabetical order and not, as wished, in arbitrarily order.
Is it possible to -not- return in alphabetical but in exact order as given in the array?
add_filter('the_content', 'my_function');
function my_function($content) {
if ( is_single() && in_category( '5' ) ) {
the_post_thumbnail('thumbnail', array('class' => 'top-post-img' ));
global $post;
$term_list=wp_get_post_terms($post->ID,array('school','article','teacher'),array( "fields" => "names"));
?>
<div style="margin:45px;">
School: <strong><?php print_r($term_list[0]); ?></strong><br />
Article: <strong><?php print_r($term_list[1]); ?></strong><br />
Teacher: <strong><?php print_r($term_list[2]); ?></strong>
</div>
<?php
return $content;
} else {
return $content;
}
}
I tried by adding:
array ('orderby' => 'post__in'), array ('post__in' => '24', '10', '3', '46' )
Numbers are the Taxonomy ID’s but it is not changing any, neither does it throw an error.
To find the Taxonomy ID’s I used following code, or is that the wrong code?
$terms = get_the_terms( $post->ID , 'taxonomy-name' );
if($terms) {
foreach( $terms as $term ) {
echo $term->term_id.'<br />';
}
}
Should I use list_filter
somewhere, or some below in the code after the print_r
?
Added
var_dump($term_list);
to see if output would show some else, but no it was the same..alphabetical order.
I am pretty sure forgetting some but what, and where. Or is my approach totally wrong?
Would love to find a solution.
Thanks in advance for all time and effort.
Thanks to suggestions made by Rarst, this code works like a charm.
As you can see, it does show the
Featured Image
which belongs to that post,and the output is returned in exact order as wished in the code.
It also uses some CSS to style (in the style.css) so it shows nice below
the title and above the content.
Update
Getting Notice: Undefined offset: 0
Anyone around to help me out?
If you take a look at source for
wp_get_object_terms()
which a lot of related functions use internally, there is not option for customorderby
in it. I would probably just re-sort returned data in PHP for this.However even more practically I would just retrieve terms for different taxonomies separately. What you have there looks tad fragile logically – for example what if one of terms is missing, or there are more than expected assigned by mistake? Output will go bad fast.