Wordpress the_excerpt显示所有id的相同数据

时间:2014-12-31 13:10:50

标签: php wordpress

我有一个Wordpress的购物车插件。它使用the_excerpt来显示产品描述。它适用于除结帐页面之外的所有页面。在结帐页面上,它显示了所有产品的相同说明。它显示的描述始终来自数据库中的第一个产品,即使该产品不在购物车中。我试过很多变化没有修复它。它为产品提供了正确的ID和价格,但只是描述对于al产品来说总是相同的。

 <?php the_excerpt(get_the_ID()); ?>
       <input id="item-<?php echo $item->ID; ?>-price" name="item-<?php echo $item->ID; ?>-price" type="hidden" value="<?php echo intval($price); ?>"/>

在上面的代码中,价格正确,但每个产品的__cercer都相同。

我试过了:

    <?php the_excerpt(get_the_ID()); ?>
    <?php the_excerpt($item->ID); ?>
    <?php the_excerpt(get_the_excerpt); ?>

我还试过if和while但它只是显示了每种产品的所有20个产品说明

这是该页面的所有代码:

     <script>
window.onload = calculate_order(0,0);

function calculate_order(qty_item, qty_type) {
    //qty_type could be add or substract
    total = 0;
    numberofitems = 0;
    if (qty_type == "add"){
        document.getElementById((qty_item+"-qty")).value++;
    }
    if (qty_type == "subtract") {
        if (document.getElementById((qty_item+"-qty")).value > 0) {
           document.getElementById((qty_item+"-qty")).value--;
        }
    }
    //return;
    $('.store-item').each(function(i, obj) {
        price = document.getElementById(($(this).attr("id")+"-price")).value;
        //price = price * 1 ;
        qty = document.getElementById(($(this).attr("id")+"-qty")).value;
        //qty = qty * 1;
        numberofitems = numberofitems + (qty*1);
        total         = total + ((price*1)*(qty*1));
        //alert (numberofitems);
    });
    document.getElementById("total_price").innerHTML = total ;
    document.getElementById("total_qty").value = numberofitems ;
    discount = 100*Math.floor(numberofitems/2);
    document.getElementById("total_discount").innerHTML = discount ;
    document.getElementById("final_price").innerHTML = (total - discount);
    document.getElementById("tot_discount").value = discount ;
    document.getElementById("tot_order").value = (total - discount);
    document.getElementById("tot_price").value = total ;
    if (numberofitems <2) {
        $("#total-price").hide();
        $("#total-discount").hide();
    }
    else {
        $("#total-price").show();
        $("#total-discount").show();
    }
    return;
  }
 </script>
 <div id="shopping-cart">


  <div>
  <h2>Shopping Cart</h2>
  </div>
  <div id="section group">
 <div class="col span_1_of_4"> Items in Cart</div>
<div class="col span_1_of_4">
  Description
</div>
<div class="col span_1_of_4" style="text-align:center">
 Price
</div>
<div class="col span_1_of_4" style="text-align:right">
  Quantity
</div>

</div></div>
<div style="clear:both"></div>

<div class="cart-header">
   <div id="checkout-form" class="cart-header" style="border:0px;">
        <fieldset class="addressdetails">
            <table width="330" border="0" cellspacing="0" cellpadding="0">
            <tr><td style="width:120px;padding-bottom:5px">Your Name:</td><td style="padding-bottom:5px"><input id="yName" type="text" name="order_name2" style="background-color:#D3E3F8"/></td></tr>
            <tr><td>Your Email</td><td style="padding-bottom:5px"><input id="Email" type="text" name="order_email" style="background-color:#D3E3F8"/> </td></tr>
            <tr><td></br></td><td>
            <tr><td style="width:120px;padding-bottom:5px">Shipping Name:</td><td style="padding-bottom:5px"><input id="Name" type="text" name="order_name" style="background-color:#D3E3F8"/></td></tr>
            <tr><td>Shipping Address:</td><td style="padding-bottom:5px"><input id="Address" type="text" name="order_address" style="background-color:#D3E3F8"/></td></tr>
            <tr><td>City: </td><td style="padding-bottom:5px"><input id="City" type="text" name="order_city" style="background-color:#D3E3F8"/></td></tr>
            <tr><td>State:</td><td style="padding-bottom:5px"><input id="State" type="text" name="order_state" style="background-color:#D3E3F8"/></td></tr>
            <tr><td>Zip Code:</td><td style="padding-bottom:5px"><input id="ZipCode" type="text" name="order_zipcode" style="background-color:#D3E3F8"/></td></tr>
            <tr><td>Country:</td><td><input id="Country" type="text" name="order_country" style="background-color:#D3E3F8"/></td></tr>
            <tr><td></br></td><td>

            <tr><td>Comments</td><td style="padding-bottom:5px"><textarea id="Comments" name="order_comments" style="width:180px;background-color:#D3E3F8"></textarea></td></tr>
            </table>


            </br>
        </fieldset>
        </br>
        <div><label></label><center><input type="submit" name="submit" value="Submit Order" class="submit_order_button"/></center></div>
</div>
</div>

<?php $total = 0; ?>
<?php $nitems = 0; ?>
<?php foreach ($_POST as $item => $quantity): ?>
<?php $matches = array(); ?>
<?php if (preg_match('/^item-([0-9]+)$/', $item, $matches) === 1 and intval($quantity) > 0): ?>
<?php $item = get_post(intval($matches[1])); ?>
<?php if ($item): ?>
  <?php $nitems++; ?>
  <?php $price = intval(get_post_meta($item->ID, 'price', true)); ?>
  <?php $total += $price; ?>


   <div class="section group" style="margin:10px 0 10px 0">
   <input id="item-<?php echo $item->ID; ?>-ID" type="hidden" name="art_id[]]" value="<?php echo $item->ID; ?>"/>

   <div class="col span_1_of_4" style="text-align:center">
      <?php echo get_the_post_thumbnail($item->ID, 'checkout-thumbnail'); ?>
    </div>
    <div class="col span_1_of_4">
      <h2><?php
        $title = explode('(', $item->post_title, 2);
        echo $title[0];
        if (count($title) > 1) {
          echo '<span class="parenthetical">(';
          echo preg_replace('/ /', '&nbsp;', $title[1]);
          echo '</span>';
        }
      ?></h2>
      <?php while ( have_posts() ) : the_post(); ?>
      <?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo get_the_excerpt($item->ID); // Outputs the processed value to the page
?>
<?php endwhile; ?>         

  </div>
   <div class="col span_1_of_4" style="text-align:center">
 $ <span id="price"><?php echo $price; ?></span>
</div>

       <input id="item-<?php echo $item->ID; ?>-price" name="item-<?php echo $item->ID; ?>-price" type="hidden" value="<?php echo intval($price); ?>"/>

    <div class="col span_1_of_4" style="text-align:right">
      <input type="text" class="store-item-quantity" style="width:20px" name="item-<?php echo $item->ID; ?>-qty" id="item-<?php echo $item->ID; ?>-qty" value="<?php echo intval($quantity); ?>" readonly />
        <input type='button' class="qtybutton" name='add' onclick='javascript: calculate_order("item-<?php echo $item->ID; ?>", "add");' value='+'/>
        <input type='button' class="qtybutton" name='subtract' onclick='javascript: calculate_order("item-<?php echo $item->ID; ?>", "subtract");' value='-'/>
    </div></div>
    <!--<input id="item-<?php echo $item->ID; ?>-qty" type="text" value="<?php echo intval($quantity); ?>"/>-->
  </div></div>






<?php endif; ?>
  <?php endif; ?>
<?php endforeach; ?>


 <!-- MSR 11062013 display mode of discount -->
 <?php $discount = 100*intval(floor($nitems/2)); ?>
 <div id="price-totals" style="padding-right:20px">
  Price includes international shipping<br />
 <table id="price-table">
  <input id="total_qty" type="hidden" value="<?php echo $nitems; ?>"/>
  <tr id="total-price" <?php if ($nitems < 2) echo 'style="display:none"' ?>>
    <td class="price-label">Total:</td>
    <td class="price-value">$ <span id="total_price"><?php echo $total; ?></span></td>
  </tr>
  <tr id="total-discount" <?php if ($nitems < 2) echo 'style="display:none"' ?>>
    <td class="price-label">Discount:</td>
    <td class="price-value">$ <span id="total_discount"><?php echo $discount; ?></span>    </td>
  </tr>
  <tr>
    <td class="price-label"><strong>Final Price:</strong></td>
    <td class="price-value"><strong>$ <span id="final_price"><?php echo $total - $discount; ?></strong></span></td>
  </tr>
</table>
<input id="tot_price" type="hidden" name="tot_price" value="<?php echo $total; ?>"/>
<input id="tot_discount" type="hidden" name="tot_discount" value="<?php echo $discount; ?>"/>
<input id="tot_order" type="hidden" name="tot_order" value="<?php echo $total - $discount; ?>"/>
  </div>
 </div>

 </div>


 </div>

1 个答案:

答案 0 :(得分:0)

您无法将参数传递给the_excerpt,它总是在循环中使用&#34;帖子&#34;同样适用于get_the_excerpt(之前我错了)。通过查看代码,您将遍历每个项目并获取其对应的WP_Post对象。它的摘录应该在$item->post_excerpt

一种可能更简洁的方法是创建一个可以循环的新WP_Query。见http://codex.wordpress.org/The_Loop#Multiple_Loops