09列表调用方式–新手小白,快速使用静态页面建立wordpress主题
09列表调用方式
一,栏目列表名称的相关调用
1.列表栏目名称调用
1.1.列表名称调用采用代码
<?php single_cat_title(); ?>
2.列表描述的调用采用函数:
<?php if (is_category()) {
$category_description = category_description();
if (!empty($category_description)) {
echo '<div class="category-description">' . $category_description . '</div>';
}
} ?>二,栏目列表循环的调用
1.列表循环调用采用代码:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
2.列表循环调用采用函数:
<?php endwhile; endif; ?> 例: <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php endwhile; endif; ?>
举例:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
echo '';
echo '<a href="'.get_permalink().'" title="'.get_the_title().'">';
echo get_the_title();
echo '</a>';
echo '';
endwhile;
else :
echo '暂无相关内容';
endif;
?>
//echo里面 的内容可以自定义
注意:中间的调用连接符采用的是“.”,而不是“+”,“.”是字符串连接符,而“+”是加法运算符。举例二:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php endwhile;
else :
echo '暂无相关内容';
endif;
?>注意:中间的调用采用的是分离式的调用,使用了“”标签,头部的数据查询调用,底部的结束,中间的内容调用。,
但是中间部分的调用也是采用了php标签,但是php标签的结束符是“?>”,而不是“;”。
二,位置导航的调用
1.页面内容自定义位置导航:
<?php if (is_category()) {
$current_cat = get_queried_object();
echo '<a href="'.home_url().'">首页</a> > ';
if ($current_cat->parent) {
$parent_cat = get_category($current_cat->parent);
echo '<a href="'.get_category_link($parent_cat->term_id).'">'.$parent_cat->name.'</a> > ';
}
echo $current_cat->name;
}
?>2.页面内部代码function.php结合调用
function.php里面写位置导航的自定义类:
function custom_breadcrumbs() {
echo '<nav class="breadcrumb" aria-label="Breadcrumb"><ol>';
// 首页链接
echo '<li><a href="'.esc_url(home_url()).'">'.__('Home').'</a></li>';
if (is_category()) {
// 分类页处理
$current_cat = get_queried_object();
if ($current_cat->parent) {
$ancestors = get_ancestors($current_cat->term_id, 'category');
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor) {
$cat = get_category($ancestor);
echo '<li><a href="'.esc_url(get_category_link($cat->term_id)).'">'.$cat->name.'</a></li>';
}
}
echo '<li>'.$current_cat->name.'</li>';
} elseif (is_single()) {
// 文章页处理
$categories = get_the_category();
if ($categories) {
$cat = $categories[0];
if ($cat->parent) {
$ancestors = get_ancestors($cat->term_id, 'category');
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor) {
$parent_cat = get_category($ancestor);
echo '<li><a href="'.esc_url(get_category_link($parent_cat->term_id)).'">'.$parent_cat->name.'</a></li>';
}
}
echo '<li><a href="'.esc_url(get_category_link($cat->term_id)).'">'.$cat->name.'</a></li>';
}
echo '<li>'.get_the_title().'</li>';
}
echo '</ol></nav>';
}前端页面调用当前页面代码:
<?php custom_breadcrumbs(); ?>
注意:上面的代码是写好的,有多种方式,可以自己选择,但是要记得在function.php里面写上位置导航的自定义类,不然会报错。
四:分页代码调用
1.内容页面分页代码书写
<?php the_posts_pagination(array(
'mid_size' => 3,
'prev_text' => __('上一页', 'textdomain'),
'next_text' => __('下一页', 'textdomain'),
'before_page_number' => '<span class="meta-nav screen-reader-text">第</span>',
'after_page_number' => '<span class="meta-nav screen-reader-text">页</span>'
));
?>2.配合function.php调用进行页面调用
function.php里面写分页的自定义类:
//分页函数//
function lypages($query_string = '', $custom_query = null)
{
global $wp_query, $paged;
// 确定使用哪个查询对象
$my_query = $custom_query ? $custom_query : $wp_query;
// 如果提供了查询字符串且没有提供自定义查询对象,则创建新查询
if (!empty($query_string) && !$custom_query) {
// 将查询字符串解析为数组
parse_str($query_string, $query_args);
// 使用WordPress后台设置的每页文章数
if (!isset($query_args['posts_per_page'])) {
$query_args['posts_per_page'] = get_option('posts_per_page');
}
// 确保分页参数正确
if (!isset($query_args['paged'])) {
$query_args['paged'] = get_query_var('paged') ? get_query_var('paged') : 1;
}
$my_query = new WP_Query($query_args);
}
// 获取当前页码
if (empty($paged)) {
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
}
$prev = $paged - 1; // 上一页
$next = $paged + 1; // 下一页
$range = 2; // 当前页前后显示的页码数量
$showitems = ($range * 2) + 1; // 显示的页码按钮总数
// 获取总页数
$pages = $my_query->max_num_pages;
// 只有当总页数大于1时才显示分页
if ($pages > 1) {
echo "<div class='pagination'>";
// 首页链接
if ($paged > 2 && $paged > $range+1 && $showitems < $pages) {
echo "<a href='" . get_pagenum_link(1) . "'>首页</a>";
}
// 上一页链接
if ($paged > 1) {
echo "<a href='" . get_pagenum_link($prev) . "'>上一页</a>";
}
// 页码链接
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
echo ($paged == $i) ?
"<a class='active' href='" . get_pagenum_link($i) . "'>" . $i . "</a>" :
"<a href='" . get_pagenum_link($i) . "' class='inactive'>" . $i . "</a>";
}
}
// 下一页链接
if ($paged < $pages) {
echo "<a href='" . get_pagenum_link($next) . "'>下一页</a>";
}
// 末页链接
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) {
echo "<a href='" . get_pagenum_link($pages) . "'>末页</a>";
}
echo "</div>";
}
}
前端调用分页代码
<?php lypages(''); ?> 注意:上面的代码是写好的,有多种方式,可以自己选择,但是要记得在function.php里面写上分页的自定义类,不然会报错。
五,补充文章列表循环的相关调用
1.文章标题调用
1.1.文章标题调用采用代码:
<?php the_title(); ?>
1.2.文章标题调用采用函数:
<?php echo the_title(); ?>
2.文章缩略图调用
2.1.文章缩略图调用采用代码:
<?php the_post_thumbnail(); ?>
2.2.文章缩略图调用采用函数:
<?php echo the_post_thumbnail(); ?>
3.文章缩略图URL调用
3.1.文章缩略图URL调用采用代码:
<?php the_post_thumbnail_url(); ?>
3.2.文章缩略图URL调用采用函数:
<?php echo the_post_thumbnail_url(); ?>
4.文章缩略图ID调用
4.1.文章缩略图ID调用采用代码:
<?php the_post_thumbnail_id(); ?>
4.2.文章缩略图ID调用采用函数:
<?php echo the_post_thumbnail_id(); ?>
4.3.日期调用
<?php the_time('Y-m-d'); ?>
4.4.时间调用
<?php the_time('H:i:s'); ?>
4.5.作者调用
<?php the_author(); ?>
4.6.作者ID调用
<?php the_author_ID(); ?>
4.7.描述调用
<?php the_excerpt(); ?>
4.8.内容调用
<?php the_content(); ?>
4.9.内容长度调用
<?php the_content_limit(100); ?>
4.10.评论数调用
<?php comments_number('0', '1', '%'); ?>
4.11.评论链接调用
<?php comments_popup_link('0', '1', '%'); ?> 下一篇: 10SEO的关键词以及描述–新手小白,快速使用静态页面建立wordpress主题 »

