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

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包