每页显示相同帖子的帖子分页

时间:2012-02-06 作者:Howdy_McGee

所以我有一个博客,按类别过滤,然后按城市过滤。在创建过滤的过程中,我打破了之前似乎工作正常的分页。它在我的主页上不起作用,它应该显示所有帖子,并转到我档案中的404。我正在使用的自定义链接结构/%category%/%postname%/ 在我的索引页中,我有:

<p align="center" id="space">
<?php posts_nav_link(\' - \',__(\'&#171; Newer Posts\',\'ml\'),__(\'Older Posts &#187;\',\'ml\')) ?>
</p>
在我的档案页面上也是一样。我创建了一个过滤所有内容的函数,这就是分页应该发生的地方:

From.phphttp://pastebin.com/d9jikfPq

    $testParent = $wp_query->get_queried_object();

function cust_query($city, $cat)// Filter and page everything by city/cat
{
    $setCity = false;
    $setCat = false;

    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 3;               // Seperates Posts into Pages
    $citySlug = get_category_by_slug($city);                                    // Used to get City && Category CatID
    $categorySlug = get_category_by_slug($cat);                                 // ^^^

    if(isset($cat) && $cat != null)                                             // Check if $cat is set
    {
        $category = $categorySlug->cat_ID;                                      // Setting Category CatID
        $setCat = true;
    }
    if(isset($city) && $city != null)                                           // Check if $city is set
    {
        $city = $citySlug->cat_ID;                                              // Setting City CatID   
        $setCity = true;
    }

    if($setCity && $setCat && !$testParent->parent)                             // If both cat and city are set filter by both
    {
        /* 
           http://wordpress.stackexchange.com/questions/40815/custom-queries-child-categories-not-showing-up
           For more information.
        */
        if(!$testParent->parent)
        {
            $args = array(
                \'tax_query\' => array(
                    \'relation\' => \'AND\',
                    array(
                        \'taxonomy\' => \'category\',
                        \'field\'    => \'id\',
                        \'terms\'    => $city,
                    ),
                    array(
                        \'taxonomy\' => \'category\', 
                        \'field\'    => \'id\',
                        \'terms\'    => $category,
                    )
                )
            );
            $args = array_merge( $args, array( \'paged\' => $paged ) );
            return query_posts($args);                                          // Return Query
        }
        return query_posts(array(category__and => array($category, $city), \'paged\' => $paged)); // Return Query
    }
    if($setCity)                                                                // Filter by City Only
        return query_posts(\'cat=\'.$city.\'&paged=\'.$paged);                      // Return Query
    else if($setCat)                                                            // Filter by Category Only
        return query_posts(\'cat=\'.$category.\'&paged=\'.$paged);                  // Return Query
    else                                                                        
        return query_posts(\'paged=\'.$paged);                                    // Return all posts
}
Header.phphttp://pastebin.com/vzm08E40

<?php require_once(\'from.php\'); ?>
<?php
    $path = $_SERVER[\'REQUEST_URI\'];                        // Get URL and Set it to $path
    $dir = explode(DIRECTORY_SEPARATOR, $path);             // Seperate URL by Directory \'/\'

    if(isset($_SESSION[\'city\']))                            // City Reset, Remove if statement if you don\'t want to reset it.               
        unset($_SESSION[\'city\']);                           // add && is_home() to reset on banner click. Useful

    if($dir[4] == \'city\')
        $_SESSION[\'city\'] = $dir[5];                        // If sub-Directory is City, get Final Directory as City
    else
        $_SESSION[\'cat\'] = end($dir);                       // Get Category

    $navCity = catName($_SESSION[\'city\']);                  // Get formatted City Name for Navigation
    $navSlug = get_category_by_slug($_SESSION[\'city\']);     // Get Category by Slug
    $cityNavID = $navSlug->cat_ID;                          // Get Category ID by Category
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="<?php bloginfo(\'html_type\'); ?>; charset=<?php bloginfo(\'charset\'); ?>" />
    <title><?php wp_title(\'&laquo;\', true, \'right\'); ?> <?php bloginfo(\'name\'); ?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" href="<?php bloginfo(\'stylesheet_url\'); ?>" type="text/css" media="screen" />
    <link rel="stylesheet" type="text/css" media="print" href="<?php bloginfo( \'stylesheet_directory\' ); ?>/print.css" />
    <link rel="alternate" type="application/rss+xml" title="<?php bloginfo(\'name\'); ?> <?php _e(\'RSS Feed\',\'ml\');?>" href="<?php bloginfo(\'rss2_url\'); ?>" />
    <?php if ( is_singular() ) wp_enqueue_script( \'comment-reply\' ); ?>
    <link rel="pingback" href="<?php bloginfo( \'pingback_url\' ); ?>" />
    <?php wp_head(); ?>
</head>
<body id="section-index"  <?php body_class(); ?>>
<div id="container">
<?php 
    //echo $_SESSION[\'cat\'] . " " . $_SESSION[\'city\'];
?>
<div id="header">
<a href="<?php bloginfo(\'siteurl\');?>" title="<?php bloginfo(\'name\');?>">
    <img src="<?php echo get_template_directory_uri() . "/img/logo.jpg" ?>" title="" alt="" />
</a>
</div><!-- end id:header -->
    <!-- This Appears Above The Sidebar-->
    <div id="headerSocial">
        <a href="AboutUs.php"> About Us</a> |
        <a href="contact_us.php">Contact Us</a> |
        <a href="member.php">Get Listed</a><br>
        <!-- AddThis Button BEGIN -->
        <div class="addthis_toolbox addthis_default_style ">
            <a class="addthis_button_preferred_1"></a>
            <a class="addthis_button_preferred_2"></a>
            <a class="addthis_button_preferred_3"></a>
            <a class="addthis_button_preferred_4"></a>
            <a class="addthis_button_compact"></a>
            <font color="004682"> &nbsp; Share This!</font>
        </div>
        <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4e5515692839a404">
        </script><!-- AddThis Button END -->
    </div>
    <!-- End Header Social -->
<div id="navContainer">
    <div id="nav">
        <ul>
            <li id="cityItem">
                Filter By: <a href="<?php get_category_link($cityNavID); ?>"><?php isset($_SESSION[\'city\']) ? print($navCity) : print("Home") ?></a>
                <ul id="cityChildren">
                    <?php if(isset($_SESSION[\'city\'])) : ?>
                        <li class="cat-item">
                            <?php $cat = get_category_by_slug($_SESSION[\'cat\']); ?>
                            <a href="<?php echo esc_url(get_category_link($cat->cat_ID)) ?>" title="">All</a>
                        </li>
                    <?php endif ?>
                    <?php wp_list_categories(\'orderby=id&child_of=1&title_li=\'); ?>
                </ul>
            </li>
            <?php wp_list_categories("orderby=slug&exclude=1&hide_empty=0&title_li="); ?>
        </ul>
    </div>
    <?php if(!is_single()) : ?>
        <div id="crumbs">
            <?php include(\'breadcrumbs.php\'); ?>
        </div>
    <?php endif; ?>
</div>
上述函数通过需要过滤的城市/类别,然后根据是否同时设置了这两个城市/类别或仅设置了一个城市/类别,它将过滤并分页。除了它没有页面。。。有什么建议吗?我还想指出,我的索引文件没有循环,它只使用query\\u posts()和changing the number inside $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 3; to anything except 1 puts all my pages to 404. If I make it 1 then it shows all posts and pages index fine... 为什么会这样?我可以做些什么来防止这种情况发生?

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

这个posts_per_page 指每页的帖子数量,而不是您正在查看的页面。为此,您需要设置paged 论点看到了吗Codex on WP_Query 还有这个article by Scribu.

使用paged 参数集,它将根据页码返回相应的帖子(第一页的前x篇帖子,第二页的下x篇帖子等)。如果不是,您总是查询前x篇文章,因此每个页面都是一样的。

例如

query_posts($args, \'posts_per_page=\'.$paged);
使用

$args = array_merge( $args, array( \'paged\' => $paged ) );
query_posts($args);
而不是

query_posts(\'posts_per_page=\'.$paged);
使用

query_posts(\'paged=\'.$paged);

SO网友:Kyle

更换(\'posts_per_page=\'.$paged) 具有(\'paged=\'.$paged)

结束

相关推荐

向函数添加APPLY_FILTERS有什么不利影响吗?

我将回顾我以前的WordPress主题,使函数可过滤,并添加函数\\u exists检查。我有没有想过这会导致现有代码出现问题?// Dummy function if ( ! function_exists( \'custom_func\' )) { function custom_func() { $output = \"Something Awesome\"; return apply_filters(\'custom_func