简体   繁体   English

为什么相同的代码在 category.php 和 page.php 中表现不同,如何规避?

[英]Why do the same code behave differently in the category.php and page.php and how can this be circumvented?

Please tell me how to solve the problem.请告诉我如何解决这个问题。 I post in a separate category post, which scored more than 100 views.我在一个单独的类别帖子中发帖,该帖子的浏览量超过 100 次。 But if I put it in the category.php, then the last 20-30 posts are clipped and not output.但是如果我把它放在category.php中,那么最后20-30个帖子就会被剪裁而不是输出。 And if the same code is copied into the page.php, then everything is displayed.如果将相同的代码复制到 page.php 中,则会显示所有内容。 Obviously, the built-in behavior of the category hinders.显然,类别的内置行为会阻碍。 How can I fix this?我怎样才能解决这个问题? The post output code is lower.后输出代码较低。

<?php get_template_part('header/header', 'header'); ?>
<?php
$pagedCat = (get_query_var('paged')) ? get_query_var('paged') : 1;
$argsForLast = array(
'orderby'     => 'date',
'posts_per_page' => -1,
'meta_query' => array(
        'count_views' => array(
            'key'     => 'post_views_count',
            'value'   => '100',
            'compare' => '>=',
            'type' => 'NUMERIC'
        ),
    ),
$argsAllPosts = get_posts($argsForLast);
$argsMain = new WP_Query(array(
    'posts_per_page' => 1,
    'meta_query' => array(
        'count_views' => array(
            'key'     => 'post_views_count',
            'value'   => '100',
            'compare' => '>=',
            'type' => 'NUMERIC'
        ),
    ),
));



$lastPostId = $argsAllPosts[0]->ID;


$argsOther = new WP_Query(array(
    'paged' => $pagedCat,
    'post_type' =>  'post',
    'meta_query' => array(
        'count_views' => array(
            'key'     => 'post_views_count',
            'value'   => '110',
            'compare' => '>=',
            'type' => 'NUMERIC'
        ),
    ),
    'orderby' => 'date',
    'order' => 'DESC',
    'post__not_in' => array($lastPostId)
));
$temp = $wp_query;
$wp_query= null;
$wp_query = $argsOther;
?>

And search and output of posts以及帖子的搜索和输出

 <?php if ( $argsOther -> have_posts() ) :?>
   <section class="other-posts-wrap scroll-wrap">
   <?php 
    while ( $argsOther -> have_posts() ) : $argsOther -> the_post();
  ?>

You should reset data before Execute another query您应该在执行另一个查询之前重置数据

wp_reset_postdata();

OR或者

wp_reset_query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM