Get parent category(taxonomy) ID from single template of a custom post type

-Parent-category-Sub-categoryWant to get parent ID when on the current post of sub-category.Created a custom post type

-Parent-category
    -Sub-category

Want to get parent ID when on the current post of sub-category. Created a custom post type and custom taxonomies for that post type. Created a category and assigned another category to have the first category as a parent. I'm listing all posts of that parent category on the parent category page when a particular post is clicked it opens the single-custom_post_type.php and want to get the parent category ID on the single-custom_post_type.php so I can add a sidebar with all the sibling's post with the same parent category.

Any help?

$args_docs   =   array(
    'post_type'         =>  Documents::DOCUMENTS_TYPE_KEY,
    'posts_per_page'    =>  -1,
    'tax_query' => array(
        array(
            'taxonomy'  =>  DocumentsTaxonomy::DOCUMENTS_TAXONOMY_KEY,
            'field'     =>  'term_id',
            'terms'     =>  9, // <- I want here to add the parent category ID dynamically,so whenever we are currently on a post it gives that posts parent category ID
        ),
    ),
);
-Parent-category
    -Sub-category

Want to get parent ID when on the current post of sub-category. Created a custom post type and custom taxonomies for that post type. Created a category and assigned another category to have the first category as a parent. I'm listing all posts of that parent category on the parent category page when a particular post is clicked it opens the single-custom_post_type.php and want to get the parent category ID on the single-custom_post_type.php so I can add a sidebar with all the sibling's post with the same parent category.

Any help?

$args_docs   =   array(
    'post_type'         =>  Documents::DOCUMENTS_TYPE_KEY,
    'posts_per_page'    =>  -1,
    'tax_query' => array(
        array(
            'taxonomy'  =>  DocumentsTaxonomy::DOCUMENTS_TAXONOMY_KEY,
            'field'     =>  'term_id',
            'terms'     =>  9, // <- I want here to add the parent category ID dynamically,so whenever we are currently on a post it gives that posts parent category ID
        ),
    ),
);
Share Improve this question edited Apr 17, 2019 at 12:27 Gufran Hasan 6918 silver badges20 bronze badges asked Apr 17, 2019 at 12:05 TestimiUEBTestimiUEB 151 gold badge2 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Get Object Terms : Retrieves an array of WP_Term object associated with the given post, in the supplied taxonomies.

Term object ( WP_Term )offers some handy information. For example,

Gives you term slug: e.g.: term-slug-example

$slug = $term->slug;

Gives you term name: e.g. Term Name Example

$name = $term->name;

Gives you term description: e.g. This is my new cool custom term.

$desc = $term->description;

But unfortunately lacks a link value. So use Get Term Link to generate a permalink for a taxonomy term archive.

$term_link = get_term_link( $term);

So your finalcode should look like this:

$sub_cats = wp_get_object_terms( $post->ID, DocumentsTaxonomy::DOCUMENTS_TAXONOMY_KEY, ); // array of categories associated with current post


$args_docs   =   array(
    'post_type'         =>  Documents::DOCUMENTS_TYPE_KEY,
    'posts_per_page'    =>  -1,
    'tax_query' => array(
        array(
            'taxonomy'  =>  DocumentsTaxonomy::DOCUMENTS_TAXONOMY_KEY,
            'field'     =>  'term_id',
            'terms'     =>  $sub_cats[0]->parent, 
        ),
    ),
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745579736a4634181.html

相关推荐

  • Get parent category(taxonomy) ID from single template of a custom post type

    -Parent-category-Sub-categoryWant to get parent ID when on the current post of sub-category.Created a custom post type

    19小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信