Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php隐藏项,除非存在一个项_Php_Wordpress - Fatal编程技术网

使用php隐藏项,除非存在一个项

使用php隐藏项,除非存在一个项,php,wordpress,Php,Wordpress,我有一个使用WordPress的博客,在单篇文章页面上有一个相关文章的滑块。嗯,这是一个很酷的功能,但它显示滑块,即使没有相关的项目匹配该职位 我的问题是,是否可以使用php隐藏滑块,并在至少有一项匹配时调用它?这是到目前为止的代码 这是我显示滑块的模板 <?php get_header(); global $smof_data; list($nosidebar, $content_css, $sidebar_css) = gbx_layout(); ?> <--- Tem

我有一个使用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(); ?>

使用以下代码查看gbx_相关文章($post->ID)中是否有任何内容


您的代码中有一个bug
$post\u id
应该是
$post->id
。您还应该先重置post数据。我能想到的正确实现这一点的唯一方法是将所有功能组合在一个适当的函数中

你的最新功能应该是

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();
}
函数获取相关帖子(){
wp_reset_postdata();
全球$员额;
$args=wp\u parse\u args($args,数组)(
“展示帖子”=>-1,
“找不到行”=>真,
'update_post_meta_cache'=>false,
'update_post_term_cache'=>false,
“忽略粘性帖子”=>1,
'post\u not\u in'=>数组($post->ID),
'category\uu in'=>wp\u get\u post\u categories($post->ID)
));
$query=新的WP\u查询($args);
如果($query->have_posts()):
?>
相关职位
评论


(函数($){
$(窗口)。加载(函数(){
变量$recentpost_jcarousel=$('.');
函数自定义\最近发布\更新SlideRheight(args){
变量高度=$(“…blogslider_项:eq(+(args.CurrentSlideEnumber-1)+”).outerHeight(true);
$recentpostjcarousel.css({height:height});
}
$recentpostjcarousel.ioslider({
snapToChildren:没错,
桌面单击拖动:true,
navPrevSelector:$recentpost_jcarousel.parent().find('a.prev'),
navNextSelector:$recentPostjCarousel.parent().find('a.next'),
onSliderLoaded:自定义\最近发布\更新SliderHeight,
onSlideChange:自定义\u最近发布的\u更新SlideRheight,
onSliderResize:自定义\u最近发布\u更新SlideRheight
});
});
})(jQuery);
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;

}
<?php if (count($smof_data['post_related_posts']) >= 1): ?>
    <?php gbx_related_post($post->ID); ?>
<?php endif; ?>
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();
}