Php Echo未从高级自定义字段检索信息。打字错误
对于我在wordpress页面上的博客帖子,我希望内容广告主要指向我网站的其他部分。它的工作很好,因为它从自定义帖子类型“广告”中获得了最合理的帖子,并显示了[内容]中的短代码。然而,我想做的最后一件事是使用高级自定义字段,或者通过下拉选择它应该链接到的所需页面,或者填写自定义url。这就是它停止工作的部分。每当我键入Php Echo未从高级自定义字段检索信息。打字错误,php,html,wordpress,if-statement,advanced-custom-fields,Php,Html,Wordpress,If Statement,Advanced Custom Fields,对于我在wordpress页面上的博客帖子,我希望内容广告主要指向我网站的其他部分。它的工作很好,因为它从自定义帖子类型“广告”中获得了最合理的帖子,并显示了[内容]中的短代码。然而,我想做的最后一件事是使用高级自定义字段,或者通过下拉选择它应该链接到的所需页面,或者填写自定义url。这就是它停止工作的部分。每当我键入echo$buttonur时,我的网站就会崩溃。我哪里有打字错误吗?我已经在wordpress中多次检查了ACF,当我手动将其替换为其中一个字段(如get\u field('pag
echo$buttonur
时,我的网站就会崩溃。我哪里有打字错误吗?我已经在wordpress中多次检查了ACF,当我手动将其替换为其中一个字段(如get\u field('page\u link')
时,它确实起作用。谁能告诉我我做错了什么或帮我一点忙吗
另外,我将在以后完全使用内联样式时删除它。此外,我更习惯于在html中包含php,而不是相反。然而,我使用的代码片段已经包含了很多的代码,我就这么做了
<?php
function my_recent_posts_shortcode($atts)
{
$q = new WP_Query(
array('post_type' => 'advertisements', 'posts_per_page' => '1')
);
$list = '<div class="row col-offset-2" style="background-color: #182027;color:white;padding:1em;color:white;">';
$buttonlink = get_field('page_link');
$buttonURL = get_field('custom_url');
if ($buttonURL != '')
{
$buttonfinallink = $buttonURL;
}
else
{
$buttonfinallink = $buttonlink;
}
while ($q->have_posts()) : $q->the_post();
$list .= '<div class="col-md-9">
<h2 style="margin-bottom:0.3em;font-size:16px;">' . get_the_title() . '</h2>
<br /><div style="color:#ccc;font-size:14px;">' . get_field('content') . '</div>
<a href="' . echo $buttonURL . '" class="link" style="margin-top:0.8em; font-size:0.8em;">' . get_field('link_label') . '</a>
</div>
<div class="col-md-3"><img src="' . get_field('image') . '" style="max-height:75px;"></div>';
endwhile;
wp_reset_query();
return $list . '</div>';
}
add_shortcode('incontent-ad', 'my_recent_posts_shortcode');
?>
您有一个语法错误。当您连接字符串时
不需要回显它
/。。。
//...
而($q->have_posts()):$q->the_post();
$buttonfinallink='';
$buttonlink=get_字段(“页面链接”);
$buttonURL=获取字段(“自定义url”);
如果($buttonURL!=''){
$buttonfinallink=$buttonURL;
}否则{
$buttonfinallink=$buttonlink;
}
$list.='
' . 获取标题()
。获取字段(“内容”)。'
';
结束时;
//...
//...
嗯,谢谢。刚刚注意到另一个打字错误。在该部分中,它不应该是$buttonURL,而应该是$ButtonFinLink。但是,在这种情况下,它仍然不会加载$buttonlink或$buttonurl的值。我不明白。页面链接和自定义url是确切的字段名称。如果我直接使用get_字段(“custom_url”),它可以工作,但由于某种原因它不能作为一个变量。还有一件事,page_link
和custom_url
对于所有广告都是一样的?页面根本不会加载。它只是保持白色。另一个可能更突出的细节是,我把它作为一个插件,而不是放在function.php的底部。页面链接和自定义url的值将根据加载的帖子而变化。。。。。哦,这可能是原因吗?我甚至在它知道从哪里获取值之前就试图定义变量?所以我可能要等一段时间后再放($q…或者它已经知道在wp_查询之后是哪篇帖子了吗?我已经更新了我的答案。是的,你必须把它们放在while
循环中。你把上面的代码放在插件或函数中都没有关系。php
;如果你的插件是活动的,它会工作得很好。谢谢你的时间和努力@Raunak Gupta。现在可以工作了,谢谢谢谢你。
//...
//...
while ($q->have_posts()) : $q->the_post();
$buttonfinallink = '';
$buttonlink = get_field('page_link');
$buttonURL = get_field('custom_url');
if ($buttonURL != '') {
$buttonfinallink = $buttonURL;
} else {
$buttonfinallink = $buttonlink;
}
$list .= '<div class="col-md-9">
<h2 style="margin-bottom:0.3em;font-size:16px;">' . get_the_title() . '</h2>
<br /><div style="color:#ccc;font-size:14px;">' . get_field('content') . '</div>
<a href="' . $buttonfinallink . '" class="link" style="margin-top:0.8em; font-size:0.8em;">' . get_field('link_label') . '</a>
</div>
<div class="col-md-3"><img src="' . get_field('image') . '" style="max-height:75px;"></div>';
endwhile;
//...
//...