WordPress主题如何在首页和列表页添加置顶文章功能
1、修改首页 index.php 文件
直接编辑主题的 index.php 文件,找到以下代码
<div id="post_list_box" class="border_gray">
在它下方添加以下代码:
<?php
$sticky = get_option( 'sticky_posts' );
query_posts( array('post__in' => $sticky,'showposts'=>3) );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" class="archive-list <?php $postds=$wp_query->current_post;if($postds % 2 ==0){echo "shuangshu";}?>">
<?php get_template_part( '/inc/content'); ?>
</article><!-- #post -->
<?php endwhile;endif; ?>
2、修改列表页 archive.php 文件
直接编辑 Blogs 主题的 archive.php 文件,找到以下代码
<div id="post_list_box" class="border_gray">
在它下方添加以下代码:
<?php
query_posts(array(
"category__in" => array(get_query_var("cat")),
"post__in" => get_option("sticky_posts"),
'showposts' => 3,
)
);
while(have_posts()) : the_post();
?>
<article id="post-<?php the_ID(); ?>" class="archive-list <?php $postds=$wp_query->current_post;if($postds % 2 ==0){echo "shuangshu";}?>">
<?php get_template_part( '/inc/content'); ?>
</article><!-- #post -->
<?php endwhile;wp_reset_query(); ?>
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'category__in' => array(get_query_var("cat")),
'post__not_in' => $sticky,
'paged' => $paged
);
query_posts( $args );
?>
3、修改 inc\content.php 文件
直接编辑 inc\content.php 文件,找到文章标题代码:
<?php the_title(); ?>
修改为
<?php if(is_sticky()){echo '【置顶】';}the_title(); ?>
至此,已经为 WordPress 版本的 Blogs 主题添加了置顶文章功能,接下来只需要在后台编辑文章的时候,在公开度 >> 公开中勾选“将文章置于首页顶端”或快速编辑勾选“置顶这篇文章”即可。版权保护: 转载请保留链接: https://cgy.isi8.cn/Wordpress/96.html
