Wordpress Woocommerce PHP Tips

I am working with the woo commerece plugin and I would like the title under the title of each product. The style and format are sorted, however I want a specific category to appear in the subtitle section. I managed to display all categories, but I want to narrow it down to one category, which is under the parent category. Below is the code I'm using, can anyone suggest how I could show any child category selected under the parent category. Thanks

<?php
/**
 * Single Product title
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product;

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
?>

<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Artist:', 'Artist:', $cat_count, 'woocommerce' ) . ' ', '.</span>' ); ?>

Here's what happened:

([0] = > stdClass Object ([term_id] = > 59 [name] = > [slug] = > colorful [term_group] = > 0 [term_taxonomy_id] = > 59 [] = > product_cat [description] = > [parent] = > 115 [count] = > 21 [filter] = > raw) [1] = > stdClass Object ([term_id] = > 96 [name] = > [slug] = > karen-grant [term_group] = > 0 [term_taxonomy_id] = > 96 [] = > product_cat [description] = > [parent] = > 90 [count] = > 5 [filter] = > raw) [2] = > stdClass Object ( [term_id] = > 69 [name] = > [slug] = > [term_group] = > 0 [term_taxonomy_id] = > 69 [taxonomy] = > product_cat [description] = > [parent] = > 115 [count] = > 35 [filter] = > raw) [3] = > stdClass Object ([term_id] = > 71 [name] = > Nature [slug] = > nature [term_group] = > 0 [term_taxonomy_id] = > 71 [] = > product_cat [description] = > [parent] = > 115 [count] = > 20 [filter] = > raw))

<?php

/**
 * Single Product title
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


global $post, $product;

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
?>


<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php 


    global $post, $product;

    $cat_array = array();
    $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

    foreach($term_list as $cat_list)
    {

        array_push($cat_array, $cat_list->term_id);

    }

    $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

   $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

   $final_result = array_intersect($cat_array,$termchildren); print_r($final_result);

    $new_cat_id = $final_result[0];

    $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

    $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

    $name = $term->name; //Store it into an varialbe

    echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
?>

+4
1

:

<?php 

    global $post, $product;

    $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

    $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

    $cat_url = get_term_link ($cat_id, 'product_cat'); //get link of parent ID

    $term = get_term( $cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

    $name = $term->name; //Store it into an varialbe

    echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";

?>

:

WooCommerce , , , "" .

, - .

:

    <?php 

        global $post, $product;

        $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

        $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

       $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

        $new_cat_id = $termchildren[2];

        $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

        $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

        $name = $term->name; //Store it into an varialbe

        echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";

    ?>

(02 2015 .)

    <?php 

        global $post, $product;

        $cat_array = array();
        $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

        foreach($term_list as $cat_list)
        {

            array_push($cat_array, $cat_list->term_id);

        }

        $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

       $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

       $final_result = array_intersect($cat_array,$termchildren);

       $final_cat_result = array_values($final_result);

        $new_cat_id = $final_cat_result[0];

        $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

        $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

        $name = $term->name; //Store it into an varialbe

        echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
    ?>

: 1 $post $product . , , , .

: 2 **. .

: 3 wp_get_post_terms ( woocommerce - ). , , ID, name .. ..

: 4 , . term_id. array_push, 2. term_id.

: 9 get_term_children, , term ID fixed.It .

: 10 array_intersect . ( , ).

: 11 array_values . ( , :))

: 12 , one , term ID. ( it.Now ID)

: 13 .

: 15 , 14, .

: 16 , !

+5

Source: https://habr.com/ru/post/1569473/


All Articles