这个是从本站INLOBP主题的Banner区文章自动切换中抽出来的,实际上就是通过query_posts来查询文章id然后在主循环里面输出来。
注意
在输出之前值得注意的是不能将这个主循环与首页的主循环文章列表放在一起,必须要另外单独写一个主循环函数,然后通过函数来输出。按我个人浅薄的理解:也就是说不能在同一个页面同时存在两个不同的主循环。
自定义函数输出该主循环
下面都注释好了,从本站目前使用的INLOBP主题上的文章切换效果中抽取的部分代码,可以删掉其中的判断语句,直接将文章id数组变量$slider_array换成你的文章id就可以看到效果,这个函数是 inlo_slider() ,另外里面的inlo_substr()函数是我自己的文章摘要输出,你也可以换成你自己的摘要函数。(实际上真正有用的是<div class="slider_post">...</div>这里面的代码)
<?php function inlo_slider(){ $is_slider = inlo_options('is_slider'); $slider_post_id = inlo_options('slider_post_id'); $slider_array = explode(',', $slider_post_id); // 以逗号为分割符 分割字符赋予该数组 if( $is_slider == 'Y' && !empty($slider_array) ){ // 若主题设置开启 并且 文章id不为空 ?> <div id="slider" class="tra"> <div class="slider_post"> <?php $posts = query_posts(array( 'post__in' => $slider_array // 文章id数组 )); ?> <?php if (have_posts()) : while (have_posts()) : the_post();?> <div class="slider_content"> <h1><a href="<?php the_permalink() ?>" class="tra"><?php the_title();?></a></h1> <div class="slider_p"> <p> <?php $desc = has_excerpt(); if ( ! $desc ) { $post_content = preg_replace( '/(\s\:.*\:\s)|(<\!--inlo_hide_start-->([\s\S]*)<\!--inlo_hide_end-->)/i', '', get_the_content() ); echo inlo_substr( strip_tags( $post_content ), 260, '<span class="read-more" style="margin-left:5px;color:#aaa">...</span>' ) ; } else { the_excerpt(); } ?> </p> </div> </div> <?php endwhile; endif; $posts = null; wp_reset_query(); ?> </div> </div> <?php } } ?>
引用方法
其实就是输出这个函数,在你希望插入该循环的地方放置下面的代码
<?php inlo_slider(); ?>
不是很复杂。
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/4412.html