// --------- Help Center Brands Menu (functions.php) ---------
// Add this to your Astra child theme's functions.php (append at end)
if ( ! defined( 'ABSPATH' ) ) {
return;
}
/**
* Shortcode: [help_brands_menu]
* Outputs a nested UL of top-level 'model' terms (brands) and their child terms (models).
* Adds "Other" when posts are assigned directly to the brand (exclude children).
*/
add_shortcode( 'help_brands_menu', 'hw_help_brands_menu_shortcode' );
function hw_help_brands_menu_shortcode( $atts ) {
// Basic environment checks
if ( ! taxonomy_exists( 'model' ) || ! post_type_exists( 'help' ) ) {
return '
Help menu: required CPT help or taxonomy model not found.
';
}
// Determine current queried term (if any) to mark active and expand branch
$current_term_id = 0;
if ( is_tax( 'model' ) ) {
$queried = get_queried_object();
if ( isset( $queried->term_id ) ) {
$current_term_id = (int) $queried->term_id;
}
}
$menu_id = 'help-brands-menu-' . uniqid();
$html = '';
return $html;
}
/**
* Helper: return number of posts of post_type 'help' assigned DIRECTLY to a term (exclude children)
*/
function hw_get_brand_direct_post_count( $brand_term_id ) {
// Quick check
if ( empty( $brand_term_id ) ) {
return 0;
}
$q = new WP_Query( array(
'post_type' => 'help',
'tax_query' => array(
array(
'taxonomy' => 'model',
'terms' => intval( $brand_term_id ),
'field' => 'term_id',
'include_children' => false,
),
),
'posts_per_page' => 1, // we only need to know if any exist, but to show count we need actual count
'no_found_rows' => true,
'fields' => 'ids',
) );
// Because we used posts_per_page=1 & no_found_rows=true, we don't have total. For accurate count, use get_objects_in_term fallback:
if ( $q->have_posts() ) {
// We need accurate count for the UI; use get_objects_in_term which returns object IDs assigned exactly to the term (does not include children)
$objects = get_objects_in_term( intval( $brand_term_id ), 'model' );
if ( is_array( $objects ) ) {
// filter by post_type 'help'
$count = 0;
foreach ( $objects as $post_id ) {
$post = get_post( $post_id );
if ( $post && $post->post_type === 'help' && $post->post_status === 'publish' ) {
$count++;
}
}
return $count;
}
return 0;
}
// No posts found quickly
return 0;
}
/**
* Add query var 'brand_other' so WP recognizes it.
*/
add_filter( 'query_vars', 'hw_add_brand_other_query_var' );
function hw_add_brand_other_query_var( $vars ) {
$vars[] = 'brand_other';
return $vars;
}
/**
* pre_get_posts handler: if brand_other=1 on a model archive, restrict main query to posts directly assigned to that brand term.
*/
add_action( 'pre_get_posts', 'hw_filter_brand_other_pre_get_posts' );
function hw_filter_brand_other_pre_get_posts( $query ) {
// Only affect front end main query
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// Check if on model taxonomy archive and brand_other flag is present
if ( is_tax( 'model' ) && get_query_var( 'brand_other', 0 ) ) {
$term = get_queried_object();
if ( ! $term || empty( $term->term_id ) ) {
return;
}
// Force post_type to 'help' (only for this archive)
$query->set( 'post_type', 'help' );
// Build tax_query: only posts directly in the brand term (exclude children)
$tax_query = array(
array(
'taxonomy' => 'model',
'terms' => intval( $term->term_id ),
'field' => 'term_id',
'include_children' => false,
),
);
$query->set( 'tax_query', $tax_query );
// Optional: ensure posts are published
$query->set( 'post_status', 'publish' );
}
}
Warning: Cannot modify header information - headers already sent by (output started at /home/xs199533/skylinkjapan.com/public_html/mgmt/wp-content/themes/astra-child/functions.php:81) in /home/xs199533/skylinkjapan.com/public_html/mgmt/wp-includes/pluggable.php on line 1450
Warning: Cannot modify header information - headers already sent by (output started at /home/xs199533/skylinkjapan.com/public_html/mgmt/wp-content/themes/astra-child/functions.php:81) in /home/xs199533/skylinkjapan.com/public_html/mgmt/wp-includes/pluggable.php on line 1453