The paging list call problem of WordPress custom article type

» WordPress » The paging list call problem of WordPress custom article type

I have introduced you how to create custom article types in Wordpress. For details, see – WORDPRESS Custom Article Type: Add Announcement Function  。 However, when I was working on a project recently, I found a paging list of custom article types( previous_posts_link next_posts_link )It cannot be called. After going back and carefully checking the bulletin page of our website, this problem also exists, and the paging list cannot be obtained (the automatic infinite paging function of our website is invalid, which has been perfectly solved by the following methods.)

WordPress自定义文章类型的分页列表调用问题-极客公园

After searching on the Internet, we found that there are many such problems, but most of them appear abroad, and it is estimated that there are few such problems found in China. Through searching for information, I finally found a solution. I would like to share it with you here. I hope that I can give you a solution to this problem.

Usually, after creating a custom article type, we create a new page in the background and call it through the page template. The paging code appears in the page template. Our general calling code is:

 <? php        $loop = new WP_Query( array( 'post_type' => 'projects' ) );         while ( $loop->have_posts() ) : $loop->the_post();  ?>      <!--  Other codes --><? php endwhile; ?>  <nav>    <?php previous_post_link('&laquo; '); ?>    <? php next_post_link(' &raquo;'); ?>  </ nav>

Through such calls, the article can be displayed completely. However, the following pages are not displayed and the syntax is correct. In fact, we need to understand that wp_query I will not introduce the usage of. I will talk about it later when I have the opportunity. Let's take a look at the solution and change the above code to the following:

 <? php     $temp = $wp_query;     $wp_query = null;     $wp_query = new WP_Query();     $ show_posts = 4;  //How many post you want on per page    $permalink = 'Post name'; // Default, Post name    $post_type = 'projects';      //Know the current URI    $req_uri =  $_SERVER['REQUEST_URI'];        // Permalink set to default    if($permalink == 'Default') {    $req_uri = explode('paged=', $req_uri);      if($_GET['paged']) {    $uri = $req_uri[0] . 'paged=';     } else {    $uri = $req_uri[0] . '&paged=';    }    //Permalink is set to Post name    } elseif ($permalink == 'Post name') {    if (strpos($req_uri,'page/') !==  false) {    $req_uri = explode('page/',$req_uri);    $ req_uri = $req_uri[0] ;    }    $ uri = $req_uri . 'page/';      }      //Query    $wp_query->query('showposts='.$ show_posts.'&post_type='. $post_type .'&paged='.$paged);     //count posts in the custom post type   $count_posts = wp_count_posts('projects');       while ($wp_query->have_posts()) : $wp_query->the_post();     ?>      <!--  Other codes --><? php endwhile;?>    <nav>    <?php previous_posts_link('« ') ?>    <? php    $count_post = $count_posts->publish / $show_posts;      if( $count_posts->publish % $show_posts == 1 ) {    $count_post++;    $count_post = intval($count_post);    };       for($i = 1; $ i <= $count_post ; $i++) { ?>    < a no numeric noise key 1012 href="<? php echo $uri . $i; ?>"><? php echo $i; ?></a>    <?php }    ?>    <?php next_posts_link(' »') ?>    </ nav>      <?php     $wp_query = null;     $wp_query = $temp;  // Reset    ?>

The above code can change the fixed link according to your needs $permalink , also $post_type Change to the name of the custom post you created.

Code source: Designphilic

Reprinted from: The paging list call problem of WordPress custom article type

--End--

在网上搜索了一番,发现这类问题存在的挺多的,但大部分问题出现在国外,国内估计比较少发现这类问题。通过查找资料,终于找到了解决方法,在这里分享给大家,希望可以给遇到此类问题的同学一个解决方案。

通常我们在创建完自定义文章类型后,就在后台新建一个页面,然后通过页面模版调用。分页的代码是出现在页面模版中,我们一般的调用代码是:

<?php        $loop = new WP_Query( array( 'post_type' => 'projects' ) );        while ( $loop->have_posts() ) : $loop->the_post();  ?>      <!-- 其他代码 -->     <?php endwhile; ?>  <nav>    <?php previous_post_link('&laquo; '); ?>    <?php next_post_link(' &raquo;'); ?>  </nav>

通过这样子的调用,文章是可以显示完整。但下面的分页却不显示,语法也没有用错。在这里其实需要了解到wp_query的用法,在这里就不多介绍了,以后有机会再谈。那么我们来看看解决方法吧,将上面代码改成如下:

<?php     $temp = $wp_query;     $wp_query = null;     $wp_query = new WP_Query();     $show_posts = 4;  //How many post you want on per page    $permalink = 'Post name'; // Default, Post name    $post_type = 'projects';      //Know the current URI    $req_uri =  $_SERVER['REQUEST_URI'];        //Permalink set to default    if($permalink == 'Default') {    $req_uri = explode('paged=', $req_uri);      if($_GET['paged']) {    $uri = $req_uri[0] . 'paged=';     } else {    $uri = $req_uri[0] . '&paged=';    }    //Permalink is set to Post name    } elseif ($permalink == 'Post name') {    if (strpos($req_uri,'page/') !== false) {    $req_uri = explode('page/',$req_uri);    $req_uri = $req_uri[0] ;    }    $uri = $req_uri . 'page/';      }      //Query    $wp_query->query('showposts='.$show_posts.'&post_type='. $post_type .'&paged='.$paged);     //count posts in the custom post type   $count_posts = wp_count_posts('projects');      while ($wp_query->have_posts()) : $wp_query->the_post();     ?>      <!-- 其他代码-->      <?php endwhile;?>    <nav>    <?php previous_posts_link('« ') ?>    <?php    $count_post = $count_posts->publish / $show_posts;      if( $count_posts->publish % $show_posts == 1 ) {    $count_post++;    $count_post = intval($count_post);    };      for($i = 1; $i <= $count_post ; $i++) { ?>    <a no numeric noise key 1012 href="<?php echo $uri . $i; ?>"><?php echo $i; ?></a>    <?php }    ?>    <?php next_posts_link(' »') ?>    </nav>      <?php     $wp_query = null;     $wp_query = $temp;  // Reset    ?>

上面的代码可以根据你的需求,自行更改固定链接$permalink,同样$post_type更改成你所创建的自定义文章的名称。

代码来源:Designphilic

转载自:WordPress自定义文章类型的分页列表调用问题

-- 完 --

Post reply

Your email address will not be disclosed. Required items have been used * tagging