How do I display the taxonomy term alongside the post type post title?
-
I would like to display the taxonomy term of the post type post besides the post type post title, separated by the “in” text string.
There are two issues:
1. only “array” is displayed instead of the term
2. I don’t know how to code the “in” text string correctly, between the term and the title (they should be in the same row)Here is the (wrong) code in question:
$output .= '<div>' . get_the_title() . '</div>'; in $output .= '<div>' . wp_get_post_terms( $post_id, $taxonomy = 'itemscategories') . '</div>';
Embedded in this shortcode:
function myprefix_custom_grid_shortcode( $atts ) { // Parse your shortcode settings with it's defaults $atts = shortcode_atts( array( 'posts_per_page' => '-1', 'term' => '' ), $atts, 'myprefix_custom_grid' ); $user_id = userpro_get_view_user( get_query_var('up_username') ); // Extract shortcode atributes extract( $atts ); // Define output var $output = ''; // Define query $query_args = array( 'author'=> $user_id, 'post_type' => 'items', // Change this to the type of post you want to show 'posts_per_page' => $posts_per_page, ); // Query by term if defined if ( $term ) { $query_args['tax_query'] = array( array( 'taxonomy' => 'category', 'field' => 'ID', 'terms' => $term, ), ); } // Query posts $custom_query = new WP_Query( $query_args ); // Add content if we found posts via our query if ( $custom_query->have_posts() ) { // Open div wrapper around loop $output .= '<div>'; // Loop through posts while ( $custom_query->have_posts() ) { // Sets up post data so you can use functions like get_the_title(), get_permalink(), etc $custom_query->the_post(); // This is the output for your entry so what you want to do for each post. $output .= '<div>' . get_the_title() . '</div>'; in $output .= '<div>' . wp_get_post_terms( $post_id, $taxonomy = 'itemscategories') . '</div>'; } // Close div wrapper around loop $output .= '</div>'; // Restore data wp_reset_postdata(); } // Return your shortcode output return $output; } add_shortcode( 'myprefix_custom_grid', 'myprefix_custom_grid_shortcode' );
- The topic ‘How do I display the taxonomy term alongside the post type post title?’ is closed to new replies.