我有一个使用WordPress的博客,在单个帖子页面上有一个相关帖子的滑块。嗯,这是一个很酷的功能,但即使没有相关的项目匹配该帖子,它也会显示滑块。
我的问题是我可以使用php隐藏滑块并在至少一个项目匹配时调用它吗?这是迄今为止的代码。
这是我的模板,我在其中显示滑块
<?php get_header();
global $smof_data;
list($nosidebar, $content_css, $sidebar_css) = gbx_layout();
?>
<--- Template code--->
<?php gbx_related_post($post->ID); ?>
<--- Other template code--->
<?php get_footer(); ?>
以下是我用来创建滑块的功能:
function gbx_related_post($post_id) {
?>
<div class="recentpost-wrapper">
<div class="image-carousel-header">
<div class="image-carousel-title"><span>Related Posts</span></div>
<div class="image-carousel-nav">
<a class="prev"><i class="fa fa-chevron-left"></i></a><a class="next"><i class="fa fa-chevron-right"></i></a>
</div>
<div class="clear"></div>
</div>
<div class="iosslider recentpost-carousel <?php echo 'recentpost-carousel'.$randomid; ?>">
<ul><?php
$recentPosts = get_related_posts($post_id);
global $disable_section_shortcode;
$disable_section_shortcode_tmp = $disable_section_shortcode;
$disable_section_shortcode = TRUE;
while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
<li class="blogslider_item">
<div>
<div class="blogslider_item_img">
<a href="<?php the_permalink(); ?>"><?php
if('' != get_the_post_thumbnail())
$full_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
else
$full_image = get_template_directory_uri().'/images/thumbnail_placeholder.png';
echo '<img class="size-full" src="' . gbx_process_image($full_image, 150, 150).'" alt="'.get_the_title(get_post_thumbnail_id($post->ID)).'"/>';
?></a>
<?php
$date = get_the_date('d');
$month = get_the_date('M');
?>
<div class="blogslider_item_date"><div><b><?php echo $date; ?></b></div><div><?php echo $month; ?></div></div>
</div>
<div class="blogslider_item_desc">
<a class="blogslider_item_title" href="<?php the_permalink() ?>"><h3><?php the_title(); ?></h3></a>
<div class="blogslider_item_excerpt"><?php
$excerpt = get_the_content(get_the_ID());
$excerpt = do_shortcode($excerpt);
$excerpt = gbx_sections_fix($excerpt);
$excerpt = string_limit_words($excerpt, 15);
echo $excerpt.'...';
?></div>
<div class="blogslider_item_meta"><?php echo get_comments_number( get_the_ID() ); ?> COMMENTS</div>
</div>
<div class="clear"></div>
</div>
</li>
<?php endwhile; wp_reset_postdata();
$disable_section_shortcode = $disable_section_shortcode_tmp;
?>
</ul>
</div>
</div>
<script>
(function($){
$(window).load(function() {
var $recentpost_jcarousel = $('.<?php echo 'recentpost-carousel'.$randomid; ?>');
function custom_recent_posts_UpdateSliderHeight(args) {
var height = $('.<?php echo 'recentpost-carousel'.$randomid; ?> .blogslider_item:eq(' + (args.currentSlideNumber-1) + ')').outerHeight(true);
$recentpost_jcarousel.css({ height: height });
}
$recentpost_jcarousel.iosSlider({
snapToChildren: true,
desktopClickDrag: true,
navPrevSelector: $recentpost_jcarousel.parent().find('a.prev'),
navNextSelector: $recentpost_jcarousel.parent().find('a.next'),
onSliderLoaded: custom_recent_posts_UpdateSliderHeight,
onSlideChange: custom_recent_posts_UpdateSliderHeight,
onSliderResize: custom_recent_posts_UpdateSliderHeight
});
});
})(jQuery);
</script>
<?php
}
和
function get_related_posts($post_id) {
$args = wp_parse_args($args, array(
'showposts' => -1,
'post__not_in' => array($post_id),
'ignore_sticky_posts' => 0,
'category__in' => wp_get_post_categories($post_id)
));
$query = new WP_Query($args);
return $query;
}
答案 0 :(得分:1)
您的代码中存在错误。 $post_id
应为$post->ID
。您还应该先重置发布数据。我能想到的唯一方法是将所有内容组合在一个适当的函数中
您的最新功能应该是
function get_related_posts() {
wp_reset_postdata();
global $post;
$args = wp_parse_args($args, array(
'showposts' => -1,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'ignore_sticky_posts' => 1,
'post__not_in' => array($post->ID),
'category__in' => wp_get_post_categories($post->ID)
));
$query = new WP_Query($args);
if ( $query->have_posts() ) :
?>
<div class="recentpost-wrapper">
<div class="image-carousel-header">
<div class="image-carousel-title"><span>Related Posts</span></div>
<div class="image-carousel-nav">
<a class="prev"><i class="fa fa-chevron-left"></i></a><a class="next"><i class="fa fa-chevron-right"></i></a>
</div>
<div class="clear"></div>
</div>
<div class="iosslider recentpost-carousel <?php echo 'recentpost-carousel'.$randomid; ?>">
<ul><?php
global $disable_section_shortcode;
$disable_section_shortcode_tmp = $disable_section_shortcode;
$disable_section_shortcode = TRUE;
while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="blogslider_item">
<div>
<div class="blogslider_item_img">
<a href="<?php the_permalink(); ?>"><?php
if('' != get_the_post_thumbnail())
$full_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
else
$full_image = get_template_directory_uri().'/images/thumbnail_placeholder.png';
echo '<img class="size-full" src="' . gbx_process_image($full_image, 150, 150).'" alt="'.get_the_title(get_post_thumbnail_id($post->ID)).'"/>';
?></a>
<?php
$date = get_the_date('d');
$month = get_the_date('M');
?>
<div class="blogslider_item_date"><div><b><?php echo $date; ?></b></div><div><?php echo $month; ?></div></div>
</div>
<div class="blogslider_item_desc">
<a class="blogslider_item_title" href="<?php the_permalink() ?>"><h3><?php the_title(); ?></h3></a>
<div class="blogslider_item_excerpt"><?php
$excerpt = get_the_content(get_the_ID());
$excerpt = do_shortcode($excerpt);
$excerpt = gbx_sections_fix($excerpt);
$excerpt = string_limit_words($excerpt, 15);
echo $excerpt.'...';
?></div>
<div class="blogslider_item_meta"><?php echo get_comments_number( get_the_ID() ); ?> COMMENTS</div>
</div>
<div class="clear"></div>
</div>
</li>
<?php endwhile;
$disable_section_shortcode = $disable_section_shortcode_tmp;
?>
</ul>
</div>
</div>
<script>
(function($){
$(window).load(function() {
var $recentpost_jcarousel = $('.<?php echo 'recentpost-carousel'.$randomid; ?>');
function custom_recent_posts_UpdateSliderHeight(args) {
var height = $('.<?php echo 'recentpost-carousel'.$randomid; ?> .blogslider_item:eq(' + (args.currentSlideNumber-1) + ')').outerHeight(true);
$recentpost_jcarousel.css({ height: height });
}
$recentpost_jcarousel.iosSlider({
snapToChildren: true,
desktopClickDrag: true,
navPrevSelector: $recentpost_jcarousel.parent().find('a.prev'),
navNextSelector: $recentpost_jcarousel.parent().find('a.next'),
onSliderLoaded: custom_recent_posts_UpdateSliderHeight,
onSlideChange: custom_recent_posts_UpdateSliderHeight,
onSliderResize: custom_recent_posts_UpdateSliderHeight
});
});
})(jQuery);
</script>
<?php
endif;
wp_reset_query();
}
答案 1 :(得分:-1)
使用以下代码查看gbx_related_post($ post-&gt; ID)中是否有任何内容
<?php if (count($smof_data['post_related_posts']) >= 1): ?>
<?php gbx_related_post($post->ID); ?>
<?php endif; ?>