自定义邮件类型的分页:404错误

时间:2013-06-25 作者:tnog

I\'ve been getting a 404 error on a CPT archive page, and am not sure why. I can go to the page set for the custom post type archive, but the next page /mycustomposttype/page/2 gets a 404 error.

I don\'t think that there\'s anything wrong with my code and I\'ve flushed my permalink rules and I think that the CPT is registered correctly. But my code is below. Any insights would be greatly appreciated since I can\'t see why the pages are not being found. I did some research here, and saw similar issues and tried to follow allow of the solutions presented. But no luck.

<!-- Row for main content area -->
<div class="full-width" role="main" >

 <section id="sketchbook-wall">


   <ul id="sketchbook-container" class="large-block-grid-4">
        <?php 

        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $args = array(
          \'post_type\' => \'tn_cstm_sketchbook\',
          \'orderby\' => \'menu_order\',
          \'order\' =>  \'ASC\',
          \'posts_per_page\' => 2,
          \'paged\'=> $paged 
          );

          $loop = new WP_Query($args);
          while($loop->have_posts()) : $loop->the_post(); ?>
          <li>
            <figure class="sketch-thumb">

            <?php 
               if(get_field(\'sketch\')) {
                $sketch = get_field(\'sketch\');
                $large_image = wp_get_attachment_image_src( $sketch, \'full\' );
                $thumbnail = wp_get_attachment_image_src( $sketch, \'large\' );
                $attachment = get_post( get_field(\'sketch\') );
                $alt = get_post_meta($attachment->ID, \'_wp_attachment_image_alt\', true);
              }
            ?>    

            <a href="<?php echo $large_image[0]; ?>" data-id="<?php the_ID(); ?>"  class="reveal" >

              <img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt; ?>" />

            </a> 
            <figcaption>

            </figcaption>

            </figure>

          </li>

        <?php endwhile; ?>

      </ul>


  <nav>
    <?php
    // Bring $wp_query into the scope of the function
    global $wp_query;

    // Backup the original property value
    $backup_page_total = $wp_query->max_num_pages;

    // Copy the custom query property to the $wp_query object
    $wp_query->max_num_pages = $loop->max_num_pages;
    ?>

    <!-- now show the paging links -->
    <div class="alignleft"><?php next_posts_link(\'Next Entries\'); ?></div>
    <div class="alignright"><?php previous_posts_link(\'Previous Entries\'); ?></div>

    <?php
    // Finally restore the $wp_query property to it\'s original value
    $wp_query->max_num_pages = $backup_page_total;
    ?>
  </nav>


 </section>

 </div>  <!-- End Content -->


<?php

    // Register Custom Post Taxonomy
     public  function create_work_taxonomies() {
            register_taxonomy(
                \'tn_cstm_sketchbook_plugin\',
                \'tn_cstm_sketchbook\',
                array(
                    \'labels\' => array(
                        \'name\' => \'Sketchbook Categories\',
                        \'add_new_item\' => \'Add New Sketchbook Category\',
                        \'new_item_name\' => "New Sketchbook Category"
                    ),
                    \'show_ui\' => true,
                    \'show_in_nav_menus\' => true,
                    \'show_admin_column\' => true, //Show custom taxonomy in admin columns
                    \'show_tagcloud\' => false,
                    \'hierarchical\' => true,
                    \'args\' => array( \'orderby\' => \'term_order\' ),
                    \'rewrite\' => array(\'slug\' => \'sketchbook-categories\', \'with_front\' => false )
                )
            );
        }


    // Register Custom Post Type
       public function create_sketchbook_work() {
            register_post_type( \'tn_cstm_sketchbook\',
                array(
                    \'labels\' => array(
                        \'name\' => \'Sketchbook\',
                        \'singular_name\' => \'Sketchbook Page\',
                        \'add_new\' => \'Add New Sketchbook Page\',
                        \'add_new_item\' => \'Add New Sketchbook Page\',
                        \'edit\' => \'Edit Sketchbook Page\',
                        \'edit_item\' => \'Edit Sketchbook Page\',
                        \'new_item\' => \'New Sketchbook Page\',
                        \'view\' => \'View Sketchbook Page\',
                        \'view_item\' => \'View Sketchbook Pages\',
                        \'search_items\' => \'Search Sketchbook Pages\',
                        \'not_found\' => \'No Sketchbook Pages found\',
                        \'not_found_in_trash\' => \'No Sketchbook Pages Found in Trash\',
                        \'parent\' => \'Parent Sketchbook Page\'
                    ),

                    \'public\' => true,
                    \'show_in_menu\' => true,
                    \'menu_position\' => 29,
                    \'taxonomies\' => array( \'\' ),
                    \'menu_icon\' => plugins_url( \'images/sketchbook-icon.png\', __FILE__ ),
                    \'publicly_queryable\' => true,
                    \'query_var\' => true,
                    \'rewrite\' => array( \'slug\' => \'sketchbook\', \'with_front\' => false ),  
                    \'has_archive\' => true,
                    \'hierarchical\' => false,
                    \'supports\' => array( \'title\', \'page-attributes\' )
                )
            );
        }

?>
2 个回复
SO网友:tnog

虽然pre_get_posts 如Milo和helgatheviking所述,对于修改特定条件(如自定义帖子类型)的存档主查询的参数,是一个更好的解决方案。尝试使用自定义查询覆盖存档的主查询会使问题过于复杂。

由于我正在构建一个类似Tumblr的自定义图库模板,该模板通过AJAX和一个模式弹出窗口在页面上通过缩略图链接特色帖子图像,因此我最终要做的是在我为创建的页面指定的自定义模板中使用上面的自定义查询。转到页面,我得到了我想要的分页行为。

SO网友:Brad Dalton

当您注册CPT时,WordPress会生成一个归档页面。如上所述,您可以从函数文件中修改每页的帖子,并使用以下代码注册CPT和分类支持:

add_action( \'pre_get_posts\', \'set_items\' );
function set_items( $query ) {

    if( $query->is_main_query() && !is_admin() && is_post_type_archive( \'tn_cstm_sketchbook\' ) ) {
        $query->set( \'posts_per_page\', \'12\' );
    }



    // Register Custom Post Taxonomy
add_action( \'init\', \'create_work_taxonomies\' );               
function create_work_taxonomies() {
                register_taxonomy(
                    \'tn_cstm_sketchbook_plugin\',
                    \'tn_cstm_sketchbook\',
                    array(
                        \'labels\' => array(
                            \'name\' => \'Sketchbook Categories\',
                            \'add_new_item\' => \'Add New Sketchbook Category\',
                            \'new_item_name\' => "New Sketchbook Category"
                        ),
                        \'show_ui\' => true,
                        \'show_in_nav_menus\' => true,
                        \'show_admin_column\' => true, //Show custom taxonomy in admin columns
                        \'show_tagcloud\' => false,
                        \'hierarchical\' => true,
                        \'args\' => array( \'orderby\' => \'term_order\' ),
                        \'rewrite\' => array(\'slug\' => \'sketchbook-categories\', \'with_front\' => false )
                    )
                );
            }


        // Register Custom Post Type
add_action( \'init\', \'create_sketchbook_work\' );        
function create_sketchbook_work() {
                register_post_type( \'tn_cstm_sketchbook\',
                    array(
                        \'labels\' => array(
                            \'name\' => \'Sketchbook\',
                            \'singular_name\' => \'Sketchbook Page\',
                            \'add_new\' => \'Add New Sketchbook Page\',
                            \'add_new_item\' => \'Add New Sketchbook Page\',
                            \'edit\' => \'Edit Sketchbook Page\',
                            \'edit_item\' => \'Edit Sketchbook Page\',
                            \'new_item\' => \'New Sketchbook Page\',
                            \'view\' => \'View Sketchbook Page\',
                            \'view_item\' => \'View Sketchbook Pages\',
                            \'search_items\' => \'Search Sketchbook Pages\',
                            \'not_found\' => \'No Sketchbook Pages found\',
                            \'not_found_in_trash\' => \'No Sketchbook Pages Found in Trash\',
                            \'parent\' => \'Parent Sketchbook Page\'
                        ),

                        \'public\' => true,
                        \'show_in_menu\' => true,
                        \'menu_position\' => 29,
                        \'taxonomies\' => array( \'\' ),
                        \'menu_icon\' => plugins_url( \'images/sketchbook-icon.png\', __FILE__ ),
                        \'publicly_queryable\' => true,
                        \'query_var\' => true,
                        \'rewrite\' => array( \'slug\' => \'sketchbook\', \'with_front\' => false ),  
                        \'has_archive\' => true,
                        \'hierarchical\' => false,
                        \'supports\' => array( \'title\', \'page-attributes\' )
                    )
                );
            }
不需要任何自定义查询或CPT归档,也不需要使用模板进行归档,因为WordPress使用Template Hierarchy

然后,您可以使用CSS以内联网格样式显示您的特色图像。

结束

相关推荐

Wordpress Pagination Problem

我想在wordpress索引中设置分页。php,但它不工作。在我的索引中。php有3个显示自定义帖子的循环部分,我想在索引中的Blog/recent post部分循环中设置分页。php。这是索引中的代码。php<?php /** * @package WordPress * @subpackage Adapt Theme */ $options = get_option( \'adapt_theme_settings\' ); ?> &l