dtftao7249656 2017-03-10 12:56
浏览 52
已采纳

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

For my blog posts on my wordpress page i wanted in-content advertising that leads mostly towards other sections of my site. Its works fine as it gets the most reason post from the custom post type 'advertising' to display with the shortcode [in-content]. However last thing i wanted to do is to use advanced custom fields to either have a drop down to select the desired page it should link to, or fill in a custom url. That's the part where it stops working. Whenever i type echo $buttonURL it crashes my site. Do i have a typo somewhere? I have checked ACF in wordpress multiple times and it does work when i manually replace it with either one of them such as get_field('page_link'). Can anyone tell me what I'm doing wrong or help me out a little.

Ps. I'll remove the inline styling at a later point when I'm completely done with it. Also I'm more used to including php in html instead of vice versa. However the snippet I used already contained a lot the code of and I just went with it.

<?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');

?>
  • 写回答

1条回答 默认 最新

  • doupapin172773 2017-03-10 13:09
    关注

    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;
    //...
    //...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵