Wordpress 高级自定义字段变量未呈现
Wordpress Advanced Custom Fields variables not rendering
我在 Woocommerce 中使用一个名为 Pay for Post 的插件,它隐藏了 posts 或 posts 的一部分,并允许您出售对 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. ?>
在上面的示例中,当我查看尚未购买的页面时,它应该呈现一个 link,其中来源是从 post 自定义字段生成的,但它只是打印字符串内的值:
<img src="{$video_screenshot}">
我已经尝试了很多方法,但我无法确定如何将这些变量包含在字符串中并让它们按应有的方式打印。任何帮助将不胜感激!
您可以通过以下方式稍微重构代码,以更易读的方式回显变量:
<?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. ?>
我在 Woocommerce 中使用一个名为 Pay for Post 的插件,它隐藏了 posts 或 posts 的一部分,并允许您出售对 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. ?>
在上面的示例中,当我查看尚未购买的页面时,它应该呈现一个 link,其中来源是从 post 自定义字段生成的,但它只是打印字符串内的值:
<img src="{$video_screenshot}">
我已经尝试了很多方法,但我无法确定如何将这些变量包含在字符串中并让它们按应有的方式打印。任何帮助将不胜感激!
您可以通过以下方式稍微重构代码,以更易读的方式回显变量:
<?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. ?>