编辑:我已经大大简化了我的代码,看看我是否真的可以获得HTML输出,但没有运气。
以下是简化版本,删除了我的所有自定义HTML,只添加<div class="test"></div>
以查看我是否可以将我的短代码输出到HTML,而不是。
//custom shortcodes
function product_snippet( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
'snippet' => '',
),
$atts,
'product_snippet'
);
return $product = wc_get_product($atts['id']);
return $product_img = $product->get_image();
return $product_title = $product->get_title();
return $product_link = get_permalink($atts['id']);
return $children = $product->get_children();
return $children_array = array();
echo "<div class='test'></div>";
}
add_shortcode( 'product_snippet', 'product_snippet' );
这会阻止网站崩溃,但不会输出HTML。
原始邮寄:
我正在尝试编写自定义短代码,以便我的客户能够更轻松地编辑他们的网站。我已经将以下代码添加到我的WordPress的functions.php
文件中,我无法确定它为什么不起作用。
我们的想法是将分组产品的ID输入到短代码中,它将提取产品的标题,图像和所有子产品。
function product_snippet( $atts ) {
$a = shortcode_atts( array (
'id',
'snippet',
), $atts);
$product = wc_get_product($a['id']);
$product_img = $product->get_image();
$product_title = $product->get_title();
$product_link = get_permalink($a['id']);
$children = $product->get_children();
//initialize array so that child products can be sorted by price.
$children_array = array();
//Assign each child product to its price in array so that child products can be sorted by price in HTML.
foreach ($children as $key => $value) {
$children_array[$value] = wc_get_product($value)->get_price();
};
asort($children_array);
return '<div class="main_pro">
<div class="left-sd">' . do_shortcode('[product id="' . $a['id'] . '"]') . '</div>
<div class="right-sd">
<div class="info">
<h2>'. $product_title . '</h2>
<p>' . $a['snippet'] . ' <a href="' . $product_link . '">More Info >></a></p>
</div>
<ul>
' . foreach ($children_array as $key => $value) {
$option = wc_get_product($key);
$option_title = $option->post->post_title;
. '
<li>
<div id="outer-bar">
<div class="bar-l">
' . $option_title;
if ($option->get_attribute('pa_pack-size')) {
strval(wc_get_product($key)->get_attribute('pa_pack-size'));
.'<br/>
<span class="small_pz">
' . "$" . strval(round(($option->get_price() / intval($option->get_attribute('pa_pack-size'))), 2)) . " per tray - SAVE $" . strval(round(($product->get_price() * $option->get_price() - $option->get_price()), 2)); . '
</span>
' . }
. '</div>
<div class="bar-r">' . do_shortcode('[add_to_cart id="' . $key . '"]'); . '</div>
</div>
</li>
' . }
. ' </ul>
</div>
</div>';
};
add_shortcode( 'product_snippet', 'product_snippet' );
假设我有我的开始和结束<?php ?>
标签。我不会在PHP中写很多,所以如果我的代码很简陋,我会道歉。如果我能够接近最佳实践(我不认为我的连接看起来正确),我愿意接受反馈。
答案 0 :(得分:1)
代码中有一堆错误。试试这个。
function product_snippet($atts)
{
$a = shortcode_atts(array(
'id',
'snippet',
), $atts);
$product = wc_get_product($a['id']);
$product_img = $product->get_image();
$product_title = $product->get_title();
$product_link = get_permalink($a['id']);
$children = $product->get_children();
//initialize array so that child products can be sorted by price.
$children_array = array();
//Assign each child product to its price in array so that child products can be sorted by price in HTML.
foreach ($children as $key => $value) {
$children_array[$value] = wc_get_product($value)->get_price();
};
asort($children_array);
$string = '<div class="main_pro">
<div class="left-sd">' . do_shortcode('[product id="' . $a['id'] . '"]') . '</div>
<div class="right-sd">
<div class="info">
<h2>' . $product_title . '</h2>
<p>' . $a['snippet'] . ' <a href="' . $product_link . '">More Info >></a></p>
</div>
<ul>';
foreach ($children_array as $key => $value) {
$option = wc_get_product($key);
$option_title = $option->post->post_title;
$string .= '
<li>
<div id="outer-bar">
<div class="bar-l">
' . $option_title;
if ($option->get_attribute('pa_pack-size')) {
strval(wc_get_product($key)->get_attribute('pa_pack-size'));
$string .= '<br/>
<span class="small_pz">
' . "$" . strval(round(($option->get_price() / intval($option->get_attribute('pa_pack-size'))), 2)) . " per tray - SAVE $" . strval(round(($product->get_price() * $option->get_price() - $option->get_price()), 2)) .
'</span>';
}
$string .= '</div>
<div class="bar-r">' . do_shortcode('[add_to_cart id="' . $key . '"]') . '</div>
</div>
</li>
';
}
$string .= ' </ul>
</div>
</div>';
return $string;
}
add_shortcode('product_snippet', 'product_snippet');
答案 1 :(得分:1)
我已经更改了一些代码以使其正常工作。这只是您可以完成的示例。试试吧,让我知道:
//custom shortcodes
if( !function_exists('product_snippet') ) {
function product_snippet( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'id' => '', // You will use $id to get the value of this attribute
'snippet' => '' // You will use $snippet to get the value of this attribute
),
$atts
));
// Get an instance of the product object
$product = wc_get_product( $id );
// Displays go here
return '<div class="test">'.$product->get_image().'<a href="'.get_permalink( $id ).'">'.$product->get_title().'</a></div>';
}
add_shortcode( 'product_snippet', 'product_snippet' );
}
这次你应该得到一个html输出......
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。