自定义POST类型档案页面分页

时间:2013-12-17 作者:ptf

我已为自定义帖子类型设置了存档页reference, 在这里,我使用查询来获取类型为reference. 这是可行的,但当我尝试对结果分页时,它不起作用。现在,它甚至没有打印出分页链接。

编辑:这是以前在常规页面上使用的,但我将其更改为存档页面。那就是分页中断的时候。

这里是源代码;

自定义帖子类型reference (插件)

<?php
   /*
   comments here removed 
   */
?>
<?php

/* Custom post types */
function create_references_post_type() {
    $labels = array(
        \'name\'               => __( \'References\', \'kasparabi\' ),
        \'singular_name\'      => __( \'Reference\', \'kasparabi\' ),
        \'add_new\'            => __( \'Add New\', \'kasparabi\' ),
        \'add_new_item\'       => __( \'Add new reference\', \'kasparabi\' ),
        \'edit_item\'          => __( \'Edit reference\', \'kasparabi\' ),
        \'new_item\'           => __( \'New reference\', \'kasparabi\' ),
        \'all_items\'          => __( \'All references\', \'kasparabi\' ),
        \'view_item\'          => __( \'View reference\', \'kasparabi\' ),
        \'search_items\'       => __( \'Search references\', \'kasparabi\' ),
        \'not_found\'          => __( \'No references found\', \'kasparabi\' ),
        \'not_found_in_trash\' => __( \'No references found in the Trash\', \'kasparabi\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => __(\'References\', \'kasparabi\')
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => __(\'All the references\', \'kasparabi\'),
        \'public\'        => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\'),
        \'has_archive\'   => __(\'references\', \'kasparabi\')/*,
        \'rewrite\'       => array(\'slug\' => __(\'references\', \'kasparabi\'))*/
    );
    register_post_type( \'reference\', $args );   
}
add_action(\'init\', \'create_references_post_type\');

function add_post_type_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( \'post_type\', array( \'reference\' ) );
    return $query;
}
add_action( \'pre_get_posts\', \'add_post_type_to_query\' );

?>
我查询帖子的方式:

$wp_query = new WP_Query(array(
                        \'post_type\' => \'reference\', 
                        \'posts_per_page\' => 9,
                        \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1 
                    ));
如何打印分页链接:

<div class="col-xs-12">
    <?php previous_posts_link(__(\'&larr; newer\', \'kasparabi\')); ?>

    <?php if ($loop->max_num_pages > $paged) :
        next_posts_link(__(\'older &rarr;\', \'kasparabi\'), $posts_per_page);
    endif; ?>
</div>
作为一个额外的问题;除了每次都创建一个新的查询之外,还有其他方法获得帖子吗?

3 个回复
SO网友:luke

难道不是这样吗?

\'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1
而next\\u posts\\u link/previous\\u posts\\u links有以下参数:

 next_posts_link( $label , $max_pages );
$label不是代码中的字符串,而是一种数组?

我对这些“神奇的PHP函数”不是很熟悉,但您认为这些“\\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?据我所知,人们不应该使用这些。

SO网友:Robert Bokori

您使用的名称注册您的帖子类型reference 在这种情况下,您可以使用内置template hierarchy 以获得正确的循环。在主题文件夹的根目录中创建一个名为archive-reference.php.

在这个文件中,您不必查询引用,因为WordPress会自动查询引用。您必须在该文件中写入的唯一内容是一个简单的逻辑:

<?php if ( ! have_posts() ) : ?>
  <div class="alert alert-warning">
    <?php _e( \'Sorry, no results were found.\', \'kasparabi\' ); ?>
  </div>
<?php endif; ?>

<?php while ( have_posts() ) : the_post(); ?>
  <article <?php post_class(); ?>>
    <h3><?php the_title(); ?></h3>

    <div class="entry-summary"><?php the_excerpt(); ?></div>

    <a href="<?php the_permalink(); ?>"><?php _e(\'Read More\', \'kasparabi\'); ?></a>
  </article>
<?php endwhile; ?>

<?php if ($wp_query->max_num_pages > 1) : ?>
  <nav class="post-nav">
    <ul class="pager">
      <li class="previous"><?php next_posts_link(__(\'Older\', \'kasparabi\')); ?></li>
      <li class="next"><?php previous_posts_link(__(\'Newer\', \'kasparabi\')); ?></li>
    </ul>
  </nav>
<?php endif; ?>
这将列出您的参考资料及其标题和摘录。

不要忘记允许在post类型声明中重写:

  $rewrite = array(
    \'slug\'                  => \'references\',
    \'with_front\'            => false,
    \'pages\'                 => true,
    \'feeds\'                 => true,
  );

  ...

  \'rewrite\' => $rewrite,
如果要在页面上查询引用,则必须始终构建自定义查询。这个答案可以帮助您做到这一点:https://stackoverflow.com/a/20905538/2043492

希望这有帮助!

SO网友:Ravi Patel
<?php 

/*this one your query*/
global $wp_query;
$wp_query = new WP_Query(array(
    \'post_type\' => \'reference\', 
    \'posts_per_page\' => 9,
    \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1 
));

/*pagination code here with update max pages*/            

$GLOBALS[\'wp_query\']->max_num_pages = $wp_query->max_num_pages;
the_posts_pagination(array(
    \'mid_size\' => 1,
    \'prev_text\' => __(\'Previous page\', \'patelextensions\'),
    \'next_text\' => __(\'Next page\', \'patelextensions\'),
    \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __(\'Page\', \'patelextensions\') . \' </span>\',
));
结束

相关推荐

Pagination for event query

我正在使用Modern Tribe Events Calendar我的问题是分页。这是对WP\\U查询分页的错误方式,还是有其他方法可以对事件日历分页?我找到了一个tutorial 这使用了WPNavi插件,但我想使用核心wordpress函数。<?php $upcoming = new WP_Query(); $upcoming->query( array( \'post_type\'=> \'tribe_events\', \'eve

自定义POST类型档案页面分页 - 小码农CODE - 行之有效找到问题解决它

自定义POST类型档案页面分页

时间:2013-12-17 作者:ptf

我已为自定义帖子类型设置了存档页reference, 在这里,我使用查询来获取类型为reference. 这是可行的,但当我尝试对结果分页时,它不起作用。现在,它甚至没有打印出分页链接。

编辑:这是以前在常规页面上使用的,但我将其更改为存档页面。那就是分页中断的时候。

这里是源代码;

自定义帖子类型reference (插件)

<?php
   /*
   comments here removed 
   */
?>
<?php

/* Custom post types */
function create_references_post_type() {
    $labels = array(
        \'name\'               => __( \'References\', \'kasparabi\' ),
        \'singular_name\'      => __( \'Reference\', \'kasparabi\' ),
        \'add_new\'            => __( \'Add New\', \'kasparabi\' ),
        \'add_new_item\'       => __( \'Add new reference\', \'kasparabi\' ),
        \'edit_item\'          => __( \'Edit reference\', \'kasparabi\' ),
        \'new_item\'           => __( \'New reference\', \'kasparabi\' ),
        \'all_items\'          => __( \'All references\', \'kasparabi\' ),
        \'view_item\'          => __( \'View reference\', \'kasparabi\' ),
        \'search_items\'       => __( \'Search references\', \'kasparabi\' ),
        \'not_found\'          => __( \'No references found\', \'kasparabi\' ),
        \'not_found_in_trash\' => __( \'No references found in the Trash\', \'kasparabi\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => __(\'References\', \'kasparabi\')
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => __(\'All the references\', \'kasparabi\'),
        \'public\'        => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\'),
        \'has_archive\'   => __(\'references\', \'kasparabi\')/*,
        \'rewrite\'       => array(\'slug\' => __(\'references\', \'kasparabi\'))*/
    );
    register_post_type( \'reference\', $args );   
}
add_action(\'init\', \'create_references_post_type\');

function add_post_type_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( \'post_type\', array( \'reference\' ) );
    return $query;
}
add_action( \'pre_get_posts\', \'add_post_type_to_query\' );

?>
我查询帖子的方式:

$wp_query = new WP_Query(array(
                        \'post_type\' => \'reference\', 
                        \'posts_per_page\' => 9,
                        \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1 
                    ));
如何打印分页链接:

<div class="col-xs-12">
    <?php previous_posts_link(__(\'&larr; newer\', \'kasparabi\')); ?>

    <?php if ($loop->max_num_pages > $paged) :
        next_posts_link(__(\'older &rarr;\', \'kasparabi\'), $posts_per_page);
    endif; ?>
</div>
作为一个额外的问题;除了每次都创建一个新的查询之外,还有其他方法获得帖子吗?

3 个回复
SO网友:luke

难道不是这样吗?

\'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1
而next\\u posts\\u link/previous\\u posts\\u links有以下参数:

 next_posts_link( $label , $max_pages );
$label不是代码中的字符串,而是一种数组?

我对这些“神奇的PHP函数”不是很熟悉,但您认为这些“\\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?据我所知,人们不应该使用这些。

SO网友:Robert Bokori

您使用的名称注册您的帖子类型reference 在这种情况下,您可以使用内置template hierarchy 以获得正确的循环。在主题文件夹的根目录中创建一个名为archive-reference.php.

在这个文件中,您不必查询引用,因为WordPress会自动查询引用。您必须在该文件中写入的唯一内容是一个简单的逻辑:

<?php if ( ! have_posts() ) : ?>
  <div class="alert alert-warning">
    <?php _e( \'Sorry, no results were found.\', \'kasparabi\' ); ?>
  </div>
<?php endif; ?>

<?php while ( have_posts() ) : the_post(); ?>
  <article <?php post_class(); ?>>
    <h3><?php the_title(); ?></h3>

    <div class="entry-summary"><?php the_excerpt(); ?></div>

    <a href="<?php the_permalink(); ?>"><?php _e(\'Read More\', \'kasparabi\'); ?></a>
  </article>
<?php endwhile; ?>

<?php if ($wp_query->max_num_pages > 1) : ?>
  <nav class="post-nav">
    <ul class="pager">
      <li class="previous"><?php next_posts_link(__(\'Older\', \'kasparabi\')); ?></li>
      <li class="next"><?php previous_posts_link(__(\'Newer\', \'kasparabi\')); ?></li>
    </ul>
  </nav>
<?php endif; ?>
这将列出您的参考资料及其标题和摘录。

不要忘记允许在post类型声明中重写:

  $rewrite = array(
    \'slug\'                  => \'references\',
    \'with_front\'            => false,
    \'pages\'                 => true,
    \'feeds\'                 => true,
  );

  ...

  \'rewrite\' => $rewrite,
如果要在页面上查询引用,则必须始终构建自定义查询。这个答案可以帮助您做到这一点:https://stackoverflow.com/a/20905538/2043492

希望这有帮助!

SO网友:Ravi Patel
<?php 

/*this one your query*/
global $wp_query;
$wp_query = new WP_Query(array(
    \'post_type\' => \'reference\', 
    \'posts_per_page\' => 9,
    \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1 
));

/*pagination code here with update max pages*/            

$GLOBALS[\'wp_query\']->max_num_pages = $wp_query->max_num_pages;
the_posts_pagination(array(
    \'mid_size\' => 1,
    \'prev_text\' => __(\'Previous page\', \'patelextensions\'),
    \'next_text\' => __(\'Next page\', \'patelextensions\'),
    \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __(\'Page\', \'patelextensions\') . \' </span>\',
));

相关推荐

Count posts for pagination

我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗