在第二页中找不到author.php中的分页自定义帖子

问题描述 投票:1回答:2

我通过此代码创建了Blog类型

 add_action( 'init', 'create_posttype' );
 function create_posttype() {
       register_post_type( 'blog',
       array(
      'labels' => array(
      'name' => __( 'Blog' ),
      'singular_name' => __( 'Blog' ),
      'add_new' => __('Add New'),
      'add_new_item' => __('Add New Blog'),
      'edit_item' => __('Edit Blog'),
      'new_item' => __('New Blog'),
      'view_item' => __('View Blog'),
      'search_items' => __('Search Blog'),
      'not_found' =>  __('No blog found'),
      'not_found_in_trash' => __('No blog found in Trash')
   ),
     'public' => true,
     'has_archive' => true,
     'rewrite' => array('slug' => 'blog','with_front'=>FALSE),
     'supports' => array('title','author', 'blogg', 'editor', 'excerpt', 'thumbnail', 'comments'           )
    )
);
    $parent_term = term_exists( 'Blogg', 'blogg' ); // array is returned if taxonomy is given
    if(!$parent_term){

        wp_insert_term( 'Blogg','blogg',array('slug' => 'blogg'));
    }
  }

然后,我添加了一个分类法如下

   register_taxonomy("blogg", array("blog"),
        array("hierarchical"=> true, "label" => "Blog Category", "singular_label" => "Blog", 
        "rewrite"=> true,'query_var' => true,'taxonomies'=>array('post-tag','blogg')));

现在,在author.php中我想列出所有博客都是由作者写的一个分页。我在author.php中的代码是:

 <?php
 $author_id    = get_query_var( 'author' );
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array(
    'author' => $author_id,
    'post_type' => 'blog',
    'post_status' => 'publish',
    'posts_per_page' => 10,     
    '$paged' =>$paged
);
query_posts($args );
  if ( have_posts() ) :
   while (have_posts() ) : the_post();
           echo somethings here......
   endwhile;
  ?>
  <div class="navigation">
                  <div class="alignleft"><?php previous_posts_link('&laquo; Forrige') ?></div>
                  <div class="alignright"><?php next_posts_link('Mer &raquo;') ?></div>
                </div>
 <?php endif;?>

显示分页,但是当我用url http://myweb.no/author/myname/page/2进入第二页时没有找到。我在这里尝试了一些解决方案https://wordpress.org/tags/custom-post-type-pagination但它仍然无效。请帮我。谢谢提前!

wordpress pagination http-status-code-404 custom-post-type taxonomy
2个回答
1
投票

你有两个主要问题

  • 你永远不应该使用query_posts。这只是一个麻烦的坏功能,喜欢分页失败
  • 切勿使用自定义查询更改主查询。而是在通过pre_get_posts执行主查询之前修改查询变量

你应该阅读并完成我最近在这里做过的this post。我已经解释了所有的事情并且不在那里,所以你应该花一些时间在那上面:-)

要解决您的问题,请删除author.php中的自定义查询并恢复为主循环。你的author.php中应该只有以下内容

if(have_posts()) {
  while(have_posts()) {
     the_post();

     //your loop elements

  }
}

现在,在functions.php中,添加以下代码。这会将您的自定义帖子类型添加到您的作者页面

function author_cpt_filter($query) {
    if ( !is_admin() && $query->is_main_query() ) {
      if ($query->is_author()) {
        $query->set('post_type', array('post', 'blog'));
        $query->set('post_per_page', 10);

      }
    }
}

add_action('pre_get_posts','author_cpt_filter');

您现在可以在作者页面中看到自定义帖子类型的帖子,您也可以像在主页上一样对其进行分页


0
投票

我已经通过使用此代码解决了这个问题。

请将此代码放在functions.php文件中

function custom_author_archive( &$query ) {
    if ($query->is_author)
        $query->set( 'post_type', 'blogs' );//blogs is custom post type name
}
add_action( 'pre_get_posts', 'custom_author_archive' );
© www.soinside.com 2019 - 2024. All rights reserved.