how to use custom loop in the_content filter

Question

Trying to use the the below code for my CPT ‘state’ like add_filter('the_content', 'my_custom_loop'); but the trial and error is not getting me anywhere. I can’t use a single-{post_type}.php since i am using a drag and drop theme headway.

Update: exact code I am trying to use in a functionality plugin and it gives me a warning include-once with debug on or a blank white screen

function my_custom_loop() {
if( is_singular('state') && is_main_query() ) {
$taxonomy = 'state_cat';

  $terms = get_the_terms($post->ID, $taxonomy);

  if ($terms && ! is_wp_error($terms)) : 


$terms_array = array();

foreach ($terms as $term) {
  $terms_array[] = $term->slug;
}

$my_query = new WP_Query(
  array(
    'posts_per_page' => 100,
    'post_type' => 'post',
    'post__not_in' => array($post->ID),
    'category__not_in' => 418,
    'tax_query' => array(
      array(
        'taxonomy' => $taxonomy,
        'field' => 'slug',
   'include_children' => false,
        'terms' => $terms_array
      )
    )
  )
);
 //Stores html in buffer
 ob_start();
 if($my_query->have_posts()) :


   while($my_query->have_posts()) : $my_query->the_post(); ?>

    <div class="custom-loop clearfix">
            <div class="loop-thumb"><?php the_post_thumbnail('thumbnail'); ?></div>
            <div class="loop-content">
                <div class="loop-title"><?php the_title(); ?></div>
                <div class="loop-excerpt"><?php the_excerpt(); ?></div>
            </div>
    </div>
  <?php endwhile; wp_reset_postdata(); 


   endif; 

 endif; 
}
 //get buffer contents
 $output = ob_get_clean();

 return $output;
}
add_filter('the_content', 'my_custom_loop');

Thank you for your time.

0
, shubhra 1 year 2020-02-24T08:38:37-05:00 0 Answers 81 views 0

Leave an answer