简体   繁体   English

在wordpress中类别中的所有帖子上显示计算值

[英]display a calulated value on all post in a category in wordpress

i am working on wordpress based coupon site. 我正在基于Wordpress的优惠券网站上工作。 I have to create a amount calculator which would work on all the individual category pages . 我必须创建一个可以在所有单独类别页面上使用的金额计算器。 I would be having a amount slider which would be having $ values. 我将有一个数量滑块,将有$值。

Once a value is selected and on clicking the submit button, i want the percentage deals (under that respective category) to calculate a amount with respect to the $ amount selected using the slider. 一旦选择了一个值并单击“提交”按钮,我希望交易百分比(在该类别下)相对于使用滑块选择的$金额计算金额。 and then displaying it on their respective deals. 然后将其显示在各自的交易中。 I hope the idea is clear. 我希望这个主意很清楚。

Till now, i have managed to, take all the posts title of the current category page into an array, and then using the preg_match feature, i have managed to extract out the '%' deal amount. 到目前为止,我设法将当前类别页面的所有文章标题都放入一个数组,然后使用preg_match功能,我已经设法提取了“%”交易额。 Also i have created a simple slider which the user will need to input their $ amount. 我还创建了一个简单的滑块,用户需要输入其$金额。

<?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  }
} ?>

the above code is used to fetch all the post under the current category and extracting the % value from the respective post title. 上面的代码用于获取当前类别下的所有帖子,并从相应的帖子标题中提取%值。 The extracted number is in '$m[1]' which i would like to pass against the respective post. 提取的数字在'$ m [1]'中,我想将其传递给相应的帖子。

I am not able to define the respective post and passing that '%' amount and in return sending the calculated amount and saving it back to that particular post. 我无法定义相应的帖子并传递该“%”金额,而无法发送计算出的金额并将其保存回该特定帖子。 that is, on clicking the submit button, i would want that each post having percentage value would get calculated and get displayed against that particular post. 也就是说,在单击“提交”按钮时,我希望每个具有百分比值的帖子都将被计算并针对该特定帖子进行显示。 Sorry for such a huge explanation. 很抱歉提供这么大的解释。 I didnt want any detail to get missed out. 我不想错过任何细节。 Any help would be appreciated. 任何帮助,将不胜感激。

EDITED CODE - This code is responsible to display a single deal. 编辑代码-此代码负责显示一笔交易。 I have placed the above mentioned in the sidebar file of my theme. 我已将上述内容放在主题的侧边栏文件中。 I want to display the savings within the respective percentage deal. 我想显示相应百分比交易内的节省。

    <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>

Ok I have created a JSFIDDLE that will give you an idea about what to do. 好的,我创建了一个JSFIDDLE,它将为您提供有关操作的想法。 http://jsfiddle.net/Q5sAK/1/ http://jsfiddle.net/Q5sAK/1/

For this I used the jquery UI slider but this should work for your own slider you just need to call the $.each() function when you are ready. 为此,我使用了jquery UI滑块,但是它应该适合您自己的滑块,您只需要在准备好后调用$ .each()函数即可。

So we know that your percentage is in the h2 tag with the class coupontitle of all of you products. 因此,我们知道您的百分比在所有产品类别优惠券标题的h2标签中。

First we are going to start by modifying that h2 to have a span tag at the end that we will use to hold the computed savings: 首先,我们将从修改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>

Then we need to add the javascript that will calculate the savings. 然后,我们需要添加用于计算节省量的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() });

Then you just need to call updateSlider() when ever the slider updates. 然后,只要滑块更新,就只需要调用updateSlider()即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM