簡體   English   中英

在Wordpress上顯示帖子以進行自定義帖子類型查詢

[英]Display posts on Wordpress for custom post type query

我在顯示10多個特定帖子類型的帖子時遇到一些問題。 我目前擁有的代碼是:

<?php get_header();
$ppp = ( !is_paged() ) ? '10' : '15';
$ofs = ( get_query_var('paged') != 0 ) ? ( ( get_query_var('paged') - 1 ) * 15 ) - 5 : 0;
// WP_Query arguments
$args = array (
    'post_type'              => array( 'book' ),
    'pagination'             => true,
    'paged'                  => get_query_var('paged'),
    'posts_per_page'         => $ppp,
    'offset'                 => $ofs,
    'ignore_sticky_posts'    => false,
);

// The Query
$query = new WP_Query( $args ); ?>
    <section class="books <?php if(get_query_var('paged') == 0){ echo 'first-page'; } else { echo 'next-page'; } ?>">
      <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
      <?php $fImg = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' ); $fi = $fImg[0]; ?>
      <a href="<?php the_permalink(); ?>" class="grid-item-story" style="background-image: url('<?php echo $fi; ?>');">
        <article <?php post_class(); ?> id="book-<?php the_ID(); ?>">
          <div class="book-item-wrap">
            <div class="book-item-inner">
              <div class="book-item-content">
                <div class="book-item-content-inner">
                  <p class="published-date"><?php the_time('M j, Y'); ?></p>
                  <?php if ($query->current_post == 0 && !is_paged() ) { ?>
                    <h1><?php the_title(); ?></h1>
                  <?php } else { ?>
                    <h1><?php echo getBookTitle(get_the_title()); ?></h1>
                  <?php } ?>
                  <div><span class="button">Read</span></div>
                </div>
              </div>
            </div>
          </div>
        </article>
      </a>
      <?php endwhile; endif; ?>
      <div class="clear"></div>
      <div class="navigation">
        <div class="next-posts"><?php next_posts_link('&laquo; Older Entries') ?></div>
        <div class="prev-posts"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
      </div>
    </section>
<?php get_footer(); ?>

這是我的front-page.php文件的完整代碼。 在我的第一頁上,將有10個帖子。 在隨后的每個頁面上,將有15個。這是因為我的帖子顯示在五行中,每行顯示三個帖子,除了第一頁上第一個帖子的寬度是兩個“帖子”而三個是“高”時。 此帖子類型總共有50個帖子。

因此,應該只有四個頁面:

  1. 帖子1-10
  2. 帖子11-25
  3. 帖子26-40
  4. 帖子41-50

但是,下一個/上一個鏈接顯示在第4頁上,這意味着存在指向第5頁的鏈接,該鏈接沒有任何內容。 此外,偏移量似乎不起作用,這意味着第4頁顯示五個帖子,而不是十個。 (通過將!is_paged()更改為get_query_var('paged') != 0 )我添加了一個公式來計算偏移量,以查看是否可以解決該偏移量,即使它確實提供了正確的設置我在其他地方回聲時的位置。 我已經嘗試了法典中WP_Query頁上列出的解決方法(更正了found_posts問題),但是似乎沒有什么可以刪除第五頁。 即使我將每頁的帖子數手動設置為15,也仍然有第五頁。 我已經嘗試過WP_Queryget_posts() ,但都沒有用。 有什么我想念的嗎?

感謝Pieter的評論,我得以找到解決方法。 根據法典頁面,我可以設置要顯示的最大頁面數。 手動設置該數字只是一個簡單的問題。

在我的next_posts_link函數的<?php ?>標記中,在調用函數之前,我添加了以下兩行代碼:

// Get the total number of posts found
$pc = $query->found_posts;
// Check if $pc is less than 11.
// If so, delete 10 from $pc (for the posts on the first page), then divide it by 15 (for the posts on other pages).
// Add 1 to account for the first page.
// Otherwise, return 1, because there will only be one page.
$mnp = ( $pc < 11 ) ? 1 : ceil( ( $pc - 10 ) / 15 ) + 1; 

添加了注釋以使您受益; 它們不包含在我的文件中。 雖然我可能應該添加它們。 通過添加過多的帖子以完全適合頁面進行測試表明它可以正常工作。 它可能看起來不漂亮,但是可以用,所以我沒有抱怨。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM