Wordpress自定义帖子类型如何在单个帖子中显示分类描述 [英] Wordpress custom post type how to display taxonomy description in single post

查看:27
本文介绍了Wordpress自定义帖子类型如何在单个帖子中显示分类描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个帖子的前端显示自定义分类项目描述.

以下情况:

  1. 为Weine"创建了 CPT
  2. 添加了分类法winzer"
  3. 将以下代码添加到我的functions.php 以显示winzer"在单个帖子中使用短代码对其进行描述的分类项目:

function wpb_catlist_desc() {$string = '

';$catlist = get_terms('winzer');如果(!空($catlist)){foreach ( $catlist as $key => $item ) {$string .= '

'.$item->name .'<br/>';$string .= ''.$item->description .'</em></div>';}}$string .= '</div>';返回 $string;}add_shortcode('wpb_categories', 'wpb_catlist_desc');

代码运行良好,但它显示了我在Winzer"分类法中创建的所有项目.

我只想显示与单个帖子相关的项目.

关于如何更改代码以完成这项工作的任何想法?

干杯!!!!

解决方案

您可以使用 get_the_terms() 而不是使用 get_terms().所以你可以做这样的事情:

add_shortcode('wpb_categories', 'wpb_catlist_desc');$the_id_of_current_post = get_the_ID();//获取当前帖子的id//将 id 传递给您的函数函数 wpb_catlist_desc($the_id_of_current_post){$string = '

';$$catlist = get_the_terms( $the_id_of_current_post, 'winzer');如果(!空($catlist)){foreach ( $catlist 作为 $cat ) {$string .= '

'.$cat-> 名称.'<br/>';$string .= ''.$cat-> 描述.'</em></div>';}}$string .= '</div>';返回 $string;}

或者另一种方式是这样的:

add_shortcode('wpb_categories', 'wpb_catlist_desc');函数 wpb_catlist_desc(){全球 $post;$the_id_of_current_post = $post->ID;//获取当前帖子的id$string = '

';$$catlist = get_the_terms( $the_id_of_current_post, 'winzer');如果(!空($catlist)){foreach ( $catlist 作为 $cat ) {$string .= '

'.$cat-> 名称.'<br/>';$string .= ''.$cat-> 描述.'</em></div>';}}$string .= '</div>';返回 $string;}

让我知道你是否能够让它工作!

I´m trying to display a custom taxonomy item description in the frontend on a single post.

Following situation:

  1. Created a CPT for "Weine"
  2. Added a taxonomy "winzer"
  3. Added the following code to my functions.php to display the "winzer" taxonomy item with its description in the single post using a shortcode:

function wpb_catlist_desc() { 
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
  foreach ( $catlist as $key => $item ) {
    $string .= '<div>'. $item->name . '<br />';
    $string .= '<em>'. $item->description . '</em></div>';
  }
}
$string .= '</div>';
 
return $string; 
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');

The code is working well, but it’s displaying all the items I created in the "Winzer" Taxonomy.

I just want to display the Item which is related to the single post.

Any ideas on how to change the code to get this done?

Cheers!!!!

解决方案

You could use get_the_terms() instead of using get_terms(). So you could do something like this:

add_shortcode('wpb_categories', 'wpb_catlist_desc');

$the_id_of_current_post = get_the_ID(); // get the id of the current post

// pass the id to your function
function wpb_catlist_desc($the_id_of_current_post) 
{ 
  $string = '<div>';
  
  $$catlist = get_the_terms( $the_id_of_current_post, 'winzer' );

  if ( ! empty( $catlist ) ) {
    foreach ( $catlist as $cat ) {
      $string .= '<div>'. $cat->name . '<br />';
      $string .= '<em>'. $cat->description . '</em></div>';
    }
  }

  $string .= '</div>';
  
  return $string; 
}

Or an alternative way would be something like this:

add_shortcode('wpb_categories', 'wpb_catlist_desc');

function wpb_catlist_desc() 
{ 
  global $post;

  $the_id_of_current_post = $post->ID; // get the id of the current post

  $string = '<div>';
  
  $$catlist = get_the_terms( $the_id_of_current_post, 'winzer' );

  if ( ! empty( $catlist ) ) {
    foreach ( $catlist as $cat ) {
      $string .= '<div>'. $cat->name . '<br />';
      $string .= '<em>'. $cat->description . '</em></div>';
    }
  }

  $string .= '</div>';
  
  return $string; 
}

Let me know if you were able to get this to work!

这篇关于Wordpress自定义帖子类型如何在单个帖子中显示分类描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆