如何在_Content过滤器中使用自定义循环

时间:2014-02-22 作者:shubhra

正在尝试将以下代码用于我的CPT“状态”,如add_filter(\'the_content\', \'my_custom_loop\'); 但试错并没有让我有任何收获。我不能使用单个-{post\\u type}。php,因为我使用的是拖放主题间隔。

Update: 我正试图在一个功能插件中使用的确切代码,它给了我一个警告,包括一次打开调试或一个空白的白色屏幕

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\');
感谢您抽出时间。

2 个回复
SO网友:Manny Fleurmond

the_content 应为字符串,您必须使用输出缓冲:

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

SO网友:dj.cowan

出现类似问题,原因是试图在附加到“the\\u content”或“the\\u extract”的操作或筛选器挂钩中使用\\u content()/the\\u extract()

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;
}
我们的解决方案是改变我们的第二个the_content() 功能到:

echo get_the_content( get_the_ID() );

结束

相关推荐

Pagination on Custom Loop

我想将最新帖子的样式与自定义页面上某个类别中的所有其他帖子的样式不同。到目前为止,我有下面的代码,它正是这样做的:<?php if (have_posts()) : ?> <?php query_posts(\"cat=4&showposts=1\"); ?> <?php while (have_posts()) : the_post(); ?> (Style1)<?php the_title(); ?><