如何添加在Wordpress中按视图显示帖子的自定义页面

时间:2015-08-27 20:43:07

标签: wordpress

我正在创建自定义WordPress主题。我想创建一个按视图显示所有帖子的页面(我正在使用WP-PostViews插件),这是我到目前为止所提出的:

<?php
//get template header
get_header(); ?>

<div id="main-content" class="clearfix">
    <?php
        //get template sidebar
        get_sidebar(); ?>
    <div id='post-home' class="clearfix">
        <h2 class='block-title'><span>Most Viewed Movies:</span></h2>
        <div id="block" class="clearfix">
           <?php
           $args = array(
                        'paged' => $paged,
                        'posts_per_page' => 1,
                        'meta_key' => 'views',
                        'orderby' => 'meta_value_num',
                        'order' => 'DESC'
                    );
           $query = new WP_Query($args);
           if ($query->have_posts()) { 
                while ($query->have_posts()) {
                    $query->the_post( ); ?>
                        //
                        // Post Content here
                        //
           <?php }
           } 
           ?>
        </div><!-- /End Did You Know -->
    </div><!-- /archive-entries-wrap -->

</div><!-- /main-content-->
<?php paginate_pages($query); ?>// adds the navigation pagination
<?php wp_reset_postdata(); ?>
<?php
//get template footer
get_footer(); ?>

此代码适用于导航分页部分;导航栏没有出现在页面上(如果在index.php页面中使用,相同的代码工作正常)。

这是分页代码:

<?php

if( ! function_exists('paginate_pages')) {
    function paginate_pages($query) {
        $pages ='';
        $range = 4;
        $showitems = ($range * 2)+1; 
        global $paged;
        if(empty($paged)) $paged = 1;

        if($pages == '') {
            global $wp_query;
            $pages = $wp_query->max_num_pages;
            if(!$pages) {
            $pages = 1;
            }
        }  

        if(1 != $pages) {
            echo "<div class=\"page_paginate_pages\"><div class=\"page_paginate_pages-inner clearfix\">";
            echo "<div class=\"page-of-page\"><span class=\"inner\">".$paged." of ".$pages."</span></div>"; 
            if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
            if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
            for ($i=1; $i <= $pages; $i++) {
                if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                {
                    echo ($paged == $i)? "<span class=\"current outer\"><span class=\"inner\">".$i."</span></span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\"><span class=\"inner\">".$i."</span></a>";
                }
            }
            if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">&rsaquo;</a>";
            if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
        echo "</div></div>\n";
    }
}

}

我不明白为什么导航没有出现。所以如果你有任何想法,我会感激不尽。

1 个答案:

答案 0 :(得分:1)

非常肯定这些行:

        global $wp_query;
        $pages = $wp_query->max_num_pages;

应该引用传递给函数的$query变量:

        $pages = $query->max_num_pages;