0

I have a shortcode function which displays the latest news, the code for a news item is as follows:

$html .= '<div class="latest-news">';
$html .= '<a href="' . get_permalink($recent["ID"]) . '">';
$html .= '<h2>' . $recent["post_title"] . '</h2>';
$html .= '</a>';
$html .= '</div>';

this should output the following HTML:

<div class="latest-news">
    <a href="[permalink]">
        <h2>[post_title]</h2>
    </a>
</div>

For some reason the anchor tag is outputted twice:

<div class="latest-news">
    <a href="[permalink]">
        <br>
        <h2>[post_title]</h2>
    </a>
    <p>
        <a href="[permalink]"></a>
    </p>
</div>

This looks to be happening because of the following code, commenting it out solved the problem:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99) ;
add_filter( 'the_content', 'shortcode_unautop', 100 );

I can't figure out a way to remove the wpautop filter and not output the extra anchor tag.

Shortcode in full:

function latest_news() {

    $args = array( 'numberposts' => 5 );
    $recent_posts = wp_get_recent_posts( $args );

    $html = '';

    $html .= '<div class="latest-news-container">';

    foreach ($recent_posts as $recent) {

        $permalink = get_permalink($recent["ID"]);

        $html .= '<div class="latest-news">';
        $html .= '<a href="' . $permalink . '">';
        $html .= '<div>';
        $html .= '<h2>' . $recent["post_title"] . '</h2>';
        $html .= '</div>';
        $html .= '</a>';
        $html .= '</div>';

    }

    $html .= '</div>';

    return $html;
}
add_shortcode("latest_news", "latest_news");
4
  • Post your entire shortcode Commented Dec 28, 2015 at 12:06
  • Question update with entire shortcode
    – j.grima
    Commented Dec 28, 2015 at 12:24
  • I can't duplicate the problem.
    – s_ha_dum
    Commented Dec 28, 2015 at 14:56
  • I thought your issue was due to old browsers and not using HTML5, but I tested your code and it works as expected. You have a serious issue somewhere, set debug to true and take it from there Commented Dec 28, 2015 at 18:24

1 Answer 1

0

You are trying to remove the filter before adding the filter and one more thing you are not adding the correct priority.

remove_filter( 'the_content', 'wpautop' ); //removing before adding without priorities.
add_filter( 'the_content', 'wpautop' , 99) ;
add_filter( 'the_content', 'shortcode_unautop', 100 );

Try remove the filter at the end with same priority when you add.

add_filter( 'the_content', 'wpautop' , 99) ;
add_filter( 'the_content', 'shortcode_unautop', 100 );
remove_filter( 'the_content', 'wpautop', 99); 
remove_filter( 'the_content', 'shortcode_unautop', 100 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.