-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,
),
),
);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745579736a4634181.html
评论列表(0条)