服务器与VPS
促销优惠信息

wordpress调用置顶文章代码,纯代码为网站首页添加置顶模块

查看目录
文章目录隐藏
  1. 一、置顶模块效果如图:
  2. 二、wordpress置顶模块代码:
  3. 三、另一段置顶文章调用代码
  4. 四、第三段置顶文章调用代码:
  5. 代码说明:
  6. 五、排除置顶文章的代码:

使用wordpress程序自己建站的朋友们,经常会需要在首页或者分类页调用置顶文章,要实现单独添加置顶文章调用模块方法很多,可以用代码实现也可以用插件来实现。目前大部分主题都可以实现首页显示置顶文章功能了,但是博主本人目前用主题置顶文章和最新文章混在一起,难免有点美中不足,所以,用代码给首页添加一个单独的wordpress置顶文章模块。

一、置顶模块效果如图:

wordpress调用置顶文章代码,纯代码为网站首页添加置顶模块

wordpress置顶文章模块

二、wordpress置顶模块代码:

1、PHP代码:

<?php
$query_post = array(
'posts_per_page' => 10,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1
);
query_posts($query_post);
?>
<?php while(have_posts()):the_post(); ?>

<div class="most-comment-posts">

<ul>
<li>
<a href="<?php%20the_permalink();%20?>">
<?php the_title(); ?> 
</a>
<span class="sticky-icon">置顶</span>

</li>
</ul>
</div>
<?php endwhile; ?>
<?php
wp_reset_query();
?> 

2、CSS代码:

温馨提示: 隐藏内容需要 回复评论 后才能查看, 评论后请 刷新 !.


上面这段wordpress调用置顶文章代码来自CSDN博客文章,原来的代码有图片,但是没有CSS样式,看上去不够美观,所以楚狂人自己进行了一些适合自己的修改。下面贴出原文代码,大家可以根据自己的需求进行个性化修改:

<?php
$query_post = array(
'posts_per_page' => 10,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1
);
query_posts($query_post);
?>
<?php while(have_posts()):the_post(); ?>
<div class="swiper-slide">
<a href="<?php%20the_permalink();%20?>">
<img src="<?php%20the_post_thumbnail_url(%20'full'%20);%20?>" alt="<?php the_title(); ?>">
<div class="shadow">
<?php the_title(); ?>
</div>
</a>
</div>
<?php endwhile; ?>
<?php
wp_reset_query();
?>

参数用一个数组的形式放在$query_post中,关键的参数为'post__in' =>get_option('sticky_posts')和'caller_get_posts' => 0。

'post__in' => get_option('sticky_posts')确定了该 LOOP 调用的是置顶文章列表。

'caller_get_posts'的作用是排除非指定性文章,即除了置顶文章之外,不显示其他的文章。

'posts_per_page' => 10,控制文章的数量

不添加的情况下,如果置顶文章条目不足'posts_per_page'规定的值,会用最新文章替补完整。

三、另一段置顶文章调用代码

<?php 
$args = array( 
'posts_per_page' => -1, 
'post__in' => get_option( 'sticky_posts' ) 
); 
$sticky_posts = new WP_Query( $args ); 
while ( $sticky_posts->have_posts() ) : $sticky_posts->the_post();?> 
<li> 
<a href="<?php%20the_permalink()%20?>"><?php the_title(); ?></a> 
</li> 
<?php endwhile; wp_reset_query();?>

这段代码同样是调用置顶文章,但是这段代码显示的文章含有文章的内容,搞得页面都错位了,看上去修改起来要麻烦很多,所以我最终采用了第一段代码,但是这段代码是可以实现调用置顶文章的,有兴趣的朋友可以自己试试。

四、第三段置顶文章调用代码:

这款代码和上面的代码效果类似,都修改都有点麻烦,但是也许有朋友需要呢?一并转载分享在这里:

<?php if (have_posts()) : ?> 
<p>文章列表如下</p> 
<ul> 
<?php while (have_posts()) : the_post(); 
if (is_sticky()): 
global $more; // 设置全局变量$more 
$more = 1; 
?> 
<li> 
<h2>[置顶]<a href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a><h2/> 
<p><?php the_content(); ?></p> 
</li> 
<?php else: 
global $more; 
$more = 0; 
?> 
<li> 
<h2><a href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a><h2/> 
<p><?php the_content('阅读更多'); ?></p> 
</li> 
<?php endif; ?> 
<?php endwhile; ?> 
</ul> 
<?php else: ?> 
<h2>没有找到更多文章</h2> 
<?php endif; ?>

补充修改后置顶代码:

这段代码是按照上面的代码修改的,之所以要费劲的修改这段wordpress置顶文章调用代码,是因为这款代码可以实现所有置顶文章为一个模块,而本文最开始的那段代码显示的是每篇文章为一个模块,美观度各有不同。

温馨提示: 隐藏内容需要 回复评论 后才能查看, 评论后请 刷新 !.

代码说明:

wordpress置顶文章有两个常用的函数

is_sticky():判断文章是否是置顶的,是就返回true,不是就返回false
get_option('sticky_posts'): 获取置顶文章ID,返回包含各置顶文章ID的数组

首页展示文章时,如果是置顶文章就全文输出

方法简介:在loop循环时,通过 is_sticky()判断是否是置顶文章

是的话就设置全局变量$more=1;然后调用 the_content();就是全文输出了

否则不是置顶文章的话就设置全局变量 $more=0;然后调用 the_content('更多...');就是截取<--more-->标签后的输出

以上文章中原代码来自:https://blog.csdn.net/weixin_30517001/article/details/101390731,我只是修改并添加了CSS样式,如果您觉得文章有用,欢迎收藏分享,谢谢。

另外,以前的文章中楚狂人分享过两款插件,可以实现分类文章置顶,有兴趣的朋友可以移步阅读。

wordpress分类文章置顶使用插件和代码哪一种方式好?

发布时间:     阅读(191)

五、排除置顶文章的代码:

如果想调用排除置顶文章的分类所有文章怎么操作?

<?php
$query_post = array(
'category__in' => array(get_query_var('cat')),//如果是栏目调用,注意这行要加,否则会调用全站所有文章
'posts_per_page' => 5,
'post__not_in' => get_option('sticky_posts'),//排除置顶
'caller_get_posts' => 1
);
query_posts($query_post);
?>
<?php while(have_posts()):the_post(); ?>
<div class="item wow zoomIn">
<div class="img-box">
<img src="<?php%20the_post_thumbnail_url(%20'full'%20);%20?>" alt="<?php the_title(); ?>">
</div>
<div class="text">
<div class="title">
<h3>
<?php the_title(); ?>
</h3> 
</div>
<div class="description">
<p>
<?php the_excerpt(); ?>
</p>
</div>
<div class="more">
<a href="<?php%20the_permalink();%20?>">Read More</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php
wp_reset_query();
?>

 

 

楚狂人 » wordpress调用置顶文章代码,纯代码为网站首页添加置顶模块

相关推荐

  • 暂无文章

评论 抢沙发

  • (必填)
  • (必填)

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏