【问题标题】:PHP fucntion doesn't run in wordpress. [Creating a Shortcode]PHP 函数不在 wordpress 中运行。 [创建简码]
【发布时间】:2017-01-23 22:38:37
【问题描述】:

我有以下 PHP 代码运行并显示定价表。我希望该函数检查用户是否已经拥有会员资格,如果未找到会员资格,则显示定价表 - MembershipLevel('1'):

    <?php
if(MembershipLevel('1'))
{
    ?>
<div class="PRICING TABLE">
</div>

  <?php
}
else echo "Full Membership Obtained"
?>

现在我想让它成为一个函数。我试过了:

<?php function get_pricing(){
if(MembershipLevel('1'))
{
    ?>
<div class="PRICING TABLE">
</div>
<?php
;}
else echo "Full Membership Obtained";}
?>
<?php get_pricing();
add_shortcode( 'pricing', 'get_pricing' ); ?>

但是当我尝试输入 [定价] 时,短代码不会显示任何内容。事实上,函数 get_pricing();本身不运行。 编辑:该功能确实运行。这就是为什么它让我想知道为什么短代码不起作用。其他主题短代码也可以正常工作。

【问题讨论】:

  • 您是否将; 添加到&lt;?php ;} else echo "Full Membership Obtained";}
  • @chop62 我得到了这个功能,它在页脚下方显示内容,这就是为什么我想将它与一个短代码相关联以在页面上显示它
  • &lt;?php ;} else echo "Full Membership Obtained";} 更改为&lt;?php } else echo "Full Membership Obtained";}
  • @chop62 我做了,没有任何改变。当我调用函数 get_pricing();它显示表格。但是,我不明白为什么它不使用 add_shortcode('pricing', 'get_pricing'); 将短代码与函数相关联。这基本上是 php 代码的最后一行。 get_pricing();单独工作正常
  • 这个代码在functions.php中吗?

标签: php wordpress shortcode


【解决方案1】:

把它放在你的functions.php中

<?php 
  function get_pricing() {
    $text = '';

    if(MembershipLevel('1')) {
      $text = '<div class="PRICING TABLE"></div>';
    } else {
      $text = 'Full Membership Obtained';
    }

    return $text;
  }

  add_shortcode( 'pricing', 'get_pricing' ); 
?>

在帖子中你可以使用

[pricing]

在你可以使用的模板中

echo do_shortcode('pricing');

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多