wordpress 按产品名称获取产品永久链接,Woocommerce

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24946600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:34:41  来源:igfitidea点击:

Get product permalink by product name ,Woocommerce

wordpresswoocommercepermalinks

提问by user3868840

I am trying to get Woocommerce product permalink by given product name. I know I can get permalink by given product ID like this:

我正在尝试通过给定的产品名称获取 Woocommerce 产品永久链接。我知道我可以通过给定的产品 ID 获得永久链接,如下所示:

$url = get_permalink( $product_id );

But I can not find any code to get it by product name. I tried this:

但是我找不到任何代码来按产品名称获取它。我试过这个:

$url = get_permalink( 'title' );

That does not work. Please help.

那行不通。请帮忙。

回答by sabarnix

This code works for me

这段代码对我有用

$product = get_page_by_title( 'Product Title', OBJECT, 'product' )
echo get_permalink( $product->ID );

回答by Alkesh Goswami

For URL:

对于网址:

$url = get_permalink($product_id)

For the title:

对于标题:

$name = get_the_title($product_id)