Echo 未从高级自定义字段中检索信息。打字错误?

Echo not retrieving info from advanced custom fields. Typo?

对于我的博客 posts 在我的 wordpress 页面上,我想要主要指向我网站其他部分的内容内广告。它工作正常,因为它从自定义 post 类型 'advertising' 中获得最多的理由 post 以显示简码 [in-content]。然而,我想做的最后一件事是使用高级自定义字段下拉到 select 它应该 link 到的所需页面,或者填写自定义 url。那是它停止工作的部分。每当我输入 echo $buttonURL 时,我的网站就会崩溃。我有错字吗?我已经在 wordpress 中检查了 ACF 多次,当我手动将其替换为其中之一(例如 get_field('page_link') 时,它确实有效。任何人都可以告诉我我做错了什么或帮助我一点。

Ps。当我完全完成它时,我将在稍后删除内联样式。此外,我更习惯于在 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');

?>

You have a syntax error. As you are concatenating string then you don't need to echo it.

//...
//...
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;
//...
//...