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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错