WooCommerce - 使用 Functions 设置子术语时分配父术语.php

WooCommerce - Assign parent term when child term is set using Functions.php

提问人:Andrew Hill 提问时间:10/12/2023 最后编辑:user3783243Andrew Hill 更新时间:10/12/2023 访问量:30

问:

TL:DR - 尝试使用函数 .php 自动将父类别分配给 Woocommerce 中的产品,其中产品主要类别设置为子类别。PS:对于这篇文章中的格式问题,我们深表歉意。我不太清楚我哪里出了问题!

第一次在这里发帖。我在 Stack Overflow 上看到了这篇帖子,标题为“WooCommerce - 设置子项时分配父项”;在代码方面,我是一个新手,但我并不完全愚蠢。我在我的函数 .php 文件中插入了这篇文章中的代码,但绝对没有任何反应。不过,该帖子已有 9 年的历史了,我不知道是否有任何更新会阻止它现在工作。

我在 Stack Exchange 上找到了另一篇帖子,这是在寻求相同的结果,但针对的是 WordPress 帖子,而不是产品。我以为我有解决方案,因为我能够弄清楚如何更改另一个函数的代码,以便它将标题分配给 Rankmath SEO 插件中的重点关键字,以便它适用于产品而不是帖子。我开始工作了,然后......无。

我正在使用的代码可以在我链接到的帖子中找到,但主要是这些: 9年前Woocommerce的第一段代码:

function set_product_parent_categories( $post_id ) {
    $term_ids = wp_get_post_terms( $post_id, 'product_cat' );
    foreach( $term_ids as $term_id ) {
        if( $term_id->parent > 0 ) {
            // Assign product to the parent category too.
            wp_set_object_terms( $post_id, $term_id->parent, 'product_cat' );
        }
    }
}
add_action( 'woocommerce_update_product', __NAMESPACE__.'\\set_product_parent_categories', 10, 1 );

Stack Exchange 中与帖子相关的第二段代码:

$_query = new WP_Query( array(
    'post_type' => 'post',
    'posts_per_page' => -1,
) );
function complete_parent_category( $term_id, $dep=array() ) {
    $category = get_term( $term_id, 'category' );
    $dep[ $category->term_id ] = $category->slug;
    if( $category->parent ) {
    $dep = complete_parent_category( $category->parent, $dep );
    }
    return $dep;
}
echo '<pre>';
while( $_query->have_posts() ) {
$_query->next_post();
echo '<br /><strong>'.$_query->post->post_title .'</strong> |>> ';
$cat_post = array();
$cats = get_the_category( $_query->post->ID );
if( count( $cats ) ) {
    foreach ($cats as $cat) {
    $cat_post[ $cat->term_id ] = $cat->slug;
    if( $cat->parent == 0 ) continue; 
    $category_object = get_term( $cat, 'category' );
    $cat_post = complete_parent_category( $cat->parent, $cat_post );
    }
}
if( count( $cats ) == count( $cat_post ) ) return;
$data = array(
    'ID'           => $_query->post->ID,
    'post_category' => array_keys($cat_post),
);
wp_update_post( $data );
}

wp_reset_postdata();

exit();

我想出的代码试图让第二位代码完成第一位代码的工作:

$_query = new WP_Query( array(
    'post_type' => 'products',
    'posts_per_page' => -1,
) );
function complete_parent_category( $product_id, $dep=array() ) {
    $product_cat = get_term( $product_id, 'product_cat');
    $dep[ $product_cat->product_id ] = $product_cat->slug;
    if( $product_cat->parent ) {
    $dep = complete_parent_category( $product_cat->parent, $dep );
    }
    return $dep;
}
echo '<pre>';
while( $_query->have_posts() ) {
$_query->next_post();
echo '<br /><strong>'.$_query->post->post_title .'</strong> |>> ';
$product_cat = array();
$product_cat = get_the_category( $_query->post->ID );
if( count( $product_cat ) ) {
    foreach ($product_cat as $product_cat) {
    $cat_post[ $product_cat->term_id ] = $cat->slug;
    if( $product_cat->parent == 0 ) continue; 
    $category_object = get_term( $product_cat, 'category' );
    $cat_post = complete_parent_category( $product_cat->parent, $cat_post );
    }
}
if( count( $product_cat ) == count( $cat_post ) ) return;
$data = array(
    'ID'           => $_query->post->ID,
    'product_cat' => array_keys($cat_post),
);
wp_update_post( $data );
}

wp_reset_postdata();

exit();

这只会破坏网站并将整个网站呈现为空白的白屏;然后我不得不跳入cpanel文件管理器并在那里更新functions.php文件,而不是在WordPress后端。我花了很多时间试图让它工作,它要么什么都不做,要么完全关闭网站(当你查看源代码时,它只是说,没有标签,没有标签......从字面上看,这是整个源代码:)<pre><head><body>

有没有人对如何使第一段代码在我的网站上工作有任何建议?

事后想想,Rankmath 代码会不会与它冲突?我将通过下面的 Rankmath 焦点关键字代码以供参考 - 我无法想象它会干扰。

/**
* Function to automatically update the focus keyword with the product, posts and pages title, if no focus keyword is set
*/
function update_focus_keywords() {
    $products = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'     => 'product' // Replace product with the name of your product type
    ));
    foreach ($products as $p) {
        // Checks if Rank Math keyword already exists and only updates if it doesn't have it
        $rank_math_keyword = get_post_meta( $p->ID, 'rank_math_focus_keyword', true );
if ( ! $rank_math_keyword ){ 
        update_post_meta($p->ID,'rank_math_focus_keyword',strtolower(get_the_title($p->ID)));
        }
    }

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'     => 'post' // Replace post with the name of your post type
    ));
    foreach($posts as $p){
        // Checks if Rank Math keyword already exists and only updates if it doesn't have it
        $rank_math_keyword = get_post_meta( $p->ID, 'rank_math_focus_keyword', true );
if ( ! $rank_math_keyword ){ 
        update_post_meta($p->ID,'rank_math_focus_keyword',strtolower(get_the_title($p->ID)));
        }
    }
$pages = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'     => 'page' // Replace product with the name of your product type
    ));
    foreach ($pages as $p) {
        // Checks if Rank Math keyword already exists and only updates if it doesn't have it
        $rank_math_keyword = get_post_meta( $p->ID, 'rank_math_focus_keyword', true );
if ( ! $rank_math_keyword ){ 
        update_post_meta($p->ID,'rank_math_focus_keyword',strtolower(get_the_title($p->ID)));
        }
    }
}
add_action( 'init', 'update_focus_keywords' );

任何帮助都非常感谢,但请记住,我真的是一个绝对的新手。我使用常识来改进我为 Rankmath 关键字找到的代码 - 我所要做的就是插入 “'post_type' => 'product'” 代替 'post_type' => 'posts'“ - 我刚刚意识到,通过将”posts“替换为”product“,该功能现在不适用于帖子。我现在已将其更改为“'post_type' => 'product','posts'”,我希望这能解决这个问题。看,我不是完全没用!但这确实是我技能的程度!

JavaScript PHP WordPress 函数 WooCommerce

评论

0赞 user3783243 10/12/2023
不确定格式是否为 100%,但认为它比您拥有的更好。如果 HTML 是纯文本,则需要在 HTML 周围使用反引号。例如.text `<p>` more text

答: 暂无答案