Wordpress高级自定义字段变量未呈现

问题描述 投票:0回答:1

我正在Woocommerce中使用名为Pay for Post的插件,它可以隐藏帖子或帖子的一部分,并允许您使用woocommerce产品出售对该帖子的访问权限。插件作者向我发送了此脚本,可用于创建页面模板:

<?php
if(Woocommerce_Pay_Per_Post_Helper::is_protected()){
   //Page is protected
   if(Woocommerce_Pay_Per_Post_Helper::has_access()){
      // Do what you want to do if they have access to the page
   } else {
      // the page is protected and the user does NOT have access
      echo Woocommerce_Pay_Per_Post_Helper::get_no_access_content();
   }

} else {
   //Page is not protected do what you need to do.
}

我正在尝试创建页面的自定义版本,具体取决于是否购买了该产品,但是我无法让我的高级自定义字段变量在我的页面模板中正确呈现。这是我出于测试目的而创建的。

<?php while ( have_posts() ) : the_post(); ?>



<?php
$video_screenshot = the_field("video_screenshot");
$video_link = the_field("video_link");
    if(Woocommerce_Pay_Per_Post_Helper::is_protected()){
    //Page is protected
        if(Woocommerce_Pay_Per_Post_Helper::has_access()){
            // Do what you want to do if they have access to the page
            echo '<div class="container"><div class="row"><div class="col-12"><iframe width="560" height="315" src="{$video_link}" frameborder="0" allowfullscreen></iframe></div></div></div>';
        } else {
            // the page is protected and the user does NOT have access
            echo Woocommerce_Pay_Per_Post_Helper::get_no_access_content();

            echo '<div class="container"><div class="row"><div class="col-12"><img src="{$video_screenshot}" /></div></div></div>';
        }

    } else {
    //Page is not protected do what you need to do.
    } 
?>

<?php endwhile; // end of the loop. ?>

在上面的示例中,当我查看一个尚未购买的页面时,它应该呈现一个与从post custom字段生成的源的链接,但它只会打印字符串内部的值:

<img src="{$video_screenshot}">

我已经尝试了很多方法,但是我无法确定如何在字符串中包括这些变量并使它们按应有的方式打印。任何帮助将不胜感激!

php wordpress advanced-custom-fields
1个回答
1
投票

这里是您可以稍微重构代码以更易读的方式将变量回显的方法:

<?php while ( have_posts() ) : the_post();

    if(Woocommerce_Pay_Per_Post_Helper::is_protected()){

    //Page is protected

        if(Woocommerce_Pay_Per_Post_Helper::has_access()){

            // Do what you want to do if they have access to the page ?>

            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <iframe width="560" height="315" src="<?php echo the_field("video_link"); ?>" frameborder="0" allowfullscreen></iframe>
                    </div>
                </div>
            </div> 

        <?php } else {

            // the page is protected and the user does NOT have access

            echo Woocommerce_Pay_Per_Post_Helper::get_no_access_content(); ?>

            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <img src="<?php echo the_field("video_screenshot") ?>" />
                    </div>
                </div>
            </div>

        <?php }

    } else {

    //Page is not protected do what you need to do.

    } 

endwhile; // end of the loop. ?>
© www.soinside.com 2019 - 2024. All rights reserved.