猿问

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

我正在使用一个名为 Pay for Post with Woocommerce 的插件,它隐藏了帖子或部分帖子,并允许您使用 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. ?>

在上面的示例中,当我查看尚未购买的页面时,它应该呈现一个链接,其中包含从帖子自定义字段生成的源,但它只是打印字符串内的值:


<img src="{$video_screenshot}">

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


慕雪6442864
浏览 110回答 1
1回答

30秒到达战场

以下是如何稍微重构代码,以更易读的方式回显变量:<?php while ( have_posts() ) : the_post();&nbsp; &nbsp; if(Woocommerce_Pay_Per_Post_Helper::is_protected()){&nbsp; &nbsp; //Page is protected&nbsp; &nbsp; &nbsp; &nbsp; if(Woocommerce_Pay_Per_Post_Helper::has_access()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do what you want to do if they have access to the page ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <iframe width="560" height="315" src="<?php echo the_field("video_link"); ?>" frameborder="0" allowfullscreen></iframe>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <?php } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the page is protected and the user does NOT have access&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo Woocommerce_Pay_Per_Post_Helper::get_no_access_content(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="<?php echo the_field("video_screenshot") ?>" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <?php }&nbsp; &nbsp; } else {&nbsp; &nbsp; //Page is not protected do what you need to do.&nbsp; &nbsp; }&nbsp;endwhile; // end of the loop. ?>
随时随地看视频慕课网APP
我要回答