Wordpress tax_query和分页问题

时间:2012-02-10 19:15:49

标签: php wordpress

我正在使用tax_query创建一个wordpress模板来查询我的自定义帖子类型帖子。它工作正常,我在页面#1中得到了正确的帖子,但是当我点击第2页时,我看到与第1页上看到的相同的帖子。我在这里失踪了什么?提前致谢。

<?php

get_header();
?>

<h1><?php
the_title();
?></h1> <hr> <br>
<div id="content" class="three_fourth <?php
echo of_get_option('blog_sidebar_pos');
?>">
        <?php

?>
  <?php
$temp     = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$page     = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args     = array(
    'tax_query' => array(
        array(
            "taxonomy" => "wpdic-category",
            "field" => "slug",
            "terms" => "featured-charities",
            "numberposts" => 5,
            "paged" => $paged
        )
    )
);


$wp_query = new WP_Query($args);
?>
  <?php

if ($wp_query->have_posts()):
    while ($wp_query->have_posts()):
        $wp_query->the_post();
?>
    <article id="post-<?php
        the_ID();
?>" <?php
        post_class();
?>>
      <header>
        <h2><a href="<?php
        the_permalink();
?>" title="<?php
        the_title();
?>" rel="bookmark"><?php
        the_title();
?></a></h2>

        <?php
        $post_meta = of_get_option('post_meta');
?>
        <?php
        if ($post_meta == 'true' || $post_meta == '')
          {
?>
          <div class="post-meta">
            <div class="fleft"></div>
            <div class="fright"></div>
          </div><!--.post-meta-->
        <?php
          }
?>      
      </header>
      <?php
        $post_image_size = of_get_option('post_image_size');
?>
      <?php
        if ($post_image_size == '' || $post_image_size == 'normal')
          {
?>
        <?php
            if (has_post_thumbnail())
              {
                echo '<a href="';
                the_permalink();
                echo '">';
                echo '<div class="featured-thumbnail"><div class="img-wrap">';
                the_post_thumbnail();
                echo '</div></div>';
                echo '</a>';
              }
?>
      <?php
          }
        else
          {
?>
        <?php
            if (has_post_thumbnail())
              {
                echo '<a href="';
                the_permalink();
                echo '">';
                echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">';
                the_post_thumbnail('post-thumbnail-xl');
                echo '</div></div></div>';
                echo '</a>';
              }
?>
      <?php
          }
?>

      <div class="post-content">

        <?php
        $post_excerpt = of_get_option('post_excerpt');
?>
        <?php
        if ($post_excerpt == 'true' || $post_excerpt == '')
          {
?>
          <div class="excerpt"><?php
            $excerpt = get_the_excerpt();
            echo my_string_limit_words($excerpt, 52);
?></div>
        <?php
          }
?>
        <div id="widget_my_contentwidget"><ul><li><a class="link" href="<?php
        the_permalink();
?>">  read more</a></li></ul></div>
<hr>
      </div>
    </article>

  <?php
    endwhile;
else:
?>
    <div class="no-results">
      <p><strong>There has been an error.</strong></p>
      <p>We apologize for any inconvenience, please <a href="<?php
    bloginfo('url');
?>/" title="<?php
    bloginfo('description');
?>">return to the home page</a> or use the search form below.</p>
      <?php
    get_search_form();
?> <!-- outputs the default Wordpress search form-->
    </div><!--noResults-->
  <?php
endif;
?>

  <?php
if (function_exists('wp_pagenavi')):
?>
        <?php
    wp_pagenavi();
?>
    <?php
else:
?>
    <?php
    if ($wp_query->max_num_pages > 1):
?>
      <nav class="oldernewer">
        <div class="older">
          <?php
        next_posts_link('&laquo; Older Entries');
?>
        </div><!--.older-->
        <div class="newer">
          <?php
        previous_posts_link('Newer Entries &raquo;');
?>
        </div><!--.newer-->
      </nav><!--.oldernewer-->
    <?php
    endif;
?>
  <?php
endif;
?>
  <!-- Page navigation -->

  <?php
$wp_query = null;
$wp_query = $temp;
?>

<div id="footer">
<div class="clearfix">
  <?php
if (!dynamic_sidebar('Footer Content')):
?>
      <?php
endif;
?></div></div>

</div><!--#content-->



<?php
get_footer();
?>

2 个答案:

答案 0 :(得分:0)

默认情况下,

wp_pagenavi()将与全局$ wp_query一起使用;如果要对某些自定义查询进行分页,则需要将其作为参数传递,例如:

wp_pagenavi( array( 'query' => $mycustomquery ) );

答案 1 :(得分:0)

之前我遇到过同样的问题。由于某些原因,我无法使WP查询工作,但我使用query_posts。这是代码

<?php

/**
 * @author MESMERiZE
 * @copyright 2012
 */
$page     = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args     = array(
    'tax_query' => array(
        array(
            "taxonomy" => "wpdic-category",
            "field" => "slug",
            "terms" => "featured-charities",
            "numberposts" => 5,
            "paged" => $paged
        )
    )
);
query_posts($args);
while(have_posts()): the_post();

endwhile;
?>

然后我在endwhile关键字之后添加了wp pagenavi函数。

if (function_exists('wp_pagenavi')) {
wp_pagenavi();}