我希望在内容之后运行函数“catloop”。而不仅仅是将其放置在_content()下面;我想用正确的方法来做。还有一个社交插件,在我需要的所有内容之后放置一些共享功能,因此,我对过滤器有更高的优先级。这是我的函数文件中的内容:
function catloop($content) {
if(is_page_template(\'page-category.php\')) {
function foo() {
$term = get_field(\'category_list\');
$term_id = $term->term_id;
$catname = $term->name;
// assign the variable as current category
// concatenate the query
$args = \'cat=\' . $term_id;
if( $term ): $showposts = 10;
$args = array(\'cat\' => $term_id, \'orderby\' => \'post_date\', \'order\' => \'DESC\', \'posts_per_page\' => $showposts,\'post_status\' => \'publish\');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
echo \'<div class="timeline">\';
echo \'<a href="\';
echo the_permalink();
echo \'"><h3>\';
echo the_title();
echo \'</h3></a>\';
endwhile; else:
_e(\'No Posts Sorry.\');
endif;
echo \'</div>\';
endif;
wp_reset_query();
}
$foo = foo;
$new_content = $foo();
$content .= $new_content;
}
return $content;
}
add_filter(\'the_content\', \'catloop\',9);
它正在发挥作用,但问题是它将此置于内容之上。我在其他问题中读到,我需要使用“return”而不是“echo”,但它会中断。不知道接下来该怎么办。
SO网友:sdexp
如果是我,我不会回显文本,我会将其设为变量,例如。$additional_text = ""
然后在函数末尾return $additional_text;
.
if (have_posts()) : while (have_posts()) : the_post();
$additional_text = \'<div class="timeline">\';
$additional_text .= \'<a href="\';
$additional_text .= the_permalink();
$additional_text .= \'"><h3>\';
$additional_text .= the_title();
$additional_text .= \'</h3></a>\';
$additional_text .= \'</div>\';
endwhile; else:
$additional_text = \'No Posts Sorry.\';
endif;
然后,当您调用函数时,它将获取变量,而不是写入页面。
SO网友:Jason Gray
好的,我成功了:
function catloop($content) {
if(is_page_template(\'page-category.php\')) {
function foo() {
$term = get_field(\'category_list\');
$term_id = $term->term_id;
$catname = $term->name;
// assign the variable as current category
// concatenate the query
$args = \'cat=\' . $term_id;
$args = array(\'cat\' => $term_id, \'orderby\' => \'post_date\', \'order\' => \'DESC\', \'posts_per_page\' => $showposts,\'post_status\' => \'publish\');
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
$perma = get_permalink();
$title = get_the_title();
$loopy .= \'<div class="timeline"><a href="\'. $perma . \'"><h3>\' . $title . \'</h3></a>\';
endwhile; else:
_e(\'No Posts Sorry.\');
endif;
endif;
wp_reset_query();
return $loopy;
}
$foo = foo;
$new_content = $foo();
$fullcontent = $content . $new_content;
}
return $fullcontent;
}
add_filter(\'the_content\', \'catloop\',9);
如果有人有更优雅的解决方案,我将不胜感激。感谢Pedro Coitinho和sdexp