【问题标题】:pagination issue wordpress on static front page静态首页上的分页问题wordpress
【发布时间】:2013-05-15 13:25:56
【问题描述】:

您有一个问题,我无法在静态首页上进行分页,查看了 wordpress 代码并完成了它所说的构建但仍然没有乐趣,

非常感谢任何帮助

<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$type = 'Galleries';
$args=array(
   'category_name' => 'Gallery',
   'post_status' => 'publish',
   'posts_per_page' => 2,
   'paged' => $paged 
 );

$my_query = null;
$my_query = new WP_Query($args);
?> 
<section id="maincontent" class="twelve columns">
<ul id="gallery_menu">
<?php if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li class="gallery_titleframe">
            <div class="gallery_titleborder">
                <div class="gallery_titleimage">
                <?php $str = get_the_ID() ; ?> 
                    <a href="<?php the_permalink() ?>" title="<?php  echo get_post_meta($str, 'gallery1', true); ?>">  
                        <img src="<?php echo get_post_meta($str, 'gallery1', true); ?>"  width="256" height="186">
                     </a>
                </div>
            </div>
            <div class="gallery_titletext">
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            </div>
         </li>    
<?php endwhile;
    } ?> 
        <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link(); ?></div>
            <div class="nav-next"><?php previous_posts_link(); ?></div>
        </div><!-- #nav-below -->

 <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

【问题讨论】:

    标签: wordpress pagination


    【解决方案1】:

    next_posts_linkprevious_posts_link 依赖于全局 $wp_query。您可以在调用它们之前将您的自定义查询临时分配给全局。

    <?php
      $temp = $wp_query;
      $wp_query = null;
      $wp_query = $my_query;
    ?>
    <div id="nav-below" class="navigation">
      <div class="nav-previous"><?php next_posts_link(); ?></div>
      <div class="nav-next"><?php previous_posts_link(); ?></div>
    </div><!-- #nav-below -->
    <?php
      $wp_query = null;
      $wp_query = $temp;
      wp_reset_query();
    ?>
    

    【讨论】:

    • 非常感谢现在获取下一页链接但仍然无法正常工作,下一页链接包含相同的 2 个帖子???也尝试使用 posts_per_page 和 showposts ,无论如何都看不到查询有问题,很奇怪
    • var_dump($paged) 在第二页告诉你什么?
    • 应该是2。试试用get_query_var('paged')代替get_query_var('page')
    【解决方案2】:

    替换这个

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    

    有了这个

    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多