下一个类别页面404 wordpress自定义帖子类型

时间:2017-08-02 19:27:59

标签: php wordpress pagination custom-post-type

我正在制作一个自己的帖子类型(自定义帖子类型),当我切换页面时,我有一个错误。例如,当我打开一个类别时,它会显示一个帖子,但是当我转到其他页面时,例如,第二个,它会显示404(未找到)错误。

页面没有错误 - www ***** / category / cat-name /
页面WITH错误 - www ***** / category / cat-name / page / 2

这是我的 taxonomy-portfolio_categories.php 文件

<div class="container page min-height">
    <div class="row works infinite-container">
        <div class="col-md-12 category-menu">
        <?php
            $taxonomy = 'portfolio_categories';
            $cat = get_the_terms( $post->ID, $taxonomy);  
            foreach ( $cat as $cat_slug){$category_slug = $cat_slug->slug;}
            $terms = get_terms($taxonomy);
                if ( $terms && !is_wp_error( $terms ) ) :
            ?>

            <script>
                $(document).ready(function(){
                    var categoryClass = '<?php echo $category_slug ?>';
                    $('.'+categoryClass).addClass('current');
                });
            </script>

                <ul>
                    <?php foreach ( $terms as $term ) { ?>
                        <li class="<?php echo $term->slug; ?>"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
                    <?php } ?>
                </ul>
            <?php endif;?>
        </div>

        <?php           
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
            $args = array(
                'post_type' => 'portfolio',
                'taxonomy'  => $taxonomy,
                'posts_per_page' => 2,
                'portfolio_categories' => $category_slug,
                'paged' => $paged
            );
            $temp = $wp_query; $wp_query= null;

            $wp_query = new WP_Query($args); 
            while ($wp_query->have_posts()) : $wp_query->the_post(); 
            $category = get_the_terms( $post->ID, 'portfolio_categories');
        ?>
            <div class="col-md-4 col-sm-6 col-xs-12 work">
                <div class="placeholder">
                    <a href="<?php the_permalink(); ?>" class="work-link"></a>
                    <div class="work-overlay">
                        <div class="overlay-content">
                            <div class="category-name"><?php foreach ( $category as $cat){echo $cat->name;}?></div>
                            <div class="work-title"><?php the_title(); ?></div>
                        </div>
                    </div>
                    <div class="work-thumb">
                        <div style="background-image:url('<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>')"></div>
                    </div>
                </div>
            </div>
        <?php 
        endwhile; 
            if(have_posts() == NULL){
                echo info("Sorry, but there is no content available at this time!");
            }
        ?>

        <?php
            if (function_exists('custom_pagination')) {
                custom_pagination($wp_query->max_num_pages,"",$paged);
            }
        ?>
    </div>
</div>

我的PHP用于分类和register_post_type

function create_portfolio_taxonomies() {
    $labels = array(
        'name'              => _x( 'Portfolio Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Categories' ),
    );

    $args = array(
        'hierarchical'      => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'cat', 'with_front' => false ),
    );

    register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args );
}
add_action( 'init', 'create_portfolio_taxonomies', 0 );

function cptui_register_my_cpts_portfolio() {

    /**
     * Post Type: Portfolio.
     */

    $labels = array(
        "name" => __( "Portfolio", "" ),
        "singular_name" => __( "portfolio", "" ),
        "menu_name" => __( "Portfolio", "" ),
        "all_items" => __( "All portfolios", "" ),
        "edit_item" => __( "Edit", "" ),
        "view_item" => __( "View", "" ),
        "view_items" => __( "View portfolio", "" ),
        "not_found" => __( "Portfolio not found", "" ),
    );

    $args = array(
        "label" => __( "Portfolio", "" ),
        "labels" => $labels,
        "description" => "portfolio page",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => 'portfolio',
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        'rewrite' => array( 'slug' => 'cat', 'with_front' => false ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail", "excerpt", "custom-fields", "multi-post-image" ),
        "taxonomies" => array( "portfolio_categories" ),
    );

    register_post_type( "portfolio", $args );
}

add_action( 'init', 'cptui_register_my_cpts_portfolio' );

分页功能

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 5;
  }

  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='pagination'>";
      echo $paginate_links;
    echo "</nav>";
  }

}

2 个答案:

答案 0 :(得分:0)

从重写选项中删除slug => 'category',它应该可以正常工作。

答案 1 :(得分:0)

这段代码非常适合我。如果您仍有问题,请尝试将永久链接设置更改为“帖子名称”。

function create_portfolio_taxonomies() {
    $labels = array(
        'name'              => _x( 'Portfolio Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Categories' ),
    );

    $args = array(
        'hierarchical'      => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'portfolio-category', 'with_front' => false ),
    );

    register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args );
    flush_rewrite_rules();
}
add_action( 'init', 'create_portfolio_taxonomies', 0 );

function cptui_register_my_cpts_portfolio() {

    /**
     * Post Type: Portfolio.
     */

    $labels = array(
        "name" => __( "Portfolio", "" ),
        "singular_name" => __( "portfolio", "" ),
        "menu_name" => __( "Portfolio", "" ),
        "all_items" => __( "All portfolios", "" ),
        "edit_item" => __( "Edit", "" ),
        "view_item" => __( "View", "" ),
        "view_items" => __( "View portfolio", "" ),
        "not_found" => __( "Portfolio not found", "" ),
    );

    $args = array(
        "label" => __( "Portfolio", "" ),
        "labels" => $labels,
        "description" => "portfolio page",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => 'portfolio',
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail", "excerpt", "custom-fields", "multi-post-image" ),
        "taxonomies" => array( "portfolio_categories" ),
    );

    register_post_type( "portfolio", $args );
    flush_rewrite_rules();
}

add_action( 'init', 'cptui_register_my_cpts_portfolio' );
  • 重写slug“category”在默认的帖子类别中使用,这就是为什么你可能会收到404错误。