wordpress模板标签调用之文章post
本文最后更新于 745 天前,其中的信息可能已经有所发展或是发生改变。
wordpress的函数有the_开头的,函数功能是直接输出,而get_开头的则是返回一个值

the_ID()

the_ID();

输出当前页面的id,不论是页面、分类、文章。虽然小写也可以,但还是跟官方文档保持一致吧。同时,也可以使用这个方法来输出

echo get_the_id();

the_title()

the_title( string $before = ''string $after = ''bool $echo = true )

输出当前页面的标题,可以在标题前后自定义字符,如h标签。最后一个参数默认true,直接输出而非返回值

the_title('<h2 class="action">','</h2>');

the_title_attribute()

the_title_attribute(string|array $args = '');

输出当前页面的标题,用法跟上面的the_title()一样。特殊情况是标题中包含html标签时,如span、div等,应该使用这个函数,而不是the_title()

the_title_attribute('<div class="action">','</div>');

single_post_title()

single_post_title(string $prefix = ''bool $display = true);

输出当前文章的标题,此函数一般放在single.php模板文件中。$prefix参数为标题前自定义字符,$display决定是输出还是返回值。默认true

the_content()

the_content(string $more_link_text = nullbool $strip_teaser = false);

输出文章内容,两个参数感觉用处不大,不做叙述了

the_excerpt()

the_excerpt();

输出文章的摘要内容,此内容在编辑文章是要单独设置。控制摘要显示文字字数代码

<?php $title = get_the_excerpt();$excerpts = wp_trim_words( $title, 60);echo $excerpts;?>

wp_link_pages()

wp_link_pages(string|array $args = '');

感觉没啥用的文章分页函数,一般来说都是列表进行分页,文章分页的比较少见。不做叙述了

posts_nav_link()

posts_nav_link(string $sep = ''string $prelabel = ''string $nxtlabel = '');

输出上一篇,下一篇文章的导航链接。参数:分割符号、上一页的字符描述、下一页的字符描述。比较少用

next_post_link()

next_post_link(
  string $format = '%link &raquo;',       //%link表示链接,前后可写html。默认是&laquo; %link
  string $link = '%title',                //链接的锚文本,前后可写html。默认是%title
  bool $in_same_term = false,             //是否在同一分类下,默认是false,表示新闻文章的下一页可以是产品
  int[]|string $excluded_terms = '',      //排除那些分类,默认为空。多个值以逗号分隔
  string $taxonomy = 'category'           //分类法,默认category
);

显示当前文章相邻的下一篇文章,很实用,需要在查询中使用。具体分页方法详见上一页下一页

next_post_link('<strong>%link</strong>','<i class="fas fa-arrow-right"></i> %title',);
previous_post_link( '%link', '%title', true, '13' );  //在当前分类的上一篇文章,排除分类id为13的文章

previous_post_link()

同上

显示当前文章相邻的上一篇文章,很实用,需要在查询中使用。具体分页方法详见上一页下一页

next_posts_link()

next_posts_link(string $label = nullint $max_page);

显示当前页面相邻的下一篇文章,可自定义显示文本,默认为空;最大页数,默认为0。比较少用

previous_posts_link()

previous_posts_link(string $label = null);

显示当前页面相邻的上一篇文章,可自定义显示文本,默认为空;最大页数,默认为0。比较少用

next_image_link()

next_image_link(string|int[] $size = 'thumbnail'string|false $text = false);

显示同一篇文章的下一章图片的链接,可设置图片尺寸以及链接文本。比较少用

previous_image_link()

previous_image_link(string|int[] $size = 'thumbnail'string|false $text = false);

显示同一篇文章的上一章图片的链接,可设置图片尺寸以及链接文本。比较少用

post_class()

post_class(string|string[] $class = ''int|WP_Post $post_id = null);

给指定文章id的文章显示指定的类名或多个类名。类名以空格分隔;多个文章id用逗号分隔,默认当前文章id

the_category()

the_category(string $separator = ''string $parents = ''int $post_id = false);

当文章有多个分类时,以链接形式显示指定的文章ID的分类链接。可指定分类间隔符号;父分类参数默认为空不显示,可选值multiple、single;多个文章id用逗号分隔,默认当前文章id。由于其方向更正常的面包屑方向相反,比较少用

the_tags()

the_tags(string $before = nullstring $sep = ', 'string $after = '');

显示出当前文章的所有tag标签。前后可写html,中间参数表示标签间隔符

the_meta()

the_meta();

显示出文章自定义字段列表,以ul包裹

文章原文:https://www.luojiasan.com/category/wordpress/825.html
文章来源:https://www.luojiasan.com
[声明]本站内容除特别声明外,均属站长手写原创内容。
1、本站所有原创文章、图片等资料,如需转载使用,必须附上来源出处及链接方式;
2、若本站侵犯到您的权益或任何版权问题,请立即告知本站,我们将及时删除并致以最深的歉意!
3、本站转载文章版权归原作者所有,内容为作者个人观点,本站只提供参考并不构成任何投资及应用建议。任何本站转载文章链接及得到的资讯、产品、服务,本站概不负责,亦不负任何法律责任。
暂无评论

发送评论 编辑评论


				
上一篇
下一篇