单代理页面中的分页无法正常工作

时间:2018-10-31 11:16:53

标签: wordpress custom-post-type

我在该页面中创建了一个代理页面,我试图在该属性区域中列出该代理的所有属性,但我一直保持引导分页不起作用,但该页面显示页面编号,但是当您单击该页面时不会进入第1页或第2页等。但是它正在重定向到同一页。这是我的代码。

get_header();
<div class="container-fluid"><div class="row bkg-white bkg-pd-top">
<div class="container">
<?php if (have_posts()) : 
while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
    <div class="col-md-9"><div class="row">
<div class="single-details">
<div class="single-sec-prop-title">Agent Details</div>
<div class="single-prop-detail">
<div class="col-md-4 col-sm-4 col-xs-4"><div class="row"><div class="agent-single-image">
<?php the_post_thumbnail('small-img-featured', array('class' => 'img-responsive')); ?>
</div></div></div>
</div>
</div></div>    
</div></div>

<div class="single-details">
<div class="single-sec-prop-title">Description</div>
<div class="single-prop-detail">   
<?php the_content(); ?>
</div>
</div>

<div class="col-xs-12"><div class="row">
<div class="single-sec-prop-title agent-single-title">Listed Properties</div>
<?php
endwhile; 
endif;

if (get_query_var('paged')) {
    $paged = get_query_var('paged');
} elseif (get_query_var('page')) { 
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
$args = array(
'post_type' => array('forsale', 'forrent'),
'numberposts' => -1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => $paged,  //very important
'meta_key' => 'select-agent',
'meta_value' => get_the_id(),
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
get_template_part( 'template-parts/property', 'agent' );  
endwhile;  ?>
<?php 
if ($custom_query->max_num_pages > 1) :
$orig_query = $wp_query;
$wp_query = $custom_query;
?><nav class="prev-next-posts">
<?php
if (function_exists('wp_bootstrap_pagination')){
wp_bootstrap_pagination();
}
?>
</nav>
<?php endif;
 wp_reset_postdata();
else:
 get_template_part( 'template-parts/no', 'post' ); 
endif;  //ends loop

?>

<?php 
get_footer(); 

这是引导程序分页代码Bootstrap pagenatio

的链接

2 个答案:

答案 0 :(得分:1)

这是您问题的答案。如果您需要在单个页面上添加分页,例如您的单个代理页面,则需要在wordpress中设置模板重定向功能,为此您需要将以下代码添加到活动主题功能中。php在此代码中,请更改代理如果您的自定义帖子不是座席,则更改为自定义帖子类型。

 add_filter('redirect_canonical','redirect_single_page');

    function redirect_single_page($redirect_url) {
    if (is_singular('agent')) $redirect_url = false; // change 'agent' to your custom post type, obviously
    return $redirect_url;
    }

答案 1 :(得分:0)

您可以使用wordpress尝试此操作。

<?php 
get_header(); ?>
<div class="container-fluid"><div class="row bkg-white bkg-pd-top">
<div class="container">
<?php if (have_posts()) : 
while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
    <div class="col-md-9"><div class="row">
<div class="single-details">
<div class="single-sec-prop-title">Agent Details</div>
<div class="single-prop-detail">
<div class="col-md-4 col-sm-4 col-xs-4"><div class="row"><div class="agent-single-image">
<?php the_post_thumbnail('small-img-featured', array('class' => 'img-responsive')); ?>
</div></div></div>
</div>
</div></div>    
</div></div>

<div class="single-details">
<div class="single-sec-prop-title">Description</div>
<div class="single-prop-detail">   
<?php the_content(); ?>
</div>
</div>

<div class="col-xs-12"><div class="row">
<div class="single-sec-prop-title agent-single-title">Listed Properties</div>
<?php
endwhile; 
endif;

if (get_query_var('paged')) {
    $paged = get_query_var('paged');
} elseif (get_query_var('page')) { 
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
$args = array(
'post_type' => array('forsale', 'forrent'),
'numberposts' => -1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => $paged,  //very important
'meta_key' => 'select-agent',
'meta_value' => get_the_id(),
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
get_template_part( 'template-parts/property', 'agent' );  
endwhile;  

customPagination($paged,$custom_query);


endif;
 wp_reset_postdata();
else:
 get_template_part( 'template-parts/no', 'post' ); 
endif;  //ends loop

?>

<?php 
get_footer();

?>


<?php 
function customPagination($paged,$probj){

            $data = '';
            $data .='<nav aria-label="Page navigation example">';
                $pager = 999999999; 
                $pargs = array(
                    'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
                    'format' => '?paged=%#%',
                    'current' => max( 1, $paged ),
                    'total' => $probj->max_num_pages,
                    'prev_next'=> false,
                    'type'=> 'array'
                );      
                $links = paginate_links( $pargs );              

                if ( $links ) :

                    $data .= '<ul class="pagination justify-content-center">';

                    // get_previous_posts_link will return a string or void if no link is set.
                    if ( $prev_posts_link = get_previous_posts_link( __( 'Previous Page' ) ) ) :
                        $data .= '<li class="prev-list-item">';
                        $data .= $prev_posts_link;
                        $data .= '</li>';
                    endif;

                    $data .= '<li class="page-item">';
                    $data .= join( '</li><li class="page-item">', $links );
                    $data .= '</li>';

                    // get_next_posts_link will return a string or void if no link is set.
                    if ( $next_posts_link = get_next_posts_link( __( 'Next Page' ) ) ) :
                        $data .= '<li class="next-list-item">';
                        $data .= $next_posts_link;
                        $data .= '</li>';
                    endif;
                    $data .= '</ul>';
                endif;



            $data .='</nav>';
            echo $data;
        }


?>

我在您的代码中添加了一个功能,请参阅此内容。