如果标签为空,则按标签获取相关帖子按类别获取帖子

时间:2019-04-10 作者:sebzi89

我更多地使用Ajax加载,并尝试通过标记获取相关帖子。工作原理:

$terms = wp_get_post_tags($post->ID); // get current categories
$term_array = []; // Create empty category array

foreach( $terms as $term ) { // Loop founf categories
  $term_array[] = $term->slug;
}

echo do_shortcode(\'[ajax_load_more tag="\'. implode(",", $term_array) .\'" post__not_in="\' . $post->ID . \'" post_type="post" posts_per_page="3" scroll="false" progress_bar="true" progress_bar_color="blue" images_loaded="true" button_label="Load More" css_classes="related-posts-container" button_loading_label="Loading More" container_type="div" ]\');
但当我尝试实现一个功能,当标签为空时,我会按类别获取帖子,这让我崩溃。知道我做错了什么吗?

function getRelatedPostsByTag(){
// Related Posts
$terms = wp_get_post_tags($post->ID); // get current categories
$term_array = []; // Create empty category array

foreach( $terms as $term ) { // Loop founf categories
  $term_array[] = $term->slug;
}

$args = array(
    \'posts_per_page\'    => 1,
    \'post__not_in\'  => $post->ID,
    \'tag\'               => implode(",", $term_array),
    \'post_type\'         => \'post\'
);
$related_posts = get_posts( $args );

$query = \'tag="\'. implode(",", $term_array) .\'"\';

if (count($related_posts) > 1) {
return $query;
}
else {
wp_reset_postdata();
getRelatedPostsByCategory();
}
}

function getRelatedPostsByCategory(){

$categories = wp_get_post_categories($post->ID);
$categories_array = [];

foreach ($categories as $category) {
    $categories_array[] = $category->CatSlug;
}
$query = \'category="\'. implode(",", $categories_array) .\'"\';

return $query;
}

wp_reset_postdata();

echo do_shortcode(\'[ajax_load_more \'. getRelatedPostsByTag() .\' post__not_in="\' . $post->ID . \'" post_type="post" posts_per_page="3" scroll="false" progress_bar="true" progress_bar_color="blue" images_loaded="true" button_label="Load More" css_classes="related-posts-container" button_loading_label="Loading More" container_type="div" ]\');

1 个回复
SO网友:sebzi89

我调用的是类别id,而不是类别名称。

以下是默认情况下尝试使用标签引入相关帖子的代码,如果没有标签的帖子,则会引入使用相同类别的帖子。此代码在单个中工作。php使用Ajax加载更多wordpress插件。

// Related Posts by tag
$terms = wp_get_post_tags($post->ID); // get current categories
$term_array = []; // Create empty category array

foreach( $terms as $term ) { // Loop founf categories
$term_array[] = $term->slug;
}
$query = \'\';

$args = array(
\'posts_per_page\'    => 1,
\'post__not_in\'  => $post->ID ,
\'tag\'               => implode(",", $term_array),
\'post_type\'         => \'post\'
);
$related_posts = get_posts( $args );
if ($related_posts) { // Tags

$query = \' tag="\'. implode(",", $term_array) .\'"\';

} else {

$categories = wp_get_post_categories($post->ID);

if($categories){
$categories_array = [];
foreach ($categories as $category) {
$cat = get_term( $category );
$categories_array[] = $cat->slug;
}
if(!empty($categories_array)){
$query = \' category="\'. implode(",", $categories_array) .\'"\';
}
}

}

echo do_shortcode(\'[ajax_load_more\'. $query .\' post__not_in="\' . $post->ID . \'" post_type="post" posts_per_page="3" scroll="false" progress_bar="true" progress_bar_color="blue" images_loaded="true" button_label="Load More" css_classes="related-posts-container" button_loading_label="Loading More" container_type="div" ]\');

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post