我想在帖子类型的帖子标题之外显示帖子类型的分类术语,并用“in”或“post in”文本字符串分隔。
到目前为止我所做的尝试:
$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>\';
所有代码只会产生空白,没有显示任何内容。我做错了什么?
如果我把var_dump( $terms );
在if语句之前显示:
array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }
WP调试报告
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
if语句之前的行末尾有一个正确的分号,所以我不知道为什么会出现这种错误。
这是嵌入代码的短代码
// 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\' );