首页 > 解决方案 > 动态生成的标题和 href 属性未正确添加到锚标记

问题描述

我想返回一个带有动态 href 和标题的链接。这就是我到目前为止所拥有的。我已经可以输出我的动态 ACF 字段get_field('acf_field_name'),但我认为我没有得到下面的串联$buffer = "<a href='".get_field('acf_field_name') "'>".the_title()'</a>';

function qg_shortcode() {
    $buffer = '<h3>Heading</h3>';
    $download = get_field('acf_field_name');
    $q = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => 5
    ));
    while ($q->have_posts()) {
        $q->the_post();
        $buffer = "<a href='".$download"'>".the_title()'</a>';
    }
    wp_reset_postdata();
    return $buffer;
}

你能告诉我有什么问题吗?

标签: phpwordpressfunctionconcatenation

解决方案


concatenation operator(.)在字符串之间丢失了。

$buffer = "<a href='" . $download . "'>" . the_title() . '</a>';

还要确保您是否有可能破坏您的 URL 的特殊字符,对其进行清理。


推荐阅读