从WooCommerce中的子术语中获取主术语

时间:2013-05-31 作者:Yhis

我需要展示

每当我点击子项时,主项第一子项第二子项。我的意思是,我不想显示其他主要术语,只想显示我与子术语一起选择的术语。

在里面woocommerce-template.php 我使用:

function woocommerce_content() {

    if ( is_singular( \'product\' ) ) {

        while ( have_posts() ) : the_post();?>

        <div class="three columns alpha">
            <div id="menu_commerce">
            <?php 
                    global $post;

                    $terms = get_the_terms( $post->ID, \'product_cat\' );
                    foreach ( $terms as $term ){
                    $category_id = $term->term_id;
                    $category_name = $term->name;
                    $category_slug = $term->slug;
                    echo \'<li><a href="\'. get_term_link($term->slug, \'product_cat\') .\'">\'.$category_name.\'</a></li>\';

                    }?>

            </div><!--END MENU COMMERCE-->
        </div><!--END 3 COLUMNS ALPHA-->

        <div class="thirteen columns alpha">

        <?php   woocommerce_get_template_part( \'content\', \'single-product\' );?>

        </div>

        <?php endwhile;

    } else {

        ?>

        <div class="three columns alpha">
            <div id="menu_commerce">
            <?php 

                global $wp_query;
                // get the query object
                $cat_obj = $wp_query->get_queried_object();
                if($cat_obj)    {
                    $category_name = $cat_obj->name;
                    $category_desc = $cat_obj->description;
                    $category_ID  = $cat_obj->term_id;
                    $category_slug = $cat_obj->slug;
                }
                woocommerce_subcats_from_parentcat_by_ID($category_ID)

                ?>
            </div><!--END MENU COMMERCE-->
        </div><!--END 3 COLUMNS ALPHA-->

        <div class="thirteen columns alpha">

        <div id="prod_titolo"><h1><?php woocommerce_page_title(); ?></h1></div>
        <hr style="margin-bottom:20px"/>
        <?php do_action( \'woocommerce_archive_description\' ); ?>

        <?php if ( have_posts() ) : ?>

            <?php do_action(\'woocommerce_before_shop_loop\'); ?>

            <div id="menu_grid">

            <?php woocommerce_product_loop_start(); ?>

                <?php woocommerce_product_subcategories(); ?>

                <?php while ( have_posts() ) : the_post(); ?>

                    <div class="four columns">

                    <?php woocommerce_get_template_part( \'content\', \'product\' ); ?>

                    </div><!--END 4 col-->

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>
            </div><!--END menu grid-->
它适用于单个产品,但如果我单击子项,主项将消失。

我在中使用的函数functions.php 是:

function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$args = array(
   \'hierarchical\' => 1,
   \'show_option_none\' => \'\',
   \'hide_empty\' => 0,
   \'parent\' => $parent_cat_ID,
   \'taxonomy\' => \'product_cat\'
);
$subcats = get_categories($args);
echo \'<ul class="wooc_sclist">\';

  foreach ($subcats as $sc) {
    $link = get_term_link( $sc->slug, $sc->taxonomy );
      echo \'<li><a href="\'. $link .\'">\'.$sc->name.\'</a></li>\';
  }
echo \'</ul>\';
}

你能帮帮我吗?

1 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

代码

function wc_origin_trail_ancestor( $link = false, $trail = false ) {

    if (is_product_category()) {
        global $wp_query;
        $q_obj = $wp_query->get_queried_object();
        $cat_id = $q_obj->term_id;

        $descendant = get_term_by("id", $cat_id, "product_cat");
        $descendant_id = $descendant->term_id;

        $ancestors = get_ancestors($cat_id, \'product_cat\');
        $ancestors = array_reverse($ancestors);

        $origin_ancestor = get_term_by("id", $ancestors[0], "product_cat");
        $origin_ancestor_id = $origin_ancestor->term_id;

        $ac = count($ancestors);

    } else if ( is_product() ) {

        $ancestors = get_the_terms( $post->ID, \'product_cat\' );

        $i = 0;
        foreach($ancestors as $ancestor) {
            if( $i == 0 ) {
                $origin_ancestor = $ancestor;
                $origin_ancestor_id = $origin_ancestor->term_id;
            }
        }

        $ancestors = array_reverse($ancestors);

        $ac = count($ancestors);

        $descendant = $ancestors[0];
        $descendant_id = $descendant->term_id;

        $ancestors = array_reverse(get_ancestors($descendant_id, \'product_cat\'));

    }


    $c = 1;
    if( $trail == false ){

        $origin_ancestor_term = get_term_by("id", $origin_ancestor_id, "product_cat");
        $origin_ancestor_link = get_term_link( $origin_ancestor_term->slug, $origin_ancestor_term->taxonomy );

        if($link == true) 
            echo \'<a href="\'. $origin_ancestor_link .\'">\';
        echo $origin_ancestor->name;
        if($link == true) 
            echo \'</a>\';

    }else{

        foreach ($ancestors as $ancestor) {
            $ancestor_term = get_term_by("id", $ancestor, "product_cat");
            $ancestor_link = get_term_link( $ancestor_term->slug, $ancestor_term->taxonomy );

            if($c++ == 1) 
                echo \'» \'; 
            else if($c++ != 1 || $c++ != $ac) 
                echo \' » \';

            if($link == true) 
                echo \'<a href="\'. $ancestor_link .\'">\';
            echo  $ancestor_term->name;
            if($link == true) 
                echo \'</a>\';

        }

        $descendant_term = get_term_by("id", $descendant_id, "product_cat");
        $descendant_link = get_term_link( $descendant_term->slug, $descendant_term->taxonomy );

        echo \' » \';
        if($link == true) 
            echo \'<a href="\'. $descendant_link .\'">\';
        echo $descendant->name;
        if($link == true) 
            echo \'</a>\';

    }

}
如何使用顶级,起源祖先;无链接
wc_origin_trail_ancestor();wc_origin_trail_ancestor(true);wc_origin_trail_ancestor(false,true);wc_origin_trail_ancestor(true,true);

    注释将不起作用

结束

相关推荐

Google Map在Tab Plugins的第二个选项卡上无法使用

我正在使用postTabs plugin 和Comprehensive Google Map Plugin, 我的问题是,当我在第二个选项卡上有我的谷歌地图时,地图没有按预期加载。但是如果我把它移到第一个标签上,效果会非常好。。有没有办法让地图在第二个选项卡上工作?实际上,无论我使用哪个选项卡插件,地图都不会正确加载到第二个选项卡上。。欢迎提出任何建议。谢谢:)