2013-03-08 5 views
20

내 WC 제품 페이지의 경우 일부 맞춤 스타일링을 수행 할 수 있도록 body 태그에 클래스를 추가해야합니다. 여기에 내가 만들 함수는 ...WooCommerce - 제품 페이지 카테고리 가져 오기

function my_add_woo_cat_class($classes) { 

    $wooCatIdForThisProduct = "?????"; //help! 

    // add 'class-name' to the $classes array 
    $classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct; 
    // return the $classes array 
    return $classes; 
} 

//If we're showing a WC product page 
if (is_product()) { 
    // Add specific CSS class by filter 
    add_filter('body_class','my_add_woo_cat_class'); 
} 

...하지만 WooCommerce cat ID는 어떻게 얻을 수 있습니까?

답변

52

WC 제품은 하나 이상의 WC 범주에 속할 수 있습니다. 하나의 WC 카테고리 ID를 얻고 싶다고 가정합니다.

global $post; 
$terms = get_the_terms($post->ID, 'product_cat'); 
foreach ($terms as $term) { 
    $product_cat_id = $term->term_id; 
    break; 
} 

WooCommerce 플러그인의 "templates/single-product /"폴더에서 meta.php 파일을 확인하십시오.

<?php echo $product->get_categories(', ', '<span class="posted_in">' . _n('Category:', 'Categories:', sizeof(get_the_terms($post->ID, 'product_cat')), 'woocommerce') . ' ', '.</span>'); ?> 
+0

이 코드를 "price.php"에 추가하면 단일 제품 페이지를 방문 할 때 다음과 같은 오류 메시지가 나타납니다. 경고 : foreach()에 잘못된 인수가 제공되었습니다. 내가 잘못했거나이 코드가 현재 WC 버전과 호환되지 않습니까? – drake035

+0

특정 카테고리를 제외하는 방법이 있습니까? – JacobTheDev

+0

이렇게하면 제품 슬러그를 가져 와서 새 코멘트를 저장 한 후 카테고리 목록으로 리디렉션 할 수있었습니다. 어떤 도움이 있다면 이것은 내 질문이었습니다. http://stackoverflow.com/questions/42014311/wordpress-get-the-category-returns-empty –

0

감사 상자. MyStile Theme을 사용하고 있으며 검색 결과 페이지에 제품 카테고리 이름을 표시해야했습니다. 이 기능을 내 자식 테마 functions.php에 추가했습니다.

다른 사람들에게 도움이되기를 바랍니다.

/* Post Meta */ 


if (!function_exists('woo_post_meta')) { 
    function woo_post_meta() { 
     global $woo_options; 
     global $post; 

     $terms = get_the_terms($post->ID, 'product_cat'); 
     foreach ($terms as $term) { 
      $product_cat = $term->name; 
      break; 
     } 

?> 
<aside class="post-meta"> 
    <ul> 
     <li class="post-category"> 
      <?php the_category(', ', $post->ID) ?> 
         <?php echo $product_cat; ?> 

     </li> 
     <?php the_tags('<li class="tags">', ', ', '</li>'); ?> 
     <?php if (isset($woo_options['woo_post_content']) && $woo_options['woo_post_content'] == 'excerpt') { ?> 
      <li class="comments"><?php comments_popup_link(__('Leave a comment', 'woothemes'), __('1 Comment', 'woothemes'), __('% Comments', 'woothemes')); ?></li> 
     <?php } ?> 
     <?php edit_post_link(__('Edit', 'woothemes'), '<li class="edit">', '</li>'); ?> 
    </ul> 
</aside> 
<?php 
    } 
} 


?> 
2

내 테마 디렉토리의 woocommerce 폴더에있는 content-single-popup.php에서이 코드 줄을 문자 그대로 스트라이프합니다.

내가 작업하고있는 테마에는 woocommerce가 통합되어 있기 때문에 이것이 내 솔루션이었습니다.

+0

고맙습니다, 카테고리 이름 대신 이름 대신 카테고리 ID를 어떻게 가져 옵니까? – DigitCart

관련 문제