yaost-primary-category.php 569 B

1234567891011121314151617181920212223
  1. <?php
  2. /**
  3. * Gets Yoast SEO primary category.
  4. *
  5. * @param int $post_id ID of the product to get category of.
  6. * @return string
  7. */
  8. public function get_primary_cat( $post_id ) {
  9. $cat_name = '';
  10. if ( class_exists( 'WPSEO_Primary_Term' ) ) {
  11. $primary_cat_id = get_post_meta( $post_id, '_yoast_wpseo_primary_product_cat', true );
  12. if ( $primary_cat_id ) {
  13. $product_cat = get_term( $primary_cat_id, 'product_cat' );
  14. if ( isset( $product_cat->name ) ) {
  15. $cat_name = $product_cat->name;
  16. }
  17. }
  18. }
  19. return $cat_name;
  20. }