WordPress自定义帖子类型分页问题

时间:2012-12-11 13:47:15

标签: wordpress pagination

我和我的朋友都完全知道这里发生了什么事。任务是将标准分页从一个帖子链接到下一个帖子。我们可以看到页面从第2页/第3页/第3页开始,但内容不会改变。

以下是我们在自定义模板中的内容。

 <?php
 /**
 * The Template for displaying all single posts.
 *
 * Template Name: Portfolio
 *
 * @package WordPress
 * @subpackage Boilerplate
 * @since Boilerplate 1.0
 */

 get_header();

 // Enable Pagination
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

 $args = array(
 'post_type' => array(
 'portfolio'
 ),
 'orderby' => 'date',
 'posts_per_page' => 1,
 'paged'=>$paged
 );

 $the_query = new WP_Query( $args );

 while ( $the_query->have_posts() ) : $the_query->the_post(); 

 ?>

 <article id="item<?php the_ID(); ?>" <?php post_class('post portfolio'); ?>>
   <h2><?php the_title(); ?></h2>
   <div>
     <?php the_content(); ?>
   </div>
 </article>

 <?php endwhile; ?>
     <?php next_posts_link('« Older Entries') ?>
     <?php previous_posts_link('Newer Entries »') ?>
 <?php wp_reset_postdata(); ?>

 <?php get_footer(); ?>

在functions.php的底部存在一些自定义的帖子类型脚本......

// Custom Post Type

function foggin_Portfolio() {
$labels = array(
    'name'               => _x( 'Portfolio', 'post type general name' ),
    'singular_name'      => _x( 'Portfolio', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Item' ),
    'edit_item'          => __( 'Edit item' ),
    'new_item'           => __( 'New Item' ),
    'all_items'          => __( 'All Items' ),
    'view_item'          => __( 'View Item' ),
    'search_items'       => __( 'Search items' ),
    'not_found'          => __( 'No item' ),
    'not_found_in_trash' => __( 'No items found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Portfolio'
);
$args = array(
    'labels'        => $labels,
    'description'   => 'Holds portfolio items and portfolio specific data',
    'public'        => true,
    'menu_position' => 5,
    'rewrite'       => array('slug'=>'','with_front'=>false),
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'taxonomies'),
    'taxonomies'    => array('post_tag'),
    'has_archive'   => true,
);
register_post_type( 'portfolio', $args );
}

add_action( 'init', 'foggin_Portfolio' );

function portfolio_messages( $messages ) {
    global $post, $post_ID;
    $messages['portfolio'] = array(
        0 => '',
        1 => sprintf( __('Portfolio item updated. <a href="%s">View item</a>'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Portfolio item restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Portfolio item published. <a href="%s">View item</a>'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Portfolio item saved.'),
        8 => sprintf( __('Portfolio item submitted. <a target="_blank" href="%s">Preview item</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Portfolio item scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview item</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Portfolio item draft updated. <a target="_blank" href="%s">Preview item</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        );
        return $messages;
    }

    add_filter( 'post_updated_messages', 'portfolio_messages' );


    function portfolio_taxonomies() {
        $labels = array(
        'name'              => _x( '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' ),
        'menu_name'         => __( 'Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'portfolio_category', 'portfolio', $args );
}
add_action( 'init', 'portfolio_taxonomies', 0 );

?>

思考,想法,建议会非常有帮助我认为我们都对此感到困惑是公平的。

1 个答案:

答案 0 :(得分:0)

这可能听起来很愚蠢,但将固定链接重置为默认值,然后再恢复为所需的格式。将帖子类型添加到functions.php

后,需要重写.htaccess文件

重写选项设置为 false 的操作也可能导致问题。如果WP在重写yourdomain.com/page/2/时不明确,那么重写为true会使其成为yourdomain.com/portfolio/page/2/

其他所有内容似乎都与您的查询有关。