【问题标题】:Two custom loops on same page in wordpress?wordpress同一页面上的两个自定义循环?
【发布时间】:2018-03-04 20:44:12
【问题描述】:

我在这里找不到任何东西,所以我把这个问题带给你。下面的代码主要做我想要的,只有页面循环不起作用:当我想导航到旧帖子时,看到的页面再次显示相同的新帖子。我该如何正确地做到这一点?

所以 index.php 有两个循环:

<?php
  $args = array( 'numberposts' => 1, 
    'post_status'=>"publish",
    'post_type'=>"post",
    'orderby'=>"post_date");
  $postslist = get_posts( $args );
  foreach ($postslist as $post) :  setup_postdata($post); 
?>

  // regular post html with wp tags

<?php endforeach; ?>

<?php
  query_posts('posts_per_page=12&offset=1');
  if (have_posts()) : ?>

<h3>Headline</h3>

<?php while (have_posts()) : the_post(); ?>

  // regular html with wp tags for these posts (teasers, with the_excerpt)

  <?php endwhile; ?>

    <div class="navigation">
      <span class="nextlink"><?php next_posts_link( 'Older', 0 ); ?></span>
      <span class="previouslink"><?php previous_posts_link( 'Newer' ); ?></span>
    </div>

    <?php else : ?>

    <h3><?php _e('Nothing found'); ?></h3>

  <?php endif; ?>

解释如下: 一个人访问博客:最新的博文在一个区块中全长显示,12 篇较旧的博文(不包括第一个带有offset 的博文,所以 2-13)在另一个区块中显示块与内容预告片和底部的下一个上一个导航显示帖子预告片 14-25 等等。 (问题:总是2-13)

// 编辑:响应顶部答案的代码,现在是 index.php 的完整代码。请注意,HTML 已更改,因为我使用了规范化的 HTML,因为它通常无关紧要。这次是实际代码。

<?php get_header(); ?>

<div class="box blog-block">
  <?php
    $args = array( 'numberposts' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
    $postslist = get_posts( $args );
     foreach ($postslist as $post) :  setup_postdata($post); ?>
  <span class="head"><?php the_time('j. F Y') ?></span>
  <h3><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
  <div class="entry">
    <?php global $more;
          $more = 1;
          the_content();
    ?>
    <p class="author"><?php the_author(); ?></p>
    <p><a class="more-link" href="<?php the_permalink(); ?>#respond" title="<?php the_title();?>">Kommentieren</a>
  </div>

    <?php endforeach;
    wp_reset_postdata(); /* 1. reset post data */
    ?>

</div>

<div class="box" id="recentposts">

  <?php

  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

  $display_count = 12;
  $excluded_count = 1;        
  $offset = ( ($paged-1) * $display_count ) + $excluded_count;

  $args = array(
      'post_status'=>"publish",
      'post_type'=>"post",
      'orderby'=>"post_date",
      'posts_per_page' => $display_count,
      'offset' => $offset,
      'paged' => $paged
  );

  $the_query = new WP_Query( $args );
  if ( $the_query->have_posts() ) : ?>

  <h3>Bisherige Artikel</h3>

  <div class="recentpostsmasonry">

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div <?php post_class('halb'); ?> id="post-<?php the_ID(); ?>">
      <span class="head"><?php the_time('j. F Y') ?></span>
      <h3><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
      <div class="entry">
        <?php the_excerpt(); ?>
        <p class="more-link"><a href="<?php the_permalink(); ?>">Weiterlesen ...</a></p>
        <p class="comments"><?php edit_post_link(__('Bearbeiten'), '', ' | '); ?> <?php comments_popup_link(__('Schreib den ersten Kommentar'), __('Ein Kommentar'), __('% Kommentare'), '', __('') ); ?></p>
      </div>
    </div>

    <?php endwhile; ?>

  </div>

  <div class="navigation">
    <span class="nextlink" title="Ältere Einträge"><?php next_posts_link( '«', 0 ); ?></span>
    <span class="previouslink" title="Jüngere Einträge"><?php previous_posts_link( '»' ); ?></span>
  </div>

  <?php else : ?>

  <h3><?php _e('Nichts gefunden :('); ?></h3>

  <?php endif; ?>

</div>

我现在正在试用此代码(14.10.17),但它没有添加功能页面导航...:

<?php get_header(); ?>

<div class="box blog-block">
  <?php
    $args = array( 'numberposts' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
    $postslist = get_posts( $args );
     foreach ($postslist as $post) :  setup_postdata($post); ?>
  <span class="head"><?php the_time('j. F Y') ?></span>
  <h3><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
  <div class="entry">
    <?php global $more;
          $more = 1;
          the_content();
    ?>
    <p class="author"><?php the_author(); ?></p>
    <p><a class="more-link" href="<?php the_permalink(); ?>#respond" title="<?php the_title();?>">Kommentieren</a>
  </div>

    <?php endforeach;
    wp_reset_postdata(); /* 1. reset post data */
    ?>

</div>

<div class="box" id="recentposts">

  <?php

  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

  $display_count = 12;
  $excluded_count = 1;
  $offset = ( ($paged-1) * $display_count ) + $excluded_count;

  $args = array(
      'post_status'=>"publish",
      'post_type'=>"post",
      'orderby'=>"post_date",
      'posts_per_page' => $display_count,
      'offset' => $offset,
      'paged' => $paged
  );

  $the_query = new WP_Query( $args );
  if ( $the_query->have_posts() ) : ?>



  <h3>Bisherige Artikel</h3>

  <div class="recentpostsmasonry">

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div <?php post_class('halb'); ?> id="post-<?php the_ID(); ?>">
      <span class="head"><?php the_time('j. F Y') ?></span>
      <h3><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
      <div class="entry">
        <?php the_excerpt(); ?>
        <p class="more-link"><a href="<?php the_permalink(); ?>">Weiterlesen ...</a></p>
        <p class="comments"><?php edit_post_link(__('Bearbeiten'), '', ' | '); ?> <?php comments_popup_link(__('Schreib den ersten Kommentar'), __('Ein Kommentar'), __('% Kommentare'), '', __('') ); ?></p>
      </div>
    </div>

    <?php endwhile; ?>

  </div>

  <div class="navigation">
    <span class="nextlink" title="Ältere Einträge"><?php next_posts_link( '«', 0 ); ?></span>
    <span class="previouslink" title="Jüngere Einträge"><?php previous_posts_link( '»' ); ?></span>
  </div>

  <?php else : ?>

  <h3><?php _e('Nichts gefunden :('); ?></h3>

  <?php endif; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

新:

<?php get_header(); ?>

<div class="box blog-block">
  <?php
    $args = array( 'numberposts' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
    $postslist = get_posts( $args );
     foreach ($postslist as $post) :  setup_postdata($post); ?>
  <span class="head"><?php the_time('j. F Y') ?></span>
  <h3><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
  <div class="entry">
    <?php global $more;
          $more = 1;
          the_content();
    ?>
    <p class="author"><?php the_author(); ?></p>
    <p><a class="more-link" href="<?php the_permalink(); ?>#respond" title="<?php the_title();?>">Kommentieren</a>
  </div>

    <?php endforeach;
    wp_reset_postdata(); /* 1. reset post data */
    ?>

</div>

<div class="box" id="recentposts">

  <?php

  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

  $display_count = 12;
  $excluded_count = 1;
  $offset = ( ($paged-1) * $display_count ) + $excluded_count;

  $args = array(
      'post_status'=>"publish",
      'post_type'=>"post",
      'orderby'=>"post_date",
      'posts_per_page' => $display_count,
      'offset' => $offset,
      'paged' => $paged
  );

  $the_query = new WP_Query( $args );
  if ( $the_query->have_posts() ) : ?>



  <h3>Bisherige Artikel</h3>

  <div class="recentpostsmasonry">

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div <?php post_class('halb'); ?> id="post-<?php the_ID(); ?>">
      <span class="head"><?php the_time('j. F Y') ?></span>
      <h3><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
      <div class="entry">
        <?php the_excerpt(); ?>
        <p class="more-link"><a href="<?php the_permalink(); ?>">Weiterlesen ...</a></p>
        <p class="comments"><?php edit_post_link(__('Bearbeiten'), '', ' | '); ?> <?php comments_popup_link(__('Schreib den ersten Kommentar'), __('Ein Kommentar'), __('% Kommentare'), '', __('') ); ?></p>
      </div>
    </div>

    <?php endwhile; ?>

  </div>

  <div class="navigation">
    <span class="nextlink" title="Ältere Einträge"><?php next_posts_link( '«', $the_query->max_num_pages ); ?></span>
    <span class="previouslink" title="Jüngere Einträge"><?php previous_posts_link( '»' ); ?></span>
  </div>

  <?php else : ?>

  <h3><?php _e('Nichts gefunden :('); ?></h3>

  <?php endif; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

【问题讨论】:

    标签: wordpress loops foreach while-loop pagination


    【解决方案1】:

    您尚未设置分页,因此您的查询始终认为它在第 1 页。

    另外根据 WP Developer Resources ,您不应该使用 query_posts - 使用 WP_Querypre_get_posts 代替,所以我使用下面的 WP_Query 来进行第二次查询。

    请注意,我假设您在静态主页上使用它,但我已在 cmets 中添加了自定义页面所需的更改。

    <?php
      $args = array( 'numberposts' => 1, 
        'post_status'=>"publish",
        'post_type'=>"post",
        'orderby'=>"post_date");
      $postslist = get_posts( $args );
      foreach ($postslist as $post) :  setup_postdata($post); 
    ?>
    
      // regular post html with wp tags
    
    <?php endforeach; 
    wp_reset_postdata(); /* 1. reset post data */
    ?>
    
    <?php
    /* Set up your pagination - $paged will contain the current page, telling WP_Query which post to start with (e.g. #13 on page 2) */
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    
    /*  NOTE: if this is NOT on a static page
    get the 'paged' query var instead of 'page', i.e.:
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    */
    
    $display_count = 12; /* variable for the number of posts per page */
    $excluded_count = 1; /* the number of posts excluded from the start position */
    
    /* Calculate the offset, i.e. the post number to start the page with
       Normally this is calculated by: ($paged-1) * $display_count 
       so we just need to add 1 for the one we excluded */
    $offset = ( ($paged-1) * $display_count ) + $excluded_count;
    
    /* set up your new query passing in the page so WP_Query knows what to return */
    $args = array(
        'post_status'=>"publish",
        'post_type'=>"post",
        'orderby'=>"post_date",
        'posts_per_page' => $display_count,
        'offset' => $offset,
        'paged' => $paged
    );
    
    /* set up your new query */
    $the_query = new WP_Query( $args );
    <?php if ( $the_query->have_posts() ) : ?>
    
        <h3>Headline</h3>
    
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>    
    
            // regular html with wp tags for these posts (teasers, with the_excerpt)
    
        <?php endwhile; ?>
    
        <div class="navigation">
          <span class="nextlink"><?php next_posts_link( 'Older', $the_query->max_num_pages ); ?></span>
          <span class="previouslink"><?php previous_posts_link( 'Newer' ); ?></span>
        </div>
    
        <?php else : ?>
    
        <h3><?php _e('Nothing found'); ?></h3>
    
    <?php endif; ?>
    

    以上代码未经测试,但基本逻辑应该是对的。

    更新:

    next_posts_linkprevious_posts_link 默认使用全局变量$wp_query,所以要使用自定义查询,将$custom_query-&gt;max_num_pages 传递给next_posts_link,例如:

    <span class="nextlink"><?php next_posts_link( 'Older', $the_query->max_num_pages ); ?></span>
    <span class="previouslink"><?php previous_posts_link( 'Newer' ); ?></span>
    

    【讨论】:

    • @ThomasMaier 空白屏幕意味着代码中存在语法错误。正如我所说,我的代码没有经过测试,因此可能存在一些基本的语法错误。如果你找不到他们,请告诉我,我会看看 - 我现在暂时下线了 :)
    • @ThomasMaier 在您添加到问题的新代码中,最后缺少get_footer();。除此之外,当我尝试使用它时,它对我来说非常有效。因为它正在工作,我能够看到我在偏移量上犯了一个错误,所以我更新了我的答案。我还在您的问题中更新了您的新代码,因此您只需将其复制并粘贴到您的 index.php 中即可。
    • @ThomasMaier 我看到你找到了导致空白屏幕的原因,现在它只是导航没有显示。您只需将next_posts_link 更改为以下内容:next_posts_link( 'Older', $the_query-&gt;max_num_pages );。我更新了也更新了我的答案。让我知道这是否适合你!
    • @ThomasMaier 听起来你使用了错误的分页变量。正如我在回答和代码中的 cmets 中所指出的,您仅将 page 用于静态主页,将 paged 用于其他所有页面。尝试将$paged = ( get_query_var('page') ) ? get_query_var('page') : 1; 更改为非静态页面的版本,即$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    • @ThomasMaier 是的,感谢您的接受。正如您在What should I do when someone answers my question? 中看到的那样,它的标准做法是对所有 有用的答案进行投票,并接受最有帮助的答案。这让其他人知道哪些答案可能对他们有用;这也是网站感谢帮助您的用户并用代表点奖励他们的方式。这里的每个人都和你一样都是志愿者,他们会在空闲时间帮助他人——表达感谢很高兴,也可能鼓励其他人回答你未来的任何问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多