I would like to display the taxonomy term of the post type post besides the post type post title, separated by the “in” or "Posted in" text string.
What i tried so far:
$output .= '<div>' . get_the_title() . '</div>';
$terms = wp_get_post_terms($post->ID, 'itemscategories', array('fields' => 'names'));
echo implode(', ', $terms);
$output .= '<div>' . get_the_title() . '</div>';
$terms = wp_get_post_terms($post->ID, 'itemscategories', array('fields' => 'names'));
if ( ! empty( $terms ) ) {
echo $terms[0];
}
$output .= '<div>' . get_the_title() . '</div>';
$terms = wp_get_post_terms($post->ID, 'itemscategories', array('fields' => 'names'));
foreach( $terms as $name ) {
echo $name.'<br />';
}
$terms = wp_get_post_terms( $post_id, 'itemscategories' );
if ( $terms && ! is_wp_error( $terms ) ) {
$output .= '<div>' . esc_html( get_the_title() ) . ' ' . esc_html__( 'Posted in:', 'text_domain' ) . ' ' . esc_html( $terms[0]->name ) . '</div>';
}
$output .= '<div>' . esc_html( get_the_title() ) . get_the_term_list( $post_id, 'itemscategories', 'Posted in: ', ', ' ) . '</div>';
All the codes just result in white space, nothing displayed. What am I doing wrong here?
If I put var_dump( $terms );
before the if statement, this is shown:
array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }
WP debug reports Parse error: syntax error, unexpected 'if' (T_IF) in /home/username(edited_by_OP)/public_html/wp-content/themes/total-child-theme-master/functions.php on line 543
There is a proper semicolon at the end of the row before the if statement, so I don't know why this error.
This is the shortcode where the code is embedded
// 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.
$terms = wp_get_post_terms( $post_id, 'itemscategories' );
if ( $terms && ! is_wp_error( $terms ) ) {
$output .= '<div>' . esc_html( get_the_title() ) . ' ' . esc_html__( 'Posted in:', 'text_domain' ) . ' ' . esc_html( $terms[0]->name ) . '</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' );
I would like to display the taxonomy term of the post type post besides the post type post title, separated by the “in” or "Posted in" text string.
What i tried so far:
$output .= '<div>' . get_the_title() . '</div>';
$terms = wp_get_post_terms($post->ID, 'itemscategories', array('fields' => 'names'));
echo implode(', ', $terms);
$output .= '<div>' . get_the_title() . '</div>';
$terms = wp_get_post_terms($post->ID, 'itemscategories', array('fields' => 'names'));
if ( ! empty( $terms ) ) {
echo $terms[0];
}
$output .= '<div>' . get_the_title() . '</div>';
$terms = wp_get_post_terms($post->ID, 'itemscategories', array('fields' => 'names'));
foreach( $terms as $name ) {
echo $name.'<br />';
}
$terms = wp_get_post_terms( $post_id, 'itemscategories' );
if ( $terms && ! is_wp_error( $terms ) ) {
$output .= '<div>' . esc_html( get_the_title() ) . ' ' . esc_html__( 'Posted in:', 'text_domain' ) . ' ' . esc_html( $terms[0]->name ) . '</div>';
}
$output .= '<div>' . esc_html( get_the_title() ) . get_the_term_list( $post_id, 'itemscategories', 'Posted in: ', ', ' ) . '</div>';
All the codes just result in white space, nothing displayed. What am I doing wrong here?
If I put var_dump( $terms );
before the if statement, this is shown:
array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }
WP debug reports Parse error: syntax error, unexpected 'if' (T_IF) in /home/username(edited_by_OP)/public_html/wp-content/themes/total-child-theme-master/functions.php on line 543
There is a proper semicolon at the end of the row before the if statement, so I don't know why this error.
This is the shortcode where the code is embedded
// 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.
$terms = wp_get_post_terms( $post_id, 'itemscategories' );
if ( $terms && ! is_wp_error( $terms ) ) {
$output .= '<div>' . esc_html( get_the_title() ) . ' ' . esc_html__( 'Posted in:', 'text_domain' ) . ' ' . esc_html( $terms[0]->name ) . '</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' );
Share
Improve this question
asked Apr 15, 2020 at 21:25
Lou CrazyblueLou Crazyblue
112 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 0Solved. The posts were not assigned to the taxonomy.
get_the_term_list
. You will simply have to useget_the_term_list( get_the_ID(), 'itemscategories', 'Posted in:' );
– Shazzad Commented Apr 16, 2020 at 0:00$output .= '<div>' . esc_html( get_the_title() ) . get_the_term_list( $get_the_ID(), 'itemscategories', 'Posted in: ', ', ' ) . '</div>';
It does not work unfortunately. It results in white space displayed and it breaks the layout of the profile page. – Lou Crazyblue Commented Apr 16, 2020 at 1:51$get_the_ID()
. – Jacob Peattie Commented Apr 16, 2020 at 5:58