dongmangji0950 2015-11-13 14:30
浏览 82
已采纳

如何在wordpress中添加附加产品类别

I use below code to add one product category to a product which is added database;

wp_set_object_terms( $the_post_id, 528, 'product_cat' );

and it appends the relevant product category to that post (I can see the category name on products page). But when I tried to do it with and submit form, it only creates and appends as number like 528 (I see the 528 on product page instead of category name);

$kategori = $_POST['kategori'];
wp_set_object_terms( $the_post_id, $kategori, 'product_cat' );

How can I add these all categories if it should be in parent categories also with a submit form?

  • 写回答

1条回答 默认 最新

  • dtf0925 2015-11-13 15:04
    关注

    Type cast your value to prevent it being used as a string.

    $kategori = $_POST['kategori'];
    wp_set_object_terms( $the_post_id, (int)$kategori, 'product_cat' );
    

    As to parent categories im not sure although I think this post has what you need. https://wordpress.stackexchange.com/questions/99681/how-to-set-hierarchical-terms-to-a-post-using-wp-set-object-terms

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部