【问题标题】:display a calulated value on all post in a category in wordpress在 wordpress 的某个类别中的所有帖子上显示计算值
【发布时间】:2013-10-03 08:14:49
【问题描述】:

我正在开发基于 wordpress 的优惠券网站。我必须创建一个适用于所有单独类别页面的金额计算器。我将有一个金额滑块,该滑块将具有 $ 值。

一旦选择了一个值并单击提交按钮,我希望百分比交易(在相应的类别下)计算相对于使用滑块选择的 $ 金额的金额。然后将其显示在各自的交易中。 我希望这个想法很清楚。

到目前为止,我已经成功地将当前类别页面的所有帖子标题放入一个数组中,然后使用 preg_match 功能,我成功​​地提取了“%”交易金额。 我还创建了一个简单的滑块,用户需要输入他们的金额。

<?php
$array = array();
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 1, 'offset' => 0, 'category__in' => array($category), 'post_status'=>'publish'));
foreach($myposts as $post) :
setup_postdata($post);

$title = get_the_title();
array_push($array,$title);

 endforeach; ?> 
<?php wp_reset_query(); ?>

  <?php   

foreach($array as $str) {
if(preg_match('/(\d+)\%/i' ,$str,$m)) {
             echo $m[1],"\n"; ?>

  <input type="text" name="a" value="<?php echo $m[1]; ?>" size=5> 

<?php  }
} ?>

上面的代码用于获取当前类别下的所有帖子,并从相应的帖子标题中提取 % 值。提取的数字在 '$m[1]' 中,我想将其传递给相应的帖子。

我无法定义相应的帖子并传递“%”金额,作为回报,我无法发送计算出的金额并将其保存回该特定帖子。也就是说,在单击提交按钮时,我希望每个具有百分比值的帖子都会被计算并针对该特定帖子显示。抱歉这么大的解释。我不想错过任何细节。任何帮助将不胜感激。

已编辑代码 - 此代码负责显示单个交易。我已将上述内容放在主题的侧边栏文件中。我想在相应的百分比交易中显示节省的费用。

    <div style="float:left; <?php if($GLOBALS['themename']['display_previewimage'] =="yes"){ ?>width:357px;<?php }else{ ?>width:477px;margin-left:10px;<?php } ?>">

     <h2 class="coupontitle">

        <a href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>" <?php if($GLOBALS['premiumpress']['analytics_tracking'] =="yes"){ ?>onclick="pageTracker._trackEvent('COUPON CLICK', 'TITLE CLICK', '<?php the_title(); ?>');"<?php } ?> <?php if(is_single()){ ?> target="_blank"<?php } ?>>

           <?php the_title(); ?>

        </a>

    </h2>

    <p><?php echo $post->post_content; ?></p> 

    <?php if($code != "" && $GLOBALS['themename']['system'] =="link"){ ?>        
    </div>

【问题讨论】:

  • 它有点不清楚你在找什么。如果我错了,请纠正我,但您有一个列出帖子的类别索引页面。在那些帖子标题中是一个百分比。在类别索引页面上,您有一个代表美元金额的滑块。这就是我感到困惑的地方,用户将滑块滑动到美元金额点击提交,你想要发生什么?
  • 嗨 Chausser,在用户选择金额并点击提交按钮后,我想从每个帖子中收集所有交易百分比并计算用户输入(滑块)的金额并显示该金额反对各自的职位。
  • 所以 EX:帖子 1 % 是 10% 帖子 2 % 是 25% 用户滑动滑块以指示 100 美元,您希望帖子 1 显示节省 10 美元的帖子 2 显示节省 25 美元?
  • 是的。现在我可以从帖子 1 中获取“10”,从帖子 2 中获取“25”。但我无法分别计算值“$10”和“$25”并将其传递给帖子 1 和 2。跨度>
  • 你能把渲染后包装器的代码和你想要的储蓄去哪里,我试一试吗

标签: javascript php jquery wordpress search


【解决方案1】:

好的,我已经创建了一个 JSFIDDLE,它可以让您了解该做什么。 http://jsfiddle.net/Q5sAK/1/

为此,我使用了 jquery UI 滑块,但这应该适用于您自己的滑块,您只需要在准备好时调用 $.each() 函数即可。

所以我们知道你的百分比在你所有产品的类优惠券标题的 h2 标签中。

首先,我们将首先修改 h2,使其末尾有一个 span 标签,我们将使用它来保存计算出的节省:

<h2 class="coupontitle">
    <a href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>" <?php if($GLOBALS['premiumpress']['analytics_tracking'] =="yes"){ ?>onclick="pageTracker._trackEvent('COUPON CLICK', 'TITLE CLICK', '<?php the_title(); ?>');"<?php } ?> <?php if(is_single()){ ?> target="_blank"<?php } ?>>
       <?php the_title(); ?>
    </a>
    <span><span>
</h2>

然后我们需要添加将计算节省的 javascript。

function updateSlider(){
    //Get the value of the slider.
    var curentSliderAmount = $('#sliderId').val();

    //Loop over all of the titles
    $.each($('.coupontitle'),function(index,coupObj){
        //This will create the variable from the regex search that will have all of the parts for the percent we need
        var percent = $(coupObj).text().match(/(\d+)\%/i);
        //We will then take the 2nd part which is just the number without the % sign and make it a percent then multiply that by the slider value and then fix it to a 2 decimal value so it can be used a curency.
        var savings = ((percent[1]*.01) * curentSliderAmount ).toFixed(2);
        //We then set the span html content = to our newly calculated value.
        $('span',coupObj).html('Save: $'+savings);
    });
}
//Run this when the page starts
$(document).ready(function(){ updateSlider() });

那么你只需要在滑块更新时调用 updateSlider()。

【讨论】:

  • 非常感谢乔瑟。该代码适用于所有百分比交易。现在我想为“$”交易和其中没有任何金额的交易建立一个条件。
  • 任何百分比金额?那么没有保存百分比的帖子只显示输入值?
  • 表示交易中没有 25% 或任何金额。有些交易有“20 美元”的折扣,有些交易中没有任何数字。此外,给定的代码仅适用于第一笔交易,可能会因为其他交易不是折扣交易而中断。
  • 我为所有 $ Off 交易和 Shipping 交易添加了多个条件,并且效果很好。我试图使用提交按钮分离输入和输出。表示我想滑动滑块并选择一个金额。并单击提交按钮,我希望进行所有计算并显示结果。
  • 在这种情况下,我建议您改为使用 wordpress 自定义字段选项来发布帖子并设置为储蓄类型和储蓄金额,然后使用标题上的“数据-”属性来确定什么类型如果该帖子有任何节省然后计算并显示它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2015-02-22
  • 1970-01-01
相关资源
最近更新 更多