如何分页标签列表
1 个回答
- 投票数
-
- 2011-07-05
您只需在URL末尾添加
/page/n
即可对页面进行分页,其中n
是所需的页码.不过,创建您的下一个/上一个链接将是一项手动操作.然后可以通过get_query_var('paged')
访问页码.然后对number
使用get_terms
参数要一次选择40个,请使用offset
参数,即您的页码-1 *每页数,以选择术语的当前"页面":$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; $taxonomy = 'post_tag'; $offset = ( $page-1 ) * 40; $args = array( 'number' => 40, 'offset' => $offset ); $tax_terms = get_terms( $taxonomy, $args );
对于查看所有内容,可以在URL上附加一个GET变量
?showall=true
,然后检查isset( $_GET['showall'] )
并进行更改要提取的数字.编辑
这是我制作的用于演示示例的快速模板.我在带有漂亮的永久链接的测试安装页面上对其进行了测试,该页面为"关于",因此分页链接为:
http://localhost/about/page/2/ http://localhost/about/?showall=true
如果永久链接不同,则必须编辑分页部分以反映您的设置.
<?php get_header(); // if show all is set if( isset($_GET['showall']) ): $args = array( 'hide_empty' => 0 ); else: // else show paged $page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; // number of tags to show per-page $per_page = 40; $offset = ( $page-1 ) * $per_page; $args = array( 'number' => $per_page, 'offset' => $offset, 'hide_empty' => 0 ); endif; $taxonomy = 'post_tag'; $tax_terms = get_terms( $taxonomy, $args ); echo '<ul>'; foreach ($tax_terms as $tax_term) { echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>'; } echo '</ul>'; // pagination // if showall isn't set if( !isset($_GET['showall']) ): $total_terms = wp_count_terms( 'post_tag' ); $pages = ceil($total_terms/$per_page); // if there's more than one page if( $pages > 1 ): echo '<ul>'; for ($pagecount=1; $pagecount <= $pages; $pagecount++): echo '<li><a href="'.get_permalink().'page/'.$pagecount.'/">'.$pagecount.'</a></li>'; endfor; echo '</ul>'; // link to show all echo '<a href="'.get_permalink().'?showall=true">show all</a>'; endif; else: // showall is set, show link to get back to paged mode echo '<a href="'.get_permalink().'">show paged</a>'; endif; get_footer(); ?>
you could paginate your page by simply adding
/page/n
to the end of the URL, wheren
is the desired page number. creating your next/prev links will be a manual affair though. the page number will then be accessible viaget_query_var('paged')
. then use thenumber
argument forget_terms
to select 40 at a time, use theoffset
argument, which will be your page number -1 * number per page, to select the current "page" of terms:$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; $taxonomy = 'post_tag'; $offset = ( $page-1 ) * 40; $args = array( 'number' => 40, 'offset' => $offset ); $tax_terms = get_terms( $taxonomy, $args );
as for viewing all, maybe append a GET var to the URL,
?showall=true
, then checkisset( $_GET['showall'] )
and change the number to fetch accordingly.EDIT
here's a quick template I made to show an example. I tested it on a page on a test install with pretty permalinks, the page was 'about', so the pagination links were:
http://localhost/about/page/2/ http://localhost/about/?showall=true
if your permalinks are different, you'll have to edit the pagination section to reflect your setup.
<?php get_header(); // if show all is set if( isset($_GET['showall']) ): $args = array( 'hide_empty' => 0 ); else: // else show paged $page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; // number of tags to show per-page $per_page = 40; $offset = ( $page-1 ) * $per_page; $args = array( 'number' => $per_page, 'offset' => $offset, 'hide_empty' => 0 ); endif; $taxonomy = 'post_tag'; $tax_terms = get_terms( $taxonomy, $args ); echo '<ul>'; foreach ($tax_terms as $tax_term) { echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>'; } echo '</ul>'; // pagination // if showall isn't set if( !isset($_GET['showall']) ): $total_terms = wp_count_terms( 'post_tag' ); $pages = ceil($total_terms/$per_page); // if there's more than one page if( $pages > 1 ): echo '<ul>'; for ($pagecount=1; $pagecount <= $pages; $pagecount++): echo '<li><a href="'.get_permalink().'page/'.$pagecount.'/">'.$pagecount.'</a></li>'; endfor; echo '</ul>'; // link to show all echo '<a href="'.get_permalink().'?showall=true">show all</a>'; endif; else: // showall is set, show link to get back to paged mode echo '<a href="'.get_permalink().'">show paged</a>'; endif; get_footer(); ?>
-
谢谢回复.我似乎无法使其工作.我在哪里以及如何确切地使用您的代码?thanks for the reply. I can't seem to make it work. Where and how exactly I am to use your code?
- 0
- 2011-07-06
- Tara
-
我仍在尝试使其正常运行,但没有运气. 我在哪里以及如何确切地使用您的代码?I still trying make it work but no luck. Where and how exactly I am to use your code?
- 0
- 2011-07-23
- Tara
-
@t-p-查看修改后的答案.@t-p - see the edited answer.
- 0
- 2011-07-23
- Milo
-
优秀的!可行!请帮助我:在标签列表的顶部,有一个名为" Example"的标签.我不知道它从哪里来.我没有" Example"标签,因此,当我单击它时,会显示"未找到".如何从列表中删除它?感谢您的时间和帮助.excellent! It workss! Please help me with this: on top of the tag list, there is this tag named "Example". I have no idea where its coming from. I don't have "Example" tag and as such when I click on it I get "not found". How to can I get rid of it from the list? thanks for your time and help.
- 0
- 2011-07-23
- Tara
-
代替编写自己的分页代码,尝试使用paginate_links,https://codex.wordpress.org/Function_Reference/paginate_linksInstead writing own code for pagination, try using paginate_links, https://codex.wordpress.org/Function_Reference/paginate_links
- 0
- 2019-02-07
- dipak_pusti
我有一个页面,显示我博客的所有标签的列表.我使用的代码效果很好:
问题:我该如何对这个隐秘页进行分页(比如说每页40张).该代码将完成什么工作,最好带有"查看所有标签"选项.
谢谢.