【问题标题】:Custom post type pagination issues自定义帖子类型分页问题
【发布时间】:2015-03-01 23:03:09
【问题描述】:

在 Genesis 子主题中实现自定义帖子类型(“车辆”)的分页时遇到问题,我在 wordpress 4.1.1 中使用 Genesis : 2.0.0

我尝试将以下代码放入 function.php add_filter('genesis_prev_link_text', 'gt_review_prev_link_text');

function gt_review_prev_link_text() {
        $prevlink = '« Previous Reviews';
        return $prevlink;
}
add_filter( 'genesis_next_link_text', 'gt_review_next_link_text' );
function gt_review_next_link_text() {
        $nextlink = 'Next Reviews »';
        return $nextlink;
} 

我也尝试过以下方法;

<?php genesis_posts_nav();?>

以下代码有效,但仅作为“较新”和“较旧”超链接,我正在尝试实现数字分页。

<?php previous_posts_link( '« Newer' ); ?>
<?php next_posts_link( 'Older »', $vehicle->max_num_pages ); ?>

这是我的自定义帖子类型存档模板。

<?php
/*
Template Name: Archive Vehicle(CAR)
*/
//* Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Remove Post Info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );

//* Remove Post Meta
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 );
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
 /** Code for custom loop */

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'am_custom_loop' );
function am_custom_loop(){
$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
$paged = intval( $paged );
$args = array('post_type' => 'vehicle', 'posts_per_page' => '3', 'paged'=> $paged, );
   $vehicle = new WP_query($args);
$wp_query = $vehicle;
                if ($vehicle -> have_posts() ){
?>
<div class="first arc">
<h2> View our latest used car collection from KH cars Birmingham</h2>
<p>Need a car?  Look no further, browse through our vast selection of used cars on site based in Birmingham.  </p>
</div>
<?php
                while ($vehicle -> have_posts() ){
                $vehicle -> the_post();

?>

<div class="first archivecar">
    <div class="one-fourth">  
<?php 
$images = get_field('image');
    if( $images ): 
        $image = $images[0];
?>                
            <a href="<?php the_permalink($id); ?>">
                <img src="<?php echo $images[0]['sizes']['large']; ?>" alt="<?php echo $image['alt']; ?>" />
            </a> 
<?php endif; ?>
         </div>
       <div class="two-fourths">
<br>
            <h3><a  style="text-decoration:none;" href="<?php the_permalink($id) ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<ul class="carinfo">
    <li><p> Year: <?php $terms = get_the_terms( $post->ID , 'year', 'year' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></p></li>
    <li><p> Trans: <?php $terms = get_the_terms( $post->ID , 'trans', 'trans' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></p></li>
   <li><p> Fuel: 
  <?php   // Get terms for post
 $terms = get_the_terms( $post->ID , 'fuel', 'fuel' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></li>
 <?php if( get_field('Mileage') ): ?>
    <li><p> Mileage: <?php the_field('Mileage');?></p></li>
 <?php endif; ?>
    <li><p>Color: <?php $terms = get_the_terms( $post->ID , 'colours', 'colours' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></p></li>
 <?php if( get_field('owners') ): ?>
    <li><p>Owners: <?php $terms = get_the_terms( $post->ID , 'owners', 'owners' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} }?></p></li>
 <?php endif; ?>
</ul>
</div>
<div class="one-fourth">

        <?php if ( get_field('price')):?>
    <p class="price"><?php the_field('price');?></p>
    <?php endif; ?>
<a class="btn" style="text-align:center" href="<?php the_permalink() ?>">view Details</a>
</div>
</div>
<?php
}
?>
<?php genesis_posts_nav();?>
<?php wp_reset_query();?>
<?php
}
}
genesis();

非常感谢任何帮助。在此先感谢

【问题讨论】:

  • 你能描述一下分页中哪个部分不起作用,你认为是$paged var的问题吗?我通常使用它,它总是有效if (get_query_var('paged')) { $paged = get_query_var('paged'); } elseif (get_query_var('page')) { $paged = get_query_var('page'); } else { $paged = 1; }
  • 分页根本不显示,如果我尝试使用 url 导航到第 2 页,它只会再次显示第 1 页。
  • 我还注意到您正在使用全局 $wp_query 如果您将其更改为自定义,例如$vehicle_query
  • 我没有看到 $spotlight_posts_latestnext_posts_link 中使用的任何内容
  • @sdcr 我刚刚使用了这个 max_num_pages ); ?> 并将 wp-query 更改为 $vehicle-query 并显示较旧且较少的文本。关于如何将其更改为数字的任何想法

标签: php wordpress pagination genesis


【解决方案1】:

首先我删除了 $wp_query = $vehicle;然后使用wordpress codex中的以下代码,瞧,它可以工作了。

<?php
$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' => $vehicle->max_num_pages
) );
?>

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2014-12-11
    • 2014-05-15
    • 2011-03-20
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多