簡體   English   中英

WooCommerce 產品簡碼在函數中不起作用。php

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

我正在研究 wordpress 簡碼。 在 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>";
}

在帖子頁面中,我添加了[post-footer-note]代碼工作正常。 我想顯示 WooCommerce 產品,而不是“希望您發現本教程有用”文本。 為了在頁面上顯示產品,我對代碼做了一些改動

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

進行更改后,顯示以下錯誤。

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 ";"

你有一個語法錯誤:

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

您需要像這樣使用不同的引號:

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

您也可以翻轉引號(外引號(雙引號或單引號)無關緊要。

根據錯誤,第二個雙引號就在8之前,因此return結束,並且需要一個分號。 接下來是8 ,編譯器不知道如何解析您的代碼。

編輯:將@Moshe Gross 的編輯添加到最終解決方案中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM