php 在 Woocommerce 中获取产品名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26167916/
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-08-25 18:12:19  来源:igfitidea点击:

Get product name in Woocommerce

phpwordpresswoocommerceproduct

提问by nsilva

I want to be able to display a product title by using PHP to echo the product name by the product ID (this is to be displayed within a Page, not the product page itself). I am using Wordpress and I have a plugin for PHP so I can include PHP code using [php]echo 'example';[/php]

我希望能够通过使用 PHP 通过产品 ID 回显产品名称来显示产品标题(这将显示在页面中,而不是产品页面本身)。我正在使用 Wordpress 并且我有一个 PHP 插件,因此我可以使用包含 PHP 代码[php]echo 'example';[/php]

One product example is; http://ukcctvinstallations.co.uk/product/1-camera-residential-system-hi-res/

一个产品示例是;http://ukcctvinstallations.co.uk/product/1-camera-residential-system-hi-res/

When I edit the product, you can see in the URL that the 'Post' = 129 so am I right in saying this is the product ID?

当我编辑产品时,您可以在 URL 中看到 'Post' = 129 那么我说这是产品 ID 对吗?

If anyone has a solution for this, that would be much appreciated. I am using WooCommerce.

如果有人对此有解决方案,将不胜感激。我正在使用 WooCommerce。

采纳答案by nsilva

Found a solution:-

找到了解决办法:-

echo get_the_title( 'ID' );

echo get_the_title( 'ID' );

Take a look here: http://docs.woothemes.com/wc-apidocs/package-WooCommerce.Functions.Product.html

看看这里:http: //docs.woothemes.com/wc-apidocs/package-WooCommerce.Functions.Product.html

回答by Jared

Assuming you get the product as an object

假设您将产品作为对象

$product = wc_get_product( id );

echo $product->get_title();

(WC version 2.5.2)

(WC 版本 2.5.2)