I use Advanced Custom Fields WordPress plugin and the Flexible Content fields. So in one of the fields I need to include a php file, so I do it like this:
<?php elseif( get_row_layout() == 'block_3_posts' ): ?>
<div class="cb-blog-content block_3_posts"><?php require_once 'thecategorypoststobe.php'; ?></div>
What does this file do? It shows a three most viewed posts from a category. All this is located on a custom single post page. So all the posts are shown successfully but below them all the other Flexible Content fields (if I place them there) are not shown. Above the three posts all fields are displayed normally, but below - not. What could be the reason? I tried make separate loops, put the code directly without the require_once - nothing worked. As for the rest of the functionality - everything works fine, the fields move and alternate, but not under these three posts. This is the code from thecategorypoststobe.php
file -->
<?php
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if ( empty( $first_img ) ) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
$category_id = $cbcategory[0]->cat_ID;
$args_views = array(
'post_type' => 'post',
'cat' => $category_id,
'posts_per_page' => 3,
'meta_key' => 'views',
'orderby' => array( 'meta_value' => 'DESC' ),
);
$wp_query = new WP_Query( $args_views );
if ( $wp_query->have_posts() ) :
while( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="cb-content-most-viewed">
<div class="cb-img-container-most-viewed">
<?php
if ( get_the_post_thumbnail( get_the_ID()) != '' ) {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
the_post_thumbnail();
echo '</a>';
} else {
$image_url = catch_that_image();
$image_id = attachment_url_to_postid( $image_url );
$alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
echo '<img src="';
echo catch_that_image();
echo '" alt="' . $alt . '" />';
echo '</a>';
}
echo '</div>'; // .cb-img-container-most-viewed
?>
<div class="preview-content">
<?php
if ( ! empty( $thecbcategory ) ) {
echo '<a href="' . get_category_link($category_id) . '"><div class="category-name">' . $thecbcategory . '</a></div>';
}
?>
<h3 class="h3-title-blog">
<a href="<?php the_permalink(); ?>" title="Читать дальше">
<?php echo mb_strimwidth( get_the_title(), 0, 60, '...' ); ?>
</a>
</h3>
<div class="cb-post-views">
<img src="/wp-content/uploads/2023/06/views.svg" alt="">
<?php echo do_shortcode('[views]'); ?>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
$wp_query
to a different name: because it's a customWP_Query
, best not to use a variable name that WP core uses. You likely don't needwp_reset_postdata()
, but I could be mistaken on that (I've not needed towp_reset_postdata()
recently).