【问题标题】:Get product category and tag terms as meta keyword in WooCommerce在 WooCommerce 中获取产品类别和标签术语作为元关键字
【发布时间】:2020-05-20 09:40:40
【问题描述】:

我一直在使用以下代码将标签和类别用作我的 wordpress 帖子的 META 关键字。

function wcs_add_meta_keywords() {
global $post;
if ( is_single() ) {
    $cats = get_the_category( $post->ID );
    $tags = get_the_tags( $post->ID );
    $keywords = '';
    foreach ( $cats as $cat ) {
        $keywords .= $cat->cat_name . ", ";
    }
    foreach ( $tags as $tag ) {
        $keywords .= $tag->name . ", ";
    }
    echo '<meta name="keywords" content="' . $keywords . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );

以下代码用于将产品描述用作 META 描述。

function wcs_add_meta_description_tag() {
global $post;
if ( is_single() ) {
    $meta = strip_tags( $post->post_content );
    $meta = strip_shortcodes( $post->post_content );
    $meta = str_replace( array("\n", "\r", "\t"), ' ', $meta );
    $meta = mb_substr( $meta, 0, 125, 'utf8' );
    echo '<meta name="description" content="' . $meta . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_description_tag' , 2 );

但现在我想在 woocommerce 中为我的产品实现相同的目标。我已经了解并知道 woocommerce 使用分类法,所以我尝试使用 get_terms() 和 product_tag,product_cat 代替 get_the_category 和 get_the_tag。但它不起作用。

谁能帮助正确使用这两个代码的变量。

提前致谢

【问题讨论】:

  • 请注意,StackOverFlow 中不允许同时提出多个问题,因此您必须删除 META 描述部分并提出一个新问题它。
  • @LoicTheAztec 所以你赢得了答案,甚至是问题。谢谢:-)

标签: php wordpress woocommerce meta taxonomy-terms


【解决方案1】:

对于单个帖子(或自定义帖子)上的 WordPress 和 WooCommerce 术语分类,您可以更好地使用wp_get_post_terms(),它允许“字段”参数指向术语“名称”,因此代码将更加紧凑和高效:

对于 WooCommerce 和 Wordpress,您将使用:

add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );
function wcs_add_meta_keywords() {
    // For WordPress single posts with categories and tags
    if ( is_single() && ! is_product() ) {
        $cats = (array) wp_get_post_terms( get_the_id(), 'category', array('fields' => 'names') );
        $tags = (array) wp_get_post_terms( get_the_id(), 'post_tag', array('fields' => 'names') );
    }
    //  For WooCommerce single product (product categories and product tags)
    elseif ( is_product() ) {
        $cats = (array) wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'names') );
        $tags = (array) wp_get_post_terms( get_the_id(), 'product_tag', array('fields' => 'names') );
    }
    if ( ! empty( $cats ) || ! empty( $tags ) ){
        echo '<meta name="keywords" content="' . implode( ', ', array_merge( $cats, $tags ) ) . '" />' . "\n";
    }
}

仅适用于 WooCommerce:

add_action( 'wp_head', 'wcs_add_meta_keywords', 2);
function wcs_add_meta_keywords() {
    if ( is_product() ) {
        $product_cats = (array) wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'names') );
        $product_tags = (array) wp_get_post_terms( get_the_id(), 'product_tag', array('fields' => 'names') );
    }
    if ( ! empty( $product_cats ) || ! empty( $product_tags ) ){
        echo '<meta name="keywords" content="' . implode( ', ', array_merge( $product_cats, $product_tags ) ) . '" />' . "\n";
    }
}

代码在您的活动子主题(活动主题)的functions.php 文件中。经过测试并且可以工作。

【讨论】:

  • 无法保存..它给出错误 - 出了点问题。您的更改可能尚未保存。请重试。
  • 我是从 cpanel 做的,现在成功了。您能否也请帮助和指导有关 woocommerce 的描述元标记
猜你喜欢
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 2019-03-05
相关资源
最近更新 更多