2017-01-24 5 views
1

EDIT:私は実際にHTMLを出すことができるかどうかを確認するためにコードをかなり単純化しましたが、運はありません。WooCommerceのカスタムショートコードに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は出力されません。

オリジナルのポスト: は、私は私のクライアントは、より簡単に自分のウェブサイトを編集することができますように、独自のショートを記述しようとしています。私はワードプレスの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'] . '&nbsp;<a href="' . $product_link . '">More Info &gt;&gt;</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でたくさん書いていないので、私のコードがjankyなら謝る。これをベストプラクティスに近づけることができれば(自分の連結が正しいとは思わない)、私はフィードバックに門を開いています。

+0

ショートコードを投稿した時点では、どのような出力が出ますか? – AceWebDesign

+0

書かれたコードは500エラーを返します。 'return 'div class =" main_pro "...とそれに続くすべてのHTMLを削除すると、ショートコードをページに入力すると、ページ読み込みのナビゲーションのみになります。 – user3183717

答えて

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'] . '&nbsp;<a href="' . $product_link . '">More Info &gt;&gt;</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'); 
+0

使用している関数が機能しているかどうかは確認していませんが、PHPは今すぐ正しくネストする必要があります。 – AceWebDesign

+0

クイックコピー/ペーストで私のWPサイトのヘッダー/ナビだけが表示されるという問題が発生します。 私は問題を隔離したと思う:何らかの理由で '$ product_img = $ product-> get_image(); $ product_title = $ product-> get_title(); $ product_link = get_permalink($ a ['id']); $ children = $ product-> get_children(); ' これらの変数の一部またはすべてがクラッシュを引き起こしています。しかし、あなたのコード編集では、正しい軌道に乗ることができるはずです。私は答えを見つけ出すとこれを更新します。 – user3183717

+0

も$ product_title = $ product-> post-> post_titleを使用できます。名前を設定する – AceWebDesign

1

私はコードを少し変更して動作させました。これはあなたが完了できる単なる例です。

コードは、任意のプラグインファイルでも、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに行くか...

//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出力にこの時間を取得する必要があります。それを試してみて、私に知らせて。

関連する問題