【问题标题】:Styling pagination in twenty twelve theme二十十二主题造型分页
【发布时间】:2014-01-21 12:22:13
【问题描述】:

我使用 WordPress 二十十二主题作为父主题。我想给它添加编号的分页,所以我找到了这段代码并将它添加到我的 function.php 文件中:

<?php 

如果(!function_exists('twentytwelve_content_nav')): 功能二十二内容导航(){ 全局 $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
}

endif; ?> 这真的很好,问题是我没有添加css样式,因为它在任何ID或类中都没有变形,这限制了我按照我计划的方式设置它的能力。有什么方法可以向此代码添加类或 Id 以便我可以对其进行样式设置?

【问题讨论】:

    标签: php css wordpress pagination


    【解决方案1】:

    <?php $category = get_category( get_query_var( 'cat' ) );
    $cat_id = $category->cat_ID;
    ?>
        <?php 
         $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
        $wpquery = new WP_Query(array(
            'order' => 'DESC',
            'cat' => $cat_id,
            'posts_per_page' => 10,
            'paged'=>$page
        ));
    
        while ($wpquery->have_posts()) {
            $wpquery->the_post();
            ?>
    
                <li class="contentlist">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    
                <p><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a></p>
                </li>
    
    
    
            <?php
        }
    
         ?>
    
         <?php 
    
    
    
        global $wpquery;
        if( $wpquery->max_num_pages >1){
          $big = 999999999; // need an unlikely integer
    
          echo paginate_links( array(
         'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
         'format' => '?paged=%#%',
         'current' => max( 1, get_query_var('paged') ),
         'total' => $wpquery->max_num_pages
           ) );
       }
    
         ?>
    

    functions.php

    // Pagination for each category
    function custom_ppp( $query ) {
        if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '10' );
        }
    }
    add_action( 'pre_get_posts', 'custom_ppp' );
    
    ?>
    

    【讨论】:

      【解决方案2】:

      我检查了 Wordpress 编解码器,没有办法在 paginate_links 函数中添加类或 id。

      为什么不将 echo 语句包装在容器 div 中?

      ?><div class="paginated-links"><?php
      echo paginate_links( array(
          'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
          'total' => $wp_query->max_num_pages
      ) );
      ?></div><?php
      

      编辑:要使用它,请将以下代码替换为上面的代码:

      echo paginate_links( array(
          'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
          'total' => $wp_query->max_num_pages
      ) );
      

      【讨论】:

      • 谢谢你,我对你的代码 sn-p 该怎么办感到困惑
      • 我已经编辑了我的答案以包含代码说明以替换为我的代码。
      • 我可能听起来像个笨蛋,但还是不明白!您是否建议替换添加编号分页的代码(在我的functions.php 文件中)或缺少某些内容?
      • 好的,这是你的代码:pastebin.com/raw.php?i=zLukxaH1,在你的functions.php文件中用这个代码替换它:pastebin.com/raw.php?i=SgwShgq4
      • 谢谢你!但这只会触发默认(新旧帖子导航)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多