自己制作的WordPress主题网站首页或者网站侧边栏希望能够让置顶文章自动显示在前面.
WordPress文章列表 把鼠标放到你要置顶的文章上面,点击快速编辑.
然后点击箭头所指之处的置顶这篇文章就OK了.
装了WordPress缓存加速软件的朋友记得更新缓存.
<ul> <?php $sticky = get_option('sticky_posts'); query_posts( array('post__in' => $sticky,'caller_get_posts' =>1,'cat'=>1,'showposts'=>8)); static $case_num=0; while (have_posts()) : the_post(); ?> <!--WordPress置顶文章 样式修改开始--> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <!--WordPress置顶文章 样式修改结束--> <?php $case_num++; endwhile; wp_reset_query(); $case_num=8-$case_num;// query_posts( array( 'post__not_in' => get_option( 'sticky_posts'),'cat'=>1,'showposts'=>$case_num )); while (have_posts()) : the_post(); ?> <!--WordPress非置顶文章 样式修改开始--> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <!--WordPress非置顶文章 样式修改结束--> <?php endwhile; wp_reset_query(); ?> </ul>
注意事项:
‘cat’=>1 设置调用的分类目录ID号;
‘showposts’=>8 设置调用文章的总数量;
<ul> <?php $sticky = get_option('sticky_posts'); query_posts( array('post__in' => $sticky,'caller_get_posts' =>1,'cat'=>1,'showposts'=>8)); static $case_num=0; while (have_posts()) : the_post(); ?> <!--WordPress置顶文章 样式修改开始--> <div class="panel clearfix"> <div class="panel-body"> <div class="panel-body-img col-md-4"> <a href="<?php the_permalink(); ?>"> <?php if ( $values = get_post_custom_values("suoluetu") ) { ?> <img class="suoluetu" src="<?php $values = get_post_custom_values("suoluetu"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /> <?php } else { ?> <img class="suoluetu" src="https://img.wpyi.com/images/suoluetu.gif" alt="<?php the_title(); ?>" /> <?php } ?> </a> </div> <div class="panel-body-text col-md-8"> <div class="panel-body-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <div class="index_main_info"> <span><i class="glyphicon glyphicon-calendar"></i><a href="<?php the_permalink(); ?>"><?php the_time( 'Y-m-d' ) ?></a></span> <span><i class="glyphicon glyphicon-eye-open"></i><a href="<?php the_permalink(); ?>"><?php echo getPostViews(get_the_ID()); ?></a></span> <span style="display:none;"><i class="glyphicon glyphicon-edit"></i><a href="<?php the_permalink(); ?>"> 2 评论</a></span> </div> <div class="index_main_text"><p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0,240,"..."); ?></p></div> </div> </div> </div> <!--WordPress置顶文章 样式修改结束--> <?php $case_num++; endwhile; wp_reset_query(); $case_num=8-$case_num;// query_posts( array( 'post__not_in' => get_option( 'sticky_posts'),'cat'=>1,'showposts'=>$case_num )); while (have_posts()) : the_post(); ?> <!--WordPress非置顶文章 样式修改开始--> <div class="panel clearfix"> <div class="panel-body"> <div class="panel-body-img col-md-4"> <a href="<?php the_permalink(); ?>"> <?php if ( $values = get_post_custom_values("suoluetu") ) { ?> <img class="suoluetu" src="<?php $values = get_post_custom_values("suoluetu"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /> <?php } else { ?> <img class="suoluetu" src="https://img.wpyi.com/images/suoluetu.gif" alt="<?php the_title(); ?>" /> <?php } ?> </a> </div> <div class="panel-body-text col-md-8"> <div class="panel-body-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <div class="index_main_info"> <span><i class="glyphicon glyphicon-calendar"></i><a href="<?php the_permalink(); ?>"><?php the_time( 'Y-m-d' ) ?></a></span> <span><i class="glyphicon glyphicon-eye-open"></i><a href="<?php the_permalink(); ?>"><?php echo getPostViews(get_the_ID()); ?></a></span> <span style="display:none;"><i class="glyphicon glyphicon-edit"></i><a href="<?php the_permalink(); ?>"> 2 评论</a></span> </div> <div class="index_main_text"><p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0,240,"..."); ?></p></div> </div> </div> </div> <!--WordPress非置顶文章 样式修改结束--> <?php endwhile; wp_reset_query(); ?> </ul>
注意事项:
‘cat’=>1 设置调用的分类目录ID号;
‘showposts’=>8 设置调用文章的总数量;