个性化阅读
专注于IT技术分析

在自定义帖子WordPress上分页

<?php query_posts('showposts=5&post_type=html5-blank'); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" class="clearfix" <?php post_class(); ?>>
           //Loop Here
    <!-- /Article -->

<?php endwhile; ?>
<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

我的分页链接上出现”页面不存在”错误。结果链接是:www.mywebsite.com/blog/page/2/这是一个博客页面。我已经编辑了循环代码。

帮帮我………


#1


我的首页(index.php)列出了帖子, 遇到了类似的问题。我一直找不到页面。 https://codex.wordpress.org/Pagination中的说明使www.domain.com/page/2/为我工作。

首先从模板文件(index.php, category.php)中删除query_posts部分

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
query_posts($args);

然后在你的functions.php中添加以下内容

function my_post_queries( $query ) {
    // do not alter the query on wp-admin pages and only alter it if it's the main query
    if (!is_admin() && $query->is_main_query()){
        // alter the query for the home and category pages
        if(is_home()){
            $query->set('posts_per_page', 3);
        }
        if(is_category()){
            $query->set('posts_per_page', 3);
        }
    }
]
add_action( 'pre_get_posts', 'my_post_queries' );

注意:HTML5空白主题和下划线主题都给我404错误, 用于分页。上述解决方案使分页适用于两个主题。


#2


我宁愿使用WP_Query并使用分页的分页参数。在此处阅读有关此内容的更多信息:WP_Query#Pagination_Parameters

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
    array(
        'post_type' => 'html5-blank', 'posts_per_page' => 5, 'paged'=>$paged
    )
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
    <article id="post-<?php the_ID(); ?>" class="clearfix" <?php post_class(); ?>>
           //Loop Here
    <!-- /Article -->

<?php endwhile; endif; ?>
<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

请告诉我 :)

第二个例子:

global $post;
global $paged, $wp_query;
$args = array( 'posts_per_page' => 5, 'post_type' => 'html5-blank', 'paged' => $paged );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
    setup_postdata($post);
    // loop
    the_title(); // or what it is needed inside the loop
endforeach;
if (  $wp_query->max_num_pages > 1 ) :
    previous_posts_link('&laquo; Newer');
    next_posts_link('Older &raquo;');
endif;

#3


<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(  
            'post_type' => 'html5-blank', //Post type
            'posts_per_page' => 5, //How many post u want to display per page
            'paged' => $paged                      
            );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
    $the_query->the_post();

    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
    <img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <p><?php the_excerpt(); ?></p>

<?php } } ?>
<div class="pagination">
<?php             
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages
    ) );
?>
</div>

#4


$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination

$the_query = new WP_Query( array( 
    'post_type' => 'YOUR_POST_TYPE_NAME', 'paged' => $paged, 'posts_per_page' => 5) 
);

while ( $the_query->have_posts() ) : $the_query->the_post();
    // YOUR CODE
endwhile;


echo '<nav>';
echo  '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo  '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";


wp_reset_postdata(); // Rest Data

你可以尝试上面的代码吗?

赞(0)
未经允许不得转载:srcmini » 在自定义帖子WordPress上分页

评论 抢沙发

评论前必须登录!