登录  /  注册
博主信息
博文 49
粉丝 0
评论 0
访问量 47794
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
wordpress search页面标签调用
lus菜
原创
223人浏览过

wordpress search页面标签调用

<!-- 分页标题 -->

<h1><?php the_search_query(); ?></h1>


<?php
// 获取当前分页的页码。如果当前页码存在(get_query_var(‘paged’)),则使用它;如果没有,则默认为第一页(1)。
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
// get_search_query() 用于获取当前的搜索词。
‘s’ => get_search_query(),
// paged 设置了当前页码,确保分页在查询中生效。
‘paged’ => $paged,
);
// 使用 new WP_Query($args) 创建一个新的查询对象,执行搜索,并处理分页。
$the_query = new WP_Query($args);
// 循环输出查询结果 have_posts() 检查是否有符合查询条件的文章。
if ($the_query->have_posts()) :
// the_post() 设置当前文章数据,准备在循环中显示该文章的内容。
while ($the_query->have_posts()) : $the_query->the_post();
?>
<div class="col medium_6">
<div class="card advice-centre">
<div class="home-widget-container">
<div class="img" style="background-image:url(<?php echo catch_that_image(); ?>)">
</div>
<div class="content">
<a href="<?php echo get_permalink(); ?>" style="text-decoration: none; color: inherit;">
<!-- 文章标题 -->
<h3 style="line-height: 29px;font-size: 21px;"><?php the_title(); ?></h3>
<!-- 文章标题 字数限制 -->
<h3 class="title"><?php echo mb_strimwidth(get_the_title(), 0, 20, ‘…’); ?></h3>
<!-- 文章内容详情 字数限制 -->
<p><?php echo mb_strimwidth(get_the_excerpt(), 0, 100, ‘…’); ?></p>
<div class="btn primary">Read more</div>
</a>
</div>
</div>
</div>
</div>
<?php endwhile;
endif; ?>


<div class="paidax">
<?php
// 实际的分页页码。
$big = 999999999;
// paginatelinks() 函数 用于生成分页的 HTML 链接
echo paginatelinks(array(
// ‘base’:定义分页链接的基本结构。strreplace($big, ‘%#%’,
‘base’ => strreplace($big, ‘%#%’, esc_url(get_pagenum_link($big))),
// ‘format’:指定分页链接的 URL 格式。这里是 ‘?paged=%#%’,意思是分页链接的 URL 将是 ?paged=1、paged=2 等
‘format’ => ‘?paged=%#%’,
// ‘current’:当前页码,max(1, get_query_var(‘paged’)) 获取当前的页码,
‘current’ => max(1, get_query_var(‘paged’)),
// ‘total’:总页数,$the_query->max_num_pages 返回自定义查询($the_query)的总页数。如果是默认的查询,则可以使用 wp_query。
‘total’ => $the_query->max_num_pages,
// ‘prev_text’ 和 ‘next_text’:这些是“上一页”和“下一页”的文字。
‘prev_text’ =>
(‘« Previous’),
‘next_text’ =>
(‘Next »’),
));
// 重置 WordPress 的全局 $post 对象,以避免影响后续的查询或循环。通常在使用自定义查询(例如 $the_query)后,需要调用 wp_reset_postdata() 来恢复全局查
wp_reset_postdata();
?>
<!-- 按钮样式 -->
<style>
.paidax {
text-align: center;
margin: 20px 0;
}

.paidax a,
.paidax span {
display: inline-block;
padding: 10px 15px;
margin: 0 5px;
background-color: #f1f1f1;
color: #333;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s, color 0.3s;
}

.paidax a:hover {
background-color: #0073aa;
color: #fff;
}

.paidax .current {
background-color: #0073aa;
color: #fff;
font-weight: bold;
}
</style>
</div>

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学