0

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

the_content is expecting a string, you'll have to use output buffering:

function my_custom_loop() {

$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;
}

$have_you_read_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;
}
  • Thank you for taking time out to reply. This gives me Warning: include_once with debug turned on. See my updated code above. – shubhra Feb 22 '14 at 23:58
  • What line is it saying is giving the error? – Manny Fleurmond Feb 23 '14 at 0:00
  • I only see a white screen now, with debug on or off – shubhra Feb 23 '14 at 0:13
0

Had a similar issue that was caused by trying to use the_content()/the_excerpt() inside of an action or filter hook attached to 'the_content' or 'the_excerpt'

add_action('the_content','callback');

function callback($content){
    ob_start();
        include '/path/file.php'; // <-- includes additional the_content() call
    $output = ob_get_contents();
    return $content . $output;
}

Our solution was to change our second included the_content() function to:

echo get_the_content( get_the_ID() );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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