简体   繁体   English

WooCommerce 产品简码在函数中不起作用。php

[英]WooCommerce product shortcode not working in functions.php

I am working on wordpress shortcode.我正在研究 wordpress 简码。 In the functions.php, I have added following code在 functions.php 中,我添加了以下代码

add_shortcode('post-footer-note', 'getPostFooterNote');
function getPostFooterNote() {
    return "<div>Hope you find this tutorial useful! <a href='#'>Share this link</a> on Social Media.</div>";
}

and in the post page, I added [post-footer-note] The code works fine.在帖子页面中,我添加了[post-footer-note]代码工作正常。 Instead of "Hope you find this tutorial useful" text I want to display WooCommerce products.我想显示 WooCommerce 产品,而不是“希望您发现本教程有用”文本。 To show the products on the page, I made few changes in the code为了在页面上显示产品,我对代码做了一些改动

add_shortcode('post-footer-note', 'getPostFooterNote');
function getPostFooterNote() {
    return "<div>[products limit="8" columns="4" category="hoodies, tshirts" cat_operator="NOT IN"]</div>";
}

After making changes, following error is showing.进行更改后,显示以下错误。

Your PHP code changes were rolled back due to an error on line 461 of file C:\xampp\htdocs\wordpress\wp-content\themes\twentynineteen\functions.php. Please fix and try saving again.

syntax error, unexpected integer "8", expecting ";"

You have a syntax error in:你有一个语法错误:

function getPostFooterNote() {
    return "<div>[products limit="8" columns="4" category="hoodies, tshirts" 
cat_operator="NOT IN"]</div>";
}

You need to use different quotations like this:您需要像这样使用不同的引号:

function getPostFooterNote() {
    return do_shortcode("<div>[products limit='8' columns='4' category='hoodies,  tshirts' cat_operator='NOT IN']</div>");
}

You could also flip the quotes (it doesn't matter which is the outer quotation marks (double or single).您也可以翻转引号(外引号(双引号或单引号)无关紧要。

As per the error, the second double quote is just before the 8 , and therefore the return is concluded, and a semicolon is expected.根据错误,第二个双引号就在8之前,因此return结束,并且需要一个分号。 Instead 8 is next, and the compiler doesn't know how to parse your code.接下来是8 ,编译器不知道如何解析您的代码。

Edit: added @Moshe Gross' edit to final solution.编辑:将@Moshe Gross 的编辑添加到最终解决方案中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM