如果存在一个或多个相关项目,则仅显示滑块


Only show slider if one or more related items exist

我有一个WordPress主题的公文包,在单个公文包页面上有一个相关公文包项目的滑块。这是一个很酷的功能,但即使没有相关项目与特定的投资组合项目匹配,它也会显示滑块。

这是我显示滑块的模板:

<div class="related_portfolio">
    <?php gbx_related_portfolio($post->ID); ?>
</div>

以下是我用来创建滑块的函数:

function get_related_portfolio($post_id) {
$item_cats = get_the_terms($post_id, 'portfolio_category');
if ($item_cats) {
    foreach ($item_cats as $item_cat) $item_array[] = $item_cat->term_id;
}
$args = wp_parse_args($args, array(
    'showposts' => 5,
    'post__not_in' => array($post_id),
    'ignore_sticky_posts' => 0,
    'post_type' => 'portfolio',
    'tax_query' => array(
        array(
            'taxonomy' => 'portfolio_category',
            'field' => 'id',
            'terms' => $item_array
        )
    ),
    'orderby' => 'rand'
));
$query = new WP_Query($args);
return $query;
}

function gbx_related_portfolio($post_id) {
?>
<div class="recentportfolio-wrapper">
    <div class="image-carousel-header">
        <div class="image-carousel-title"><span>Related Items</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 recentportfolio-carousel <?php echo 'recentportfolio-carousel'.$randomid; ?>">
        <ul><?php
            $recentPortfolio = get_related_portfolio($post_id);
            if ($recentPortfolio->have_posts()) {
                while ( $recentPortfolio->have_posts() ) { $recentPortfolio->the_post(); global $post; ?>
                <li class="portfolioslider_item">
                    <div class="portfolio-item">
                        <div class="image">
                            <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, 270, 240).'" alt="'.get_the_title(get_post_thumbnail_id($post->ID)).'"/>';
                                ?>
                            </a>
                            <div class="image-extras">
                                <div class="image-extras-bg"></div>
                                <div class="image-extras-content">
                                    <a class="icon" href="<?php the_permalink(); ?>"><span class="glyphicon glyphicon-paperclip"></span></a>
                                    <a class="icon swipebox" href="<?php echo $full_image; ?>" title="<?php echo get_the_title($post->ID); ?>"><span class="glyphicon glyphicon-search"></span></a>
                                </div>
                            </div>
                        </div>
                        <div class="portfolio-meta">
                            <div class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
                            <div class="pull-left"><span class="glyphicon glyphicon-time"></span><?php echo get_the_date('F j, Y'); ?></div>
                            <div class="pull-right">
                                <span class="likeanimation"></span>
                                <a href="#" class="like <?php echo gbx_get_like_status($post->ID); ?>" title="<?php echo gbx_get_like_title($post->ID); ?>" data_action="likepost" data_postid="<?php echo $post->ID; ?>" data_nonce="<?php echo wp_create_nonce("gbx_like_nonce"); ?>">
                                    <span class="glyphicon glyphicon-heart"></span>
                                    <span class="likecount"><?php echo gbx_get_likecount($post->ID); ?></span>
                                </a>
                                <span class="glyphicon glyphicon-eye-open"></span><?php echo gbx_get_viewcount($post->ID); ?>
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                </li>
            <?php } } ?>
        </ul>
    </div>
</div>
<script>
(function($){
    $(window).load(function() {
        var $recentportfolio_carousel = $('.<?php echo 'recentportfolio-carousel'.$randomid; ?>');
        function custom_recent_portfolio_UpdateSliderHeight(args) {               
            var height = $('.<?php echo 'recentportfolio-carousel'.$randomid; ?> .portfolioslider_item:eq(' + (args.currentSlideNumber-1) + ')').outerHeight(true);
            $recentportfolio_carousel.css({ height: height });
        }
        $recentportfolio_carousel.iosSlider({
            snapToChildren: true,
            desktopClickDrag: true,
            navPrevSelector: $recentportfolio_carousel.parent().find('a.prev'),
            navNextSelector: $recentportfolio_carousel.parent().find('a.next'),
            onSliderLoaded: custom_recent_portfolio_UpdateSliderHeight,
            onSlideChange: custom_recent_portfolio_UpdateSliderHeight,
            onSliderResize: custom_recent_portfolio_UpdateSliderHeight
        });
    });
})(jQuery);
</script>
<?php
}

您的代码中混杂了许多语法错误。我已经把你所有的代码放在一个函数中,重新排列并修复了错误。我不是100%确定这部分,虽然,它是未经测试的

 function gbx_related_portfolio() {
wp_reset_postdata();
global $post;
$orig_post = $post;
if ( 'portfolio' == get_post_type() ) {
$tags = wp_get_post_terms($post->ID, 'portfolio_category');
if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
        'tax_query' => array(
        array(
            'taxonomy'  => 'portfolio_category',
            'terms'     => $tag_ids,
            'operator'  => 'IN'
        )
    ),
    'post__not_in'          => array( $post->ID ),
    'posts_per_page'        => 5,
    'ignore_sticky_posts'   => 0
    );
    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) { ?>
<div class="recentportfolio-wrapper">
    <div class="image-carousel-header">
        <div class="image-carousel-title"><span>Related Items</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 recentportfolio-carousel <?php echo 'recentportfolio-carousel'.$randomid; ?>">
        <ul>
        <?php  while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                <li class="portfolioslider_item">
                    <div class="portfolio-item">
                        <div class="image">
                            <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';
                                ?>
                            </a>
                            <div class="image-extras">
                                <div class="image-extras-bg"></div>
                                <div class="image-extras-content">
                                    <a class="icon" href="<?php the_permalink(); ?>"><span class="glyphicon glyphicon-paperclip"></span></a>
                                    <a class="icon swipebox" href="<?php echo $full_image; ?>" title="<?php echo get_the_title($post->ID); ?>"><span class="glyphicon glyphicon-search"></span></a>
                                </div>
                            </div>
                        </div>
                        <div class="portfolio-meta">
                            <div class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
                            <div class="pull-left"><span class="glyphicon glyphicon-time"></span><?php echo get_the_date('F j, Y'); ?></div>
                            <div class="pull-right">
                                <span class="likeanimation"></span>
                                <a href="#" class="like <?php echo gbx_get_like_status($post->ID); ?>" title="<?php echo gbx_get_like_title($post->ID); ?>" data_action="likepost" data_postid="<?php echo $post->ID; ?>" data_nonce="<?php echo wp_create_nonce("gbx_like_nonce"); ?>">
                                    <span class="glyphicon glyphicon-heart"></span>
                                    <span class="likecount"><?php echo gbx_get_likecount($post->ID); ?></span>
                                </a>
                                <span class="glyphicon glyphicon-eye-open"></span><?php echo gbx_get_viewcount($post->ID); ?>
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                </li>
            <?php endwhile; ?>  
        </ul>
    </div>
</div>
<script>
(function($){
    $(window).load(function() {
        var $recentportfolio_carousel = $('.<?php echo 'recentportfolio-carousel'.$randomid; ?>');
        function custom_recent_portfolio_UpdateSliderHeight(args) {               
            var height = $('.<?php echo 'recentportfolio-carousel'.$randomid; ?> .portfolioslider_item:eq(' + (args.currentSlideNumber-1) + ')').outerHeight(true);
            $recentportfolio_carousel.css({ height: height });
        }
        $recentportfolio_carousel.iosSlider({
            snapToChildren: true,
            desktopClickDrag: true,
            navPrevSelector: $recentportfolio_carousel.parent().find('a.prev'),
            navNextSelector: $recentportfolio_carousel.parent().find('a.next'),
            onSliderLoaded: custom_recent_portfolio_UpdateSliderHeight,
            onSlideChange: custom_recent_portfolio_UpdateSliderHeight,
            onSliderResize: custom_recent_portfolio_UpdateSliderHeight
        });
    });
})(jQuery);
</script>
<?php
}
}
}
$post = $orig_post;
wp_reset_query(); 
}   

只需按以下进行调用

<div class="related_portfolio">
    <?php gbx_related_portfolio(); ?>
</div>