DO_SHORT代码在某些页面上不起作用

时间:2013-02-24 作者:user1889007

我正在使用Artiss货币转换器转换我网站上的一些数字。我想把do_shortcode(\'[convert number=1 from="aud" to="jpy"]\') 共2页。一个是中的模板页mythemefolder/newtemplate.php 另一个在mythemefolder/functions/single_product.php

do_shortcode(\'[convert number=1 from="aud" to="jpy"]\') 完美工作于newtempalte.php 但这似乎不起作用single_product.php.

为什么会这样?我如何修复它,php文件的位置是否重要do_shortcode 去工作?

这是single\\u product中的函数。php

    function cps_ajax_search($meta_boxes){
    $posts = cps_search_posts();?>

<?php get_template_part("sidebar-left-common");?>

<?php require_once(TEMPLATEPATH."/functions/var/default-box-one.php"); ?>
<div class="detail-page-content hideOnSearch">

 <!-- detail page content starts -->

<div class="searchBreadcrumbs"><!--  breadcrumbs starts -->
        <?php  \'<a href="#" class="cpsBack">Home</a> &raquo;\';?> 
        <?php cps_breadcrumbs(); ?>
    </div> <!-- breadcrumbs ends -->
    <div style="clear:both"></div>  
<div class="sort-by-bar"> <!-- sort bar starts -->
<div class="searchSort"> <!-- search sort starts -->
<?php _e(\'Sort By:\',\'language\');?>
<?php cps_sort_by(\'miles\') ?> - 
<?php cps_sort_by(\'year\') ?> -
<?php cps_sort_by(\'price\') ?>

</div> <!-- search sort ends -->
</div> <!-- sort bar ends -->
<div style="clear:both"></div>
<?php wp_reset_postdata();?>
        <?php
            $displayed = array();
             if(!empty($posts)): foreach($posts as $post): 

                if(in_array($post->ID,$displayed)):
                    continue;
                else:
                    $displayed[] = $post->ID;
                endif;
            ?>
<?php global $options;$fields;$options2;$options3;$symbols;
              $fields = get_post_meta($post->ID, \'mod1\', true);
              $options2 = get_post_meta($post->ID, \'mod2\', true);
              $options3 = get_post_meta($post->ID, \'mod3\', true);
              $symbols = get_option(\'gorilla_symbols\');
              $options = get_option(\'gorilla_fields\'); ?>           
<?php $blogurl = get_bloginfo(\'template_url\'); ?>
<?php $surl = get_bloginfo(\'url\'); ?>   
<div class="result-car"><!-- result car -->
<?php       
    $args = array(
    \'order\'          => \'ASC\',
    \'orderby\'        => \'menu_order\',
    \'post_type\'      => \'attachment\',
    \'post_parent\'    => $post->ID,
    \'post_mime_type\' => \'image\',
    \'post_status\'    => null,
    \'numberposts\'    => 1,
    );

    $attachments = get_posts($args);
    if ($attachments) {


        foreach ($attachments as $attachment) {

        echo \'<a href=\'.$surl.\'/\'.$post->post_name.\'>\'.wp_get_attachment_image($attachment->ID, \'thumbnail_results\').\'<span class="\'.$fields[\'statustag\'].\'"></span></a>\';
    }
} ?> 
                        <div class="result-detail-wrapper">  <!-- result detail wrapper -->
                            <p><a href="<?php echo $post->post_name ?>" rel="bookmark" title="<?php echo $post->post_title ?>"><?php if ( $fields[\'year\']){ echo $fields[\'year\'];}else {  echo \'\'; }?> <?php echo $post->post_title ?></a></p>
                            <p><strong><?php if (isset( $fields[\'miles\'])){
                            ##### New ##########
                            //number_format($fields[\'miles\'],0,\'.\',\'.\')
                            echo number_format($fields[\'miles\'],0,\'.\',\',\').\' \'.$options[\'milestext\'];
                            ##### End ##########
                            }else {  echo \'\'; };?></strong></p>
                            <p><?php if (isset( $fields[\'vehicletype\'])){ echo $fields[\'vehicletype\'].\' | \';}else {  echo \'\'; };?> <?php if (isset( $fields[\'transmission\'])){ echo $fields[\'transmission\'];}else {  echo \'\'; };?><br/><?php if (isset( $options2[\'cylinders\'])){ echo $options2[\'cylinders\'].\' \'.$options[\'cylinderstext\'].\' | \';}else {  echo \'\'; };?><?php if (isset( $fields[\'interior\'])){ echo $fields[\'interior\'].\' | \';}else {  echo \'\'; };?><?php if (isset( $fields[\'epamileage\'])){ echo $fields[\'epamileage\'];}else {  echo \'\'; };?></p>
                            <p class="result-price"><?php include(TEMPLATEPATH."/functions/var/default-box-one.php");
                            //echo $symbols[\'currency\'];     
                            ##### New ##########
                            // echo number_format($fields[\'price\']);  
                            ##### End ##########

                          ?>

                          <span id="calPriceInAud<?php echo $callPriceInAudCounter; ?>">Calculating...</span>

                            <script>
                            //Calculator.js
                            //function CarCostCalculator(enteredPriceInYen,cubicMeters,complianceFee,serviceFee,inventoryItemID)
                            CarCostCalculator(<?php echo $fields[\'price\']; ?>,14, 2500, 1100,<?php  echo do_shortcode(\'[convert number=1 from="usd" to="aud"]\');?>)
                            </script>
                             </p>

                         </div> <!--   result detail wrapper ends -->
 </div> <!-- result car ends --> 
            <?php endforeach; else: ?>
                <p style="padding:30px;"><?php _e(\'Sorry, no listings matched your criteria.\',\'language\');?></p>
            <?php endif; ?>
            <div class="bottom-pagination"> <!-- Pagination starts -->
                        <p><a id="link" href="#top"><?php _e(\'BACK TO TOP\',\'language\');?></a></p>
                        <p class="paging">
                            <?php cps_show_pagination() ?>
                        </p>
                  </div>  <!-- Pagination ends -->  
                </div>
    <?php

    exit;
}
如你所见

成本计算器(<?php echo $fields[\'price\']; ?>,1425001100,<?php echo do_shortcode(\'[convert number=1 from="usd" to="aud"]\');?>)

do_shortcode(\'[convert number=1 from="usd" to="aud"]\') 但它不起作用。但在模板文件上效果很好。

2 个回复
SO网友:Tom J Nowell

global $options;$fields;$options2;$options3;$symbols;
这与:

global $options;
$fields;
$options2;
$options3;
$symbols;
您已经将选项声明为全局变量,但其他变量只是声明了,它们什么也不做,这些行也可能不存在。

接下来我们有:

$fields = get_post_meta($post->ID, \'mod1\', true);
但您没有检查它是否为错误值。

if (isset( $fields[\'miles\'])){
                            ##### New ##########
                            //number_format($fields[\'miles\'],0,\'.\',\'.\')
                            echo number_format($fields[\'miles\'],0,\'.\',\',\').\' \'.$options[\'milestext\'];
                            ##### End ##########
                            }else {  echo \'\'; }
所以你一无所获的原因,是因为英里范围没有设定。所以它会打印“”,也就是什么都没有。尝试将“”替换为“error”,您将看到打印的“error”。

最后,短代码用于内容。相反,您是在循环之外间接调用短代码。

短代码可能会引用当前帖子,您最好调用短代码绑定到的实际函数,而不是使用do\\u短代码

SO网友:user1889007

我弄明白了为什么有些页面不允许do\\u短代码。这可以应用于任何Wordpress插件,因此任何人都可以使用此技巧。我包括了插件的函数php文件

<?php require_once($dir = ABSPATH . "/wp-content/plugins/artiss-currency-converter/includes/acc-functions.php"); ?>
并用它的函数来计算利率

get_conversion( \'number=1&from=aud&to=jpy\' )
就是这样。

结束

相关推荐

Using shortcodes in PHP

我试图在我的页面中使用短代码,我从一个简单的测试开始。在里面functions.php:function HelloWorldShortcode() { return \'<p>Hello World!</p>\'; } add_shortcode(\'helloworld\', \'HelloWorldShortcode\'); 在中index.php:[helloworld] 这不会产生<p>Hello World