This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from …
我正在尝试将自定义查询添加到 WordPress 模板并包含分页,但我的分页没有出现,例如,我正在尝试将其添加到 page.php。
我有以下标记,当放置在 category.php 之类的类别模板中时,它可以完美运行,分页显示并且功能正常。问题是在 page.php 或任何自定义页面模板中放置相同的代码时不会出现分页。
查询:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'desc',
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop Markup goes here.
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php pagination(); ?>
Run Code Online (Sandbox Code Playgroud)
在functions.php中定义的分页():
function pagination() {
global $wp_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' …Run Code Online (Sandbox Code Playgroud) 编辑:这是我试图分页的当前代码。它创建一个自定义查询,不包括最新帖子以及一个类别中的所有帖子。分页在大多数情况下都可以正常工作,但是问题是分页列表中的最后一个链接将我带到了空白页。
<section class="card-grid card-grid--push">
<main class="container container--wide">
<?php
$current_page = get_query_var('paged');
$current_page = max( 1, $current_page );
$per_page = 3;
$offset = ( $current_page - 1 ) * $per_page + 1;
$post_list = new WP_Query(array(
'cat' => -15,
'posts_per_page' => $per_page,
'paged' => $current_page,
'offset' => $offset,
'orderby' => 'date',
'order' => 'DESC',
));
if ( $post_list->have_posts() ):
while ( $post_list->have_posts() ):
$post_list->the_post();
?>
<a href="<?php the_permalink(); ?>" <?php post_class('card'); ?>>
<article class="card__content">
<?php the_post_thumbnail('th-card'); ?>
<div class="card__head">
<span class="category"> …Run Code Online (Sandbox Code Playgroud)