使用短代码未显示的自定义帖子类型的分页

时间:2016-10-23 11:27:07

标签: wordpress pagination custom-post-type

起初,我已经检查了类似的问题,但没有一个解决方案正在解决我的问题。

所以,我有以下代码,它在我使用短代码时生成帖子。但分页/导航没有显示。这是代码 -

add_shortcode( 'wm-offers', 'shortcodes_offers' );
function shortcodes_offers( $atts ) {
    // define attributes and their defaults
    extract( shortcode_atts( array (
        'items_per_row' => '2',
        'posts' => 10,
    ), $atts ) );

    // define query parameters based on attributes
    $options = array(
        'post_type' => 'offers',
        'posts_per_page' => $posts,
    );

    $grid_col = (12 / $items_per_row);

    $query = new WP_Query( $options );
    if ( $query->have_posts() ) { ?>
        <div class="row ">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="col-xs-12 col-sm-6 col-md-<?php echo $grid_col;?>">
                <div class="card wm-coupons-post-card">
                    <?php
                        echo '<div class="card-img">';
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail('wm-post-thumb');
                            } else {
                                echo '<img src="'.get_template_directory_uri().'/images/content-cards-placeholder.png"  />';
                            }
                            echo '<div class="post-date">';
                                echo esc_html( get_post_meta( get_the_ID(), 'offer_expire', true ) );
                            echo '</div>';

                            echo '<div class="post-categories">';
                                $categories_list = get_the_category_list( esc_html__( ', ', 'wm-coupons' ) );
                                if ( $categories_list && wm_coupons_categorized_blog() ) {
                                    printf( '' . esc_html__( '%1$s', 'wm-coupons' ) . '', $categories_list ); // WPCS: XSS OK.'<span class="fa fa-tags"></span> ' . 
                                }
                            echo '</div>';
                        echo '</div>';
                    ?>  
                    <div class="card-header">
                        <h5 class="card-title"><?php the_title(); ?></h5>
                        <h6 class="card-subtitle text-muted">
                            <?php
                                echo '<span class="fa fa-user"></span> <a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a>';

                                edit_post_link($link = '<span class="fa fa-pencil-square-o"></span> Edit', $before = '', $after = '', $id = "", $class="btn btn-sm btn-outline-primary ml-1");
                            ?>
                        </h6> 
                    </div>
                    <div class="card-block">
                        <?php
                            the_excerpt(); 
                        ?>

                        <p class="text-xs-right mb-0">
                            <?php 
                                echo get_the_tag_list(' ', ' ') . ' <span class="fa fa-tags"></span>';
                            ?>
                        </p>
                    </div>

                    <div class="card-footer text-xs-right">
                        <?php
                            if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
                                echo '<span class="comments-link pull-left"><span class="fa fa-comment-o"></span> ';

                                comments_popup_link( sprintf( wp_kses( __( 'Leave a Comment', 'wm-coupons' ), array( 'span' => array( 'class' => array() ) ) ) ) );
                                echo '</span>';
                            }

                            echo ' <a class="read-more btn btn-sm btn-info" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'your-text-domain') . '</a>';
                        ?>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <div class="nav-previous"><?php get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ) ) ?></div>
        <div class="nav-next"><?php get_previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) ?></div>
        <?php 
            the_posts_pagination();
            wp_reset_postdata(); 
        ?>
    <?php
    }
}

1 个答案:

答案 0 :(得分:0)

您的示例代码包含两个错误;

  • 应回显get_next_posts_linkget_previous_posts_link函数。所以例如echo get_next_post_link(...)
  • 您的WP_Query应使用paged参数。

有关paged参数的详细信息,请参阅WordPress Codex here。可以看到在自定义循环中使用分页的示例here

祝你好运!

相关问题