自定义模板上未显示Pagenav

时间:2013-11-07 作者:dannyw24

我刚刚使用以下代码完成了自定义页面模板的构建。问题是pagenav不起作用,但可以在多个帖子的其他页面上工作。我想这一个出现,如果有超过5个职位在相关类别。

<?php
/*
  Template Name: specialoffers
 *
 */
?><?php global $up_options ?>
<?php get_header(); ?>
<!-- start midle -->
<div id="midle" class="container">
    <div class="block-widget" id="category_slider">
        <div class="slider_box">
                <ul id="slider">
                    <li>
                        <a href="#"><img src="http://www.coastline-leisure.co.uk/v3/wp-content/uploads/2013/11/specialoffers-header.jpg" alt="yorkshire caravan parks"/></a>
                    </li>
                </ul>
        </div>
    </div>
    <div class="clear"></div>
</div>
<!-- end midle -->
<!--start content -->
<div id="main" class="container">

    <div class="main_content">
        <div class="article left">
            <div class="content-special">

                    <?php
                    $args = array( 
                        \'post_type\' => \'property\',
                        \'posts_per_page\' => 5,
                        \'paged\' => get_query_var( \'paged\' ),
                        \'tax_query\' => array(
                            array (
                                \'taxonomy\' => \'sale_type\',
                                \'field\' => \'slug\',
                                \'terms\' => \'special offers\'       
                            )
                            )
                             );
                $query = new WP_Query ( $args ); ?>                     
                    <?php if ($query->have_posts()) : ?>
                    <?php while ($query->have_posts()) : $query->the_post(); ?>
                         <ul class="special_grind_post">
                            <div class="box-one left">

                                <?php the_post_thumbnail(\'special\'); ?>
                                       <h3 class="offer-text"><?php the_field(\'special_offer\'); ?></h3>

                            </div>
                            <div class="box-two left">
                                <div class="inside-box">
                                <h3 class="special-title"><?php the_title(); ?></h3>
                                <p><?php the_content(); ?></p>
                                <div class="full-details"><a href="<?php the_permalink(); ?>">Full Details</a></div>
                            </div>
                            </div>
                            <div class="box-three left last">
                            <?php
                            $attachment_id = get_field(\'location_image\');
                            $size = "thumbnail"; // (thumbnail, medium, large, full or custom size) 
                            $image = wp_get_attachment_image_src( $attachment_id, $size );
                            // url = $image[0];
                            // width = $image[1];
                            // height = $image[2];
                            ?>
                            <img src="<?php echo $image[0]; ?>" />
                             <h3 class="offer-text"><?php the_field(\'location\'); ?></h3>
                             </div>
                              <div class="clear"></div>
                              </ul>
                        <?php endwhile; ?>
                    <?php endif; ?>

                <div class="clear">
                    <?php wpld_pagenavi(); ?>
                </div>
                <div class="clear"></div>
            </div>
        </div>
        <div class="clear"></div>
    </div>
</div>
<!--end content -->
<?php get_footer(); ?>
wpld\\U pagenavi函数用于生成pagenavi的代码。

function wpld_pagenavi() {
    global $wp_query, $wp_rewrite;
    $pages = \'\';
    $max = $wp_query->max_num_pages;
    if (!$current = get_query_var(\'paged\'))
        $current = 1;
    $a[\'base\'] = ($wp_rewrite->using_permalinks()) ? user_trailingslashit(trailingslashit(remove_query_arg(\'s\', get_pagenum_link(1))) . \'page/%#%/\', \'paged\') : @add_query_arg(\'paged\', \'%#%\');
    if (!empty($wp_query->query_vars[\'s\']))
        $a[\'add_args\'] = array(\'s\' => get_query_var(\'s\'));
    $a[\'total\'] = $max;
    $a[\'current\'] = $current;

    $total = 1; //1 - display the text "Page N of N", 0 - not display
    $a[\'mid_size\'] = 5; //how many links to show on the left and right of the current
    $a[\'end_size\'] = 1; //how many links to show in the beginning and end
    $a[\'prev_text\'] = \'&laquo; Previous\'; //text of the "Previous page" link
    $a[\'next_text\'] = \'Next &raquo;\'; //text of the "Next page" link

    if ($max > 1)
        echo \'<div class="navigation">\';
    if ($total == 1 && $max > 1)
        $pages = \'<span class="pages">Page \' . $current . \' of \' . $max . \'</span>\' . "\\r\\n";
    echo $pages . paginate_links($a);
    if ($max > 1)
        echo \'</div>\';
}
如果有人能友好地向我解释为什么它没有出现,那就太棒了!

非常感谢

1 个回复
SO网友:Rarst

您的导航功能似乎从主查询(全局)获取数据$wp_query) 变量

然而,自定义页面上的查询是一个完全独立的查询(new WP_Query()), 它不会修改主选项(因为它不应该修改)。

最好的方法可能是修改分页函数,有选择地接受自定义查询对象作为要使用的参数,而不是主参数。

结束

相关推荐

如何通过插件将代码包含到函数.php文件中

我的函数中有很多自定义代码。php文件,我想做的是将所有这些移到一个插件中,例如myplugin函数。然后将该文件包含到主题函数中。php文件。我可以使用挂钩来完成这项工作,还是必须手动包含该文件。我之所以想将其作为插件,是因为我可以轻松禁用它,而无需编辑主题文件!