Php _类别功能工作不正常;类别没有显示它们应该在哪里
我在我的公文包页面中有这个相关的帖子部分,我正试图用“theu category”函数为每个帖子生成类别。在这种情况下,左边的帖子有类别“Photo,Test”,其余的有类别“Blog” 该功能似乎工作正常,但正如您在所附图像中看到的,类别显示在标题上方 我希望在“listing_meta”块中生成类别。(评论旁)Php _类别功能工作不正常;类别没有显示它们应该在哪里,php,wordpress,function,categories,Php,Wordpress,Function,Categories,我在我的公文包页面中有这个相关的帖子部分,我正试图用“theu category”函数为每个帖子生成类别。在这种情况下,左边的帖子有类别“Photo,Test”,其余的有类别“Blog” 该功能似乎工作正常,但正如您在所附图像中看到的,类别显示在标题上方 我希望在“listing_meta”块中生成类别。(评论旁) .esc_html(获取时间(获取选项('date_格式'))。' “.”由“,”gt3建筑商“.” “._类(”,“)” 有什么解决办法吗 多谢各位 ------------
.esc_html(获取时间(获取选项('date_格式'))。'
“.”由“,”gt3建筑商“.”
“._类(”,“)”
有什么解决办法吗
多谢各位
-------------------------编辑-------------------------------
我尝试了函数“get_the_category”,但现在它返回所有类别作为数组,如图所示
<div class="blog_content">
<div class="listing_meta">
<span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>
<span class="blog_post_author">'. __('by', 'gt3_builder') .' <a href="'.get_author_posts_url( get_the_author_meta('ID')).'" class="text-capitalize">'.get_the_author_meta('display_name').'</a></span>
<span class="comments"><a href="' . get_comments_link() . '">'. get_comments_number(get_the_ID()) .'</a></span>
<span>'. get_the_category() .'</span>
</div>
<div class="blog_post_title"><h2><a href="'. get_permalink() .'">' . get_the_title() . '</a></h2><div class="blog_post_format_label"></div></div>
</div>
.esc_html(获取时间(获取选项('date_格式'))。'
“.”由“,”gt3建筑商“.”
“.获取_类别()
这就是你想要的功能。如果你使用
the_category()
它将立即回显,然后在您的其余内容之前回显
---------------编辑--------------
如果返回多个类别,则可以使用foreach将数组转换为以逗号分隔的类别列表:
//get all the categories
$categories = get_the_category();
//begin to assemble our string/html we want return/output
$returnedString = '<div class="blog_content">
<div class="listing_meta">
<span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>
<span class="blog_post_author">'. __('by', 'gt3_builder') .' <a href="'.get_author_posts_url( get_the_author_meta('ID')).'" class="text-capitalize">'.get_the_author_meta('display_name').'</a></span>
<span class="comments"><a href="' . get_comments_link() . '">'. get_comments_number(get_the_ID()) .'</a></span>
<span>';
//loop over the array of category objects
foreach($categories as $category){
//append each category name to the returned string
$returnedString .= $category->name . ',';
}
//add the remaining HTML
$returnedString .= '</span>
</div>
<div class="blog_post_title"><h2><a href="'. get_permalink() .'">' . get_the_title() . '</a></h2><div class="blog_post_format_label"></div></div>
</div>';
//获取所有类别
$categories=get_the_categories();
//开始组装我们想要返回/输出的字符串/html
$returnedString='1!'
.esc_html(获取时间(获取选项('date_格式'))。'
“.”由“,”gt3建筑商“.”
';
//在类别对象数组上循环
foreach($categories作为$category){
//将每个类别名称附加到返回的字符串中
$returnedString.=$category->name.',';
}
//添加剩余的HTML
$returnedString.='
';
谢谢。我试过了,他们完全在街区里。不过,我还有一个问题。现在,它以数组的形式返回所有类别,如第二幅图所示……(我已经更新了帖子)我对PHP了解不多,我会感谢你的帮助。你没有包括HTML周围发生的所有代码,因为看起来HTML是从函数返回的,或者被设置为变量,所以我会更新我的答案,但你得稍微适应一下。
the_category()
//get all the categories
$categories = get_the_category();
//begin to assemble our string/html we want return/output
$returnedString = '<div class="blog_content">
<div class="listing_meta">
<span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>
<span class="blog_post_author">'. __('by', 'gt3_builder') .' <a href="'.get_author_posts_url( get_the_author_meta('ID')).'" class="text-capitalize">'.get_the_author_meta('display_name').'</a></span>
<span class="comments"><a href="' . get_comments_link() . '">'. get_comments_number(get_the_ID()) .'</a></span>
<span>';
//loop over the array of category objects
foreach($categories as $category){
//append each category name to the returned string
$returnedString .= $category->name . ',';
}
//add the remaining HTML
$returnedString .= '</span>
</div>
<div class="blog_post_title"><h2><a href="'. get_permalink() .'">' . get_the_title() . '</a></h2><div class="blog_post_format_label"></div></div>
</div>';