I am trying to do something I thought would be fairly simple. But it's not working, and has me stumped as to why. Any insight would be appreciated.
I have a custom /woocommerce/archive-product.php
page in a theme. It works fine, and displays wherever WC product pages are called.
I am trying to add a Worpdress conditional to it. As follows:
if ( is_page( 'Bangles','bangles' ) ) {
CODE TO RUN
} else {
OTHER CODE TO RUN
}
Yet it never tests as true.
The page url I was expecting it to come back as true for is site.org/product-categories/bangles
. "Bangles" in this case is the product category. It's also the title of the page.
I figured the Bangles
(capital B) variant might get picked up through the title test this conditional does, and the bangles
version would get picked up by the page name slug test. One, other, nor both work as expected.
In every case OTHER CODE TO RUN
is what will run.
Am I over looking something? Is there any chance this conditional only tests for actual Wordpress Pages, as opposed to Woocommerce product archive pages?
In case it helps, below is the surround code in which this conditional has been placed.
<?php
if ( function_exists( 'wc_the_product_table' ) ) {
if (is_page('Bangles','bangles')) {
echo "test";
} else {
$shortcode = '[product_table columns="image:blank,sku,name,weight,dimensions,price:Est. Cost,add-to-cart:Add?" sort="sku" variations="separate" links="none" filters="true" cart_button="checkbox" priorities="1,6,2,4,3,1" ajax_cart="true" show_footer="true" paging_type="full_numbers" product_limit="600" rows_per_page="50"]';
}
/**
* Don't modify anything below here unless you know what you're doing!
*/
$args = shortcode_parse_atts( str_replace( array( '[product_table ', ']' ), '', $shortcode ) );
if ( is_product_category() ) {
// Product category archive
$args['category'] = get_queried_object_id();
} elseif ( is_product_tag() ) {
// Product tag archive
$args['tag'] = get_queried_object_id();
} elseif ( is_product_taxonomy() ) {
// Other product taxonomy archive
$term = get_queried_object();
$args['term'] = "{$term->taxonomy}:{$term->term_id}";
} elseif ( is_post_type_archive( 'product' ) && get_query_var( 's' ) ) {
// Product search results page
add_filter( 'wc_product_table_run_in_search', '__return_true' );
if ( have_posts() ) {
global $wp_query;
$args['include'] = wp_list_pluck( $wp_query->posts, 'ID' );
} else {
// Force 'nothing found' message if no posts in query
$args['include'] = -1;
}
}
// Display the product table
wc_the_product_table( $args );
} elseif ( have_posts() ) {
UPDATE:
So, I've since found a solution to achieve what I was wanting to achieve. I would, however, still like to understand why the above did not work. I'd like to know what I've misunderstood about how is_page
functions.
The solution I found was to instead use the has_term
conditional. In my case, I've used, if ( has_term ('bangles','product_cat')) {
and this has done the trick.
As pointed out by Jacob, my update was using the wrong approach. With his help, what I've come up with is: (which tests whether the current product category is the child of either of two parents)
$id = get_queried_object_id();
$ancestors = get_ancestors( $id, 'product_cat', 'taxonomy' );
if (in_array("94", $ancestors) || in_array("19", $ancestors)) {
echo "true";
} else {
echo "false";
}