我想从自定义类型的帖子id中获取类别id。我有帖子id,但我无法获取它的类别id。
我已经使用了这么多代码,但它不工作,可能是由于自定义的帖子类型。
$category = get_the_category( $post->ID );
有什么建议吗?我想从自定义类型的帖子id中获取类别id。我有帖子id,但我无法获取它的类别id。
我已经使用了这么多代码,但它不工作,可能是由于自定义的帖子类型。
$category = get_the_category( $post->ID );
有什么建议吗?wp_get_post_categories
只能获取帖子类别而不是自定义帖子的类别,请尝试以下操作:
$category = get_the_terms( $post->ID, \'custom-taxonomy-here\' );
foreach ( $category as $cat){
echo $cat->name;
}
检查this link您的自定义帖子类型是否支持标准类别分类法?如果自定义帖子类型中使用的类别是自定义分类法,而不是标准类别分类法,则应get_the_terms()
而不是get_the_category()
.
$categories = get_the_terms($post->ID, "my-custom-taxonomy");
此解决方案适用于我:
global $wpdb;
// get all category id\'s based on post id
$result = $wpdb->get_results( " select term_taxonomy_id from " . $wpdb->prefix . "term_relationships where object_id = \'" . $post_id . "\' " );
$cats_ids_array = [];
foreach ( $result as $c ) {
$cats_ids_array[] = $c->term_taxonomy_id;
}
我想做的是显示“新闻”类别中的每一篇文章(在我的例子中,“新闻”类别的ID是13),每页5篇文章。页面上会显示分页链接,但当我单击“上一页”链接时,它只会显示类别中的前5篇文章。页面新闻的内容。php:<?php get_header(); ?> <h1 class=\"title\">Latest News</h1> <?php /*$args = array(\'cat\' => 13); $posts =