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");