Browser: Edge
OS: Windows
URL: localhost
Video:
I use Bricks Builder’s {echo:} to call my custom function, and it works normally.
However, when using echo to call the function that returns HTML content, it automatically adds tags before the tags. I know this is an issue related to autoP, but I have already handled it in the original function, and it does not automatically add tags. This only happens with the HTML rendered after processing through {echo:my_function()}.
This troubles me a lot. How can I avoid using {echo:} to call a custom function without automatically adding tags?
Here is my custom function code in function.php
/**
* Display Random Posts
*
* @param int $number_of_posts
* @return string rendered html
*/
function show_rand_posts($number_of_posts = 5) {
ob_start();
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => $number_of_posts,
'ignore_sticky_posts' => 1,
'no_found_rows' => true,
'fields' => 'ids'
);
$query = new WP_Query($args);
$default_img = apply_filters('rand_posts_default_image', 'http://images.xxx/default-img/thumbnail-default-image.png');
echo '<div class="widget-rand-posts">';
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$permalink = esc_url(get_permalink());
$title = esc_html(get_the_title());
$post_date = get_the_date('Y-m-d');
$thumbnail = has_post_thumbnail() ?
get_the_post_thumbnail_url($post_id, 'medium') :
$default_img;
?>
<div class="widget-rand-post__wrapper">
<a href="<?php echo $permalink; ?>" aria-label="<?php echo $title; ?>">
<img class="widget-rand-post__thumbal"
src="<?php echo $thumbnail; ?>"
alt="<?php echo $title; ?>"
loading="lazy">
</a>
<div class="widget-rand-post__body">
<h3 class="widget-rand-post__title">
<a href="<?php echo $permalink; ?>"><?php echo $title; ?></a>
</h3>
<time class="widget-rand-post__date" datetime="<?php echo $post_date; ?>">
<?php echo $post_date; ?>
</time>
</div>
</div>
<?php
}
wp_reset_postdata();
} else {
echo '<p class="no-posts">' . esc_html__('No posts found.', 'your-text-domain') . '</p>';
}
echo '</div>';
return ob_get_clean();
}