EDIT: Ich habe meinen Code erheblich vereinfacht, um zu sehen, ob ich HTML tatsächlich erhalten kann, aber kein Glück.Ausgabe Produkt HTML in benutzerdefinierten Shortcode für WooCommerce
Hier ist die vereinfachte Version, entfernen Sie alle meine benutzerdefinierten HTML und nur <div class="test"></div>
hinzufügen, um zu sehen, ob ich meinen Shortcode HTML ausgeben kann, was es nicht tut.
//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');
Dies stoppt die Website vor dem Absturz, aber HTML wird nicht ausgegeben.
ORIGINAL POST: Ich versuche, einen benutzerdefinierten Shortcode zu schreiben, damit mein Client leichter ihre Website bearbeiten kann. Ich habe den folgenden Code zu meiner WordPress functions.php
Datei hinzugefügt und ich kann nicht feststellen, warum es nicht funktioniert.
Die Idee besteht darin, die ID eines gruppierten Produkts in den Funktionscode einzugeben, und es wird den Titel, das Bild und alle untergeordneten Produkte des Produkts aufrufen.
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');
Angenommen, ich meine Öffnung haben und <?php ?>
Tags schließen. Ich schreibe nicht viel in PHP, also entschuldige ich mich, wenn mein Code janky ist. Wenn ich dies den Best Practices näher bringen kann (ich glaube nicht, dass meine Verkettung richtig aussieht), bin ich offen für Feedback.
Was wird in dem Moment ausgegeben, wenn Sie den Shortcode in einen Post schreiben? – AceWebDesign
Der Code wie geschrieben gibt einen 500 Fehler zurück. Wenn ich 'return 'div class =" main_pro "...' und den gesamten nachfolgenden HTML-Code entferne, wird beim Eingeben des Kurzwahlcodes in eine Seite nur der nav der Seite geladen. – user3183717