Search code examples
phpwordpressfunctionconcatenation

Dynamic generated title & href attributes are not added correctly to anchor tag


I would like to return a Link with a dynamic href and title. This is what I have so far. I allready can output my dynamic ACF field get_field('acf_field_name'), but I think I dont get the concatenation right under $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;
}

Can you tell me what's wrong?


Solution

  • You are missing concatenation operator(.) in between string.

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

    Also make sure if you have special characters which can break your URL, sanitize them.