。当我点击WordPress网站中的分页时,当前类没有改变

时间:2016-08-22 08:35:29

标签: php css wordpress pagination

我找不到任何方法来解决这种类型的错误。我在我的wordpress网站上添加了一个分页。所有的东西都没问题。但是当我点击2,3,4 ...等页面时它没有改变它的类.current就是为什么它不能获得.current类的CSS。

图库的功能代码:

function jellythemes_gallery($atts, $content=null) {
    extract( shortcode_atts( array(
        'limit' => -1
        ), $atts ) );
    global $post;
    $back=$post;
    echo '<div id="portfolio">
                <div class="section portfoliocontent">
                    <section id="i-portfolio" class="clear">';

      // set up or arguments for our custom query
      $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
      $query_args = array(
        'post_type' => 'media',
        'posts_per_page' => 12,
        'paged' => $paged
      );
      // create a new instance of WP_Query
      $the_query = new WP_Query( $query_args );

    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

                $image = get_post_meta( $post->ID, '_jellythemes_media_photo', true );
                $video = get_post_meta( $post->ID, '_jellythemes_media_video', true );
                $img = wp_get_attachment_image_src($image, 'media_thumb');

                echo '<div class="ch-grid element">
                                <img class="ch-item" src="' . $img[0] .'" alt="' . get_the_title() . '" />';
                if (empty($video)) {
                    echo '<a class="fancybox img-lightbox" rel="" href="' . $img[0] .'">';
                } else {
                    echo '<a class="fancybox-media" rel="" href="' . $video.'">';
                }
                echo '<div>
                        <span class="p-category ' . (empty($video) ? 'photo' : 'video') . '"></span>
                          </div>
                            </a>
                            </div>';
     endwhile; 

             $post=$back; //restore post object
    $return .= '</section>
            </div>
        </div>';

    echo "<div class='pagination_center'>";
     if ($the_query->max_num_pages > 1) { 
                $current_page = max(1, get_query_var('paged'));

                echo paginate_links(array(
                    'base' => get_pagenum_link(1) . '%_%',
                    'format' => '/page/%#%',
                    'current' => $current_page,
                    'total' => $the_query->max_num_pages,
                    'show_all'  => true,
                    'prev_text' => __('« PREV'),
                    'next_text' => __('NEXT »'),
                    'prev_next' => false,
                ));
    }

     else{ 
      echo '<article>
        <h1>Sorry...</h1>
        <p>';echo 'Sorry, no posts matched your criteria.'; echo'</p>
      </article>';
    }
     endif; 
    echo "</div>";

    return $return;
}
add_shortcode( 'jellythemes_gallery', 'jellythemes_gallery' );

的CSS:

.pagination_center .current{
border: 1px solid #c3121c;
    padding: 0px 5px;
    color: black;
    font-weight: bold;
    background: #c3121c;
}

在这里,我添加了一个image与inspect元素。它是第2页,但.current类在第1页。任何人都可以告诉我问题出在哪里?

提前致谢。如果您需要进一步的代码,请告诉我。

2 个答案:

答案 0 :(得分: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;
}

在WP_Query循环中。

然后您需要更换

'current' => $current_page,

使用

'current'      => max( 1, $paged ),

在您的paginate_links数组中。

就我而言,它就像一种魅力。您也应该尝试一下。我相信它会100%工作。

答案 1 :(得分:0)

您需要替换下一页的基础。可能问题可能是%_%%#%,如果没有测试,它似乎就没那么有用了。但请尝试以下代码,

$big = 999999999; // need an unlikely integer
echo paginate_links(array(
    'base' => str_replace($big, '%#%', esc_url( get_pagenum_link($big) ) ),
    'format' => '/page/%#%',
    'current' => $current_page,
    'total' => $the_query->max_num_pages,
    'show_all' => true,
    'prev_text' => __('« PREV'),
    'next_text' => __('NEXT »'),
    'prev_next' => false,
));

它可能对你有所帮助。另请参阅Paginate Links

的更多内容