最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I display the taxonomy term alongside the post type post title?

programmeradmin3浏览0评论

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
  • Please check the function get_the_term_list. You will simply have to use get_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
  • You have a dollar sign that shouldn't be there: $get_the_ID(). – Jacob Peattie Commented Apr 16, 2020 at 5:58
  • I removed the dollar sign, and only the titles are displayed. No terms shown. – Lou Crazyblue Commented Apr 16, 2020 at 17:00
  • Sorry everyone for my stupidity, the posts were not assigned to the taxonomy. Solved! – Lou Crazyblue Commented Apr 16, 2020 at 18:04
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Solved. The posts were not assigned to the taxonomy.

发布评论

评论列表(0)

  1. 暂无评论