admin管理员组文章数量:1134248
In category display type u can choose between (default, products, subcategories, both) options. Is there any way to make an if statement that uses those values?
I would like todo something like the below but i'm having a hard time figuring out how u can check for those values.
Now i have something like the below but that requires me to do this for hundreds of categories
<?php if (is_product_category('979') || is_product_category('979')) then do something ?>
So i'm actually looking for something like the below
<?php if (category is display type ('products')) then do something ?>
So if anyone know a way to filter using display type that would be great.
Thanks in advance
In category display type u can choose between (default, products, subcategories, both) options. Is there any way to make an if statement that uses those values?
I would like todo something like the below but i'm having a hard time figuring out how u can check for those values.
Now i have something like the below but that requires me to do this for hundreds of categories
<?php if (is_product_category('979') || is_product_category('979')) then do something ?>
So i'm actually looking for something like the below
<?php if (category is display type ('products')) then do something ?>
So if anyone know a way to filter using display type that would be great.
Thanks in advance
Share Improve this question asked May 4, 2020 at 10:08 Jeroen_LJeroen_L 431 gold badge1 silver badge5 bronze badges4 Answers
Reset to default 3You can do this using the function get_term_meta
.
// Get the current category ID;
$category_id = get_queried_object_id();
// Get the meta data using the ID;
$term_vals = get_term_meta( $category_id );
// One of the values is display type, with the value at index 0;
// this can be products, subcategories, or both
if ( $term_vals[ 'display_type' ][ 0 ] === 'products' ) {
// Do your code here
}
Depending on where whoocommerce posts are stored, you could do this via basic Wordpress post queries, via:
$args = array(
'post_type' => 'your_post_type',
'category_name' => 'your_category',
'tag' => 'your_tag'
);
$arr_posts = new WP_Query($args);
if ($arr_posts->have_posts()) {
while ($arr_posts->have_posts()) {
echo $arr_posts->the_post();
// Echo out content of each post, but here you may do whatever you want
the_content();
}
} else {
echo "<p class='error'>No posts fulfill your search criteria!<p>";
}
Note that you can adapt your query parameters, by specifying additional query criteria in the $args array (or take out any of the ones I provided, if you please).
I've added an error class to your error feedback such that you can style it in CSS as you may please (color:red; etc). Again, this example works if your product posts are stored in the wp_posts database. If not, you may do something similar, by querying in the corresponding database holding your product posts.
Just found this https://wp-staging.com/in-which-database-table-is-woocommerce-storing-products/, so the code above should definitely work.
After some searching i found a solution in the code below which checks if your category display view is set in my case as products.
$cat_id = $queried_object->term_id;
$test = array(
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
array(
'key' => 'display_type',
'value' => 'products',
'compare' => 'LIKE'
)
),
);
$terms = get_terms( $test );
foreach ($terms as $product_page) {
if ($product_page->term_id === $cat_id) {
$category_value = $product_page->term_id;
} }
Now u can use something like $cat_id === $category_value Whenever a category page is set to display_type products it will return a true
If needed u can change this to any of the other options as shown below.
'value' => 'subcategories',
'value' => '', // <-- this is the default
'value' => 'both', // <-- this is both options
The answer from @Tim Steel is a good direction, but we can do it in a simpler way:
$category_id = get_queried_object_id();
if ($category_id) {
$term_vals = get_term_meta($category_id, 'display_type', true);
if ($term_vals === 'subcategories') {
echo 'We are on subcategories';
}
}
本文标签: WordPressWoocommerceuse quotcategory display typequot in if statement
版权声明:本文标题:WordPress | Woocommerce | use "category display type" in if statement 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736820934a1954280.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论