WordPress分页无法正常工作。

huangapple go评论78阅读模式
英文:

WordPress pagination not working properly

问题

我分析了你的代码并翻译了相关部分:

// 允许的 HTML 标签
$allowed_html = [
    "span" => [
        "class" => []
    ],
    "a" => [
        "class" => [],
        "href" => []
    ]
];

// 页码前后应该出现的内容
$args = [
    'before_page_number' => '<span>',
    'after_page_number' => '</span>'
];

// 打印分页链接
printf('<nav class="pagination">%s</nav>', wp_kses(paginate_links($args), $allowed_html));

请注意,这段代码是用于分页的,但你提到了一个问题,即在点击链接时,.current 类没有更新。如果需要帮助解决此问题,请提供更多相关信息,我将尽力协助你。

英文:

my pagination code:

function tevent_pagination() {

// Allowed html 

$allowed_html = [
    &quot;span&quot; =&gt; [
        &quot;class&quot; =&gt; []
    ],
    &quot;a&quot; =&gt; [
        &quot;class&quot; =&gt; [],
        &quot;href&quot; =&gt; []
    ]
];

// What should be before the page number
$args = [
    &#39;before_page_number&#39; =&gt; &#39;&lt;span&gt;&#39;,
    &#39;after_page_number&#39; =&gt; &#39;&lt;/span&gt;&#39;
];
// Printing the pagination
printf(&quot;&lt;nav class=&#39;pagination&#39;&gt;%s&lt;/nav&gt;&quot;, wp_kses( paginate_links( $args ), $allowed_html ));}

pagination links are correctly displayed, even the content is perfectly display which I click pagination links. but when I click on links the .current class is not updating. I have tried many things but still this issue is not resolving.

答案1

得分: 2

This issue was resolved by writing wp_reset_query() at the top of the function.

英文:

This issue was resolved by writing wp_reset_query() at the top of the function.

答案2

得分: 1

你没有在参数中传递当前页面。

global $wp_query;

$big = 999999999; // 需要一个不太可能的整数
$args = [
    'before_page_number' => '<span>',
    'after_page_number' => '</span>',
    'format' => '?paged=%#%',
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages
];
echo paginate_links($args);
英文:

You are not passing current page on the args.

global $wp_query;

$big = 999999999; // need an unlikely integer
$args = [
    &#39;before_page_number&#39; =&gt; &#39;&lt;span&gt;&#39;,
    &#39;after_page_number&#39; =&gt; &#39;&lt;/span&gt;&#39;,
    &#39;format&#39;  =&gt; &#39;?paged=%#%&#39;,
    &#39;current&#39; =&gt; max( 1, get_query_var(&#39;paged&#39;) ),
    &#39;total&#39;   =&gt; $wp_query-&gt;max_num_pages
];
echo paginate_links($args);

huangapple
  • 本文由 发表于 2023年8月5日 05:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定