admin管理员组文章数量:1023838
-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
1 Answer
Reset to default 1Get 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,
),
),
);
-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
1 Answer
Reset to default 1Get 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,
),
),
);
本文标签: Get parent category(taxonomy) ID from single template of a custom post type
版权声明:本文标题:Get parent category(taxonomy) ID from single template of a custom post type 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745579735a2157236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论