4,wordpress 模板标签使用
wp_title() 系统标题
| wp_title(参数一,参数二,参数三) 首页使用参数无效,默认分隔符">>" |
| |
| 参数一:自定义分隔符 |
| |
| 参数二:true 和 false |
| |
| 参数三:分隔符的作用 left 和 right |
the_title() 标题
| 注意:该函数只能在主循环里面使用,如果在index.php调用的是所有文章标题,在singer.php里面只显示一篇文章标题,在page.php调用的是页面的标题 |
| the_title($before, $after, $bool) |
| 参数一:在标题前面追加字符串 |
| 参数二:在参数后面追加字符串 |
| 参数三:true:直接输出标题,false:需要用标量接收或者echo输出 |
| 例如:the_title("<b style='color:red;'>", '</b>', true) |
| |
| <?php if(have_posts()): ?> |
| <?php while(have_posts()): the_post(); ?> |
| // 在这里调用数据 文章标题 |
| <?php the_title();?> |
| <?php endwhile;?> |
| |
| <?php else:?> |
| // 如果没有在这里提示一个信息 |
| <?php endif;?> |
| |
the_permalink() 获取文章的链接
the_content() 显示当前页面的文章内容
| # 显示当前页面的文章内容 |
| 参数一:$more_link_text ,文章内容太多的时候,只显示部分内容会出现一个更多按钮点击更多可以显示全文 默认为[更多...] 可替换为你自己的参数[点击展示更多] |
| 参数二:使用不到 |
| 例如:the_content("点击显示更多") |
the_excerpt()
get_post_meta() 获取文章自定义的字段
| # 获取文章自定义的字段 |
| 例如: |
| <?php global $post; ?> |
| <p><?php echo get_post_meta($post->ID, 'offer', true); ?></p> |
| |
the_category() 分类
| # 该函数只能在主循环里面使用,获取文章对应的分类,并且是超链接的形式,点击可以跳到当前分类的文章列表 |
| 参数一:多个分类的分隔符 |
| 参数二:multiple (如果未勾选父分类,自动加上父分类,展示的是多个分类超链接) 通常不需要 |
| single (如果未勾选父分类,自动加上父分类,展示的是一个整体超链接) |
| 参数三:要调取哪篇文章的所属分类,一般不需要 |
| |
get_the_category() 获取分类返回值
| <?PHP |
| $categories = get_the_category(); |
| $tags = get_the_tags(); |
| if ( ! empty( $categories ) ) { |
| |
| echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $tags[0]->name ) . '</a>'; |
| } |
| ?> |
| |
| 参数一:追加内容到标签的前面,默认值是标签 |
| 参数二:标签之间的分隔符默认逗号分开 |
| 参数三:在标签之后追加内容 |
| the_tags('所属标签:', '-') |
| <?PHP |
| $categories = get_the_category(); |
| $tags = get_the_tags(); |
| if ( ! empty( $categories ) ) { |
| echo '<a href="' . esc_url( get_tag_link( $tags[0]->term_id ) ) . '">' . esc_html( $tags[0]->name ) . '</a>'; |
| } |
| ?> |
get_template_directory_uri() 当前主题路径
| |
| <?php echo get_template_directory_uri(); ?> |
the_author()
the_author_posts_link()
the_time()
| # 该函数只能在主循环里面使用,获取文章的发布时间 |
| 参数一:Y:年 m:月 d:日 h:时 i:分 s:秒 |
| <p>发布时间:<?php the_time('Y-m-d h:i:s');?></p> |
| |
| |
| 参数一:(参数为任何值)当没有任何评论的时候修改wordpress的默认值 【hahaha无评论】可以改成 【0条评论】 |
| 参数二:(参数为任何值)当只有一条评论的时候可以修改提示的默认值 |
| 参数三:(参数为任何值)当有多条评论的时候修改默认值 % 号标识评论的条数 |
| 参数四:添加一个class属性 |
| 参数五:当评论被关闭显示的内容,只有在没有评论的文章里面生效,有评论的文章此参数无效 |
| 例如:comments_popup_link('0条评论','1条评论', '%条评论', 'tiaoshu', '评论关闭') |
| |
edit_post_link() 文章编辑
| |
| 参数一:改超链接的文本内容:默认是编辑可以改成自己想要的名称 |
| 参数二:before 追加内容在默认文本前面 |
| 参数三:after 追加内容在默认文本后面 |
| 参数四:指定编辑的文章ID |
next_post_link() 文章链接
| |
| 参数一:默认参数'>'' next_post_link('%link>>') 修改指示符 |
| 参数二:改变超文本链接内容 next_post_link('%title') |
| 参数三:bool值 true在同一分类下 改成 false 显示不同分类的下一篇 |
| 参数四:排除某些分类下面的文章字符串或者数据,内容为分类的ID |
| 参数五:同一个标签的下一篇 |
| |
previous_post_link() 文章链接
next_posts_link() 下一页
previous_posts_link() 上一页
bloginfo($show)
| show(不填默认就是 'name') |
| (string)关于你的博客的详细信息。有效的参数值: |
| 'name' - 站点的标题) |
| 'description' - 站点的副标题 |
| 'url' -你的博客的网站地址的URL。 |
| 'rdf_url' -RDF/RSS 1.0 feed的URL。 |
| 'rss_url' -RSS 0.92 feed的URL。 |
| 'rss2_url' - RSS 2.0 feed的URL。 |
| 'atom_url' -Atom feed的URL。 |
| 'comments_rss2_url' - URL for comments RSS 2.0 feed. |
| 'pingback_url' -Pingback (XML-RPC 文件)的URL。 |
| 'admin_email' - 管理员的电子邮件地址;设置在一般选项中。 |
| 'charset' -你的博客的字符编码;设置在阅读选项中。 |
| 'version' - 你的博客使用的WordPress版本。 |
| 'text_direction' - 从右到左,返回'rtl',从左到右返回'ltr'(默认)。 |
| 下面的,在WordPress1.5版本或者以后的版本中运行:: |
| |
| 'html_type' - 你的博客的 "内容类型" 。 |
| 'wpurl' - WordPress安装的URL。 |
| 'template_url' - 使用的模板的URL。 |
| 'template_directory' - 模板的目录的URL。 |
| 'stylesheet_url' - 主要的CSS文件的URL。返回: http//example.com/wp-content/themes/ + your-active-theme-name(value from wp_options, "stylesheet" row) + "/style.css"(hardcoded in wp-includes/theme.php) |
| 取消的参数 |
| 'stylesheet_directory' - 样式表目录的URL。 (2.3.1版本中这个已经取消了) |
| |
5,主循环
| <?php if(have_posts()): ?> |
| <?php while(have_posts()): the_post(); ?> |
| |
| <?php the_title();?> |
| <?php endwhile;?> |
| |
| <?php else:?> |
| |
| <?php endif;?> |
6,wordpress主题特色功能
开启Feed自动链接
add_theme_support() 特色图(即缩列图)
| |
| 参数一:post-thumbnails |
| add_theme_support('post-thumbnails'); 在主题下面的functions.php 里面添加此参数 |
| |
| 参数二:array() 告诉wordpress哪些页面需要开启特色图片 |
| add_theme_support('post-thumbnails', array('post')); 在主题下面的functions.php 里面添加此参数 |
| add_theme_support( 'post-formats', array( 'aside', 'gallery' ) ); |
| add_theme_support( 'post-thumbnails' ); |
| add_theme_support( 'post-thumbnails', array( 'post' ) ); |
| add_theme_support( 'post-thumbnails', array( 'page' ) ); |
| add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); |
| |
| |
| the_post_thumbnail(); |
| |
| |
| |
| the_post_thumbnail(array(100,300)); |
| |
| |
| the_post_thumbnail( 'thumbnail' ); |
| the_post_thumbnail( 'medium' ); |
| the_post_thumbnail( 'medium_large' ); |
| the_post_thumbnail( 'large' ); |
| the_post_thumbnail( 'full' ); |
| |
| |
| the_post_thumbnail( 'shop_thumbnail' ); |
| the_post_thumbnail( 'shop_catalog' ); |
| the_post_thumbnail( 'shop_single' ); |
has_post_thumbnail() 判断有没有缩略图
| register_nav_menu($location, $description); |
| // 开启导航功能 $location, $description |
| register_nav_menu('header_menu', '网站顶部导航'); |
| register_nav_menu('sidebar_menu', '网站侧部导航'); |
| register_nav_menu( 'footer_menu', '网站底部导航'); |
| |
| wp_nav_menu($args=array()) |
| $defaults = array( |
| 'theme_location' => '', |
| 'menu' => '', |
| 'container' => 'div', |
| 'container_class' => '', |
| 'container_id' => '', |
| 'menu_class' => 'menu', |
| 'menu_id' => '', |
| 'echo' => true, |
| 'fallback_cb' => 'wp_page_menu', |
| 'before' => '', |
| 'after' => '', |
| 'link_before' => '', |
| 'link_after' => '', |
| 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', |
| 'depth' => 0, |
| 'walker' => '' |
| ); |
| wp_nav_menu( $defaults ); |
| |
| # 简单使用 |
| $defaults = array( |
| 'theme_location' => 'header_menu' |
| ); |
| wp_nav_menu( $defaults ); |
| // 开启主题侧边栏的功能 小工具 参数: register_sidebar(array()); |
| |
| name: 侧边栏的名字(默认是 ‘Sidebar’ 加 数字 ID) |
| id: 侧边栏 ID,必须全部小写,不带空格(默认是一个自动递增的数字 ID) |
| description: 用来说明侧边栏是什么,在哪里显示的文字。会在小工具管理界面显示。(默认为空) |
| class: 配到小工具 HTML输出 中的CSS选择器名字(默认为空) |
| before_widget:在每个小工具前面输出的 HTML代码(默认: '<li id="%1$s" class="widget %2$s">')注:使用sprintf的变量替换 |
| after_widget: 在每个小工具后面输出的 HTML代码(默认: “</li>\n“) |
| before_title: 在标题前输出的 HTML代码(默认: <h2 class="widgettitle">) |
| after_title: 在标题后输出的 HTML代码 (默认:”</h2>\n“) |
| |
| <?php |
| $args = array( |
| 'name' => __( 'Sidebar name', 'theme_text_domain' ), |
| 'id' => 'unique-sidebar-id', |
| 'description' => '', |
| 'class' => '', |
| 'before_widget' => '<li id="%1$s" class="widget %2$s">', |
| 'after_widget' => '</li>', |
| 'before_title' => '<h2 class="widgettitle">', |
| 'after_title' => '</h2>' |
| ); |
| ?> |
| |
| |
| |
| # 使用案例:在主题下面的functions.php文件里面注册 |
| register_sidebar(array( |
| "name" => '主侧边栏', |
| "id" => 'sidebar-1' |
| )); |
| |
| register_sidebar(array( |
| "name" => '左侧边栏', |
| "id" => 'sidebar-2' // 多个侧边栏 id 一定要改 |
| )); |
| |
| |
| if(dynamic_sidebar("sidebar-1")) { |
| dynamic_sidebar("sidebar-1") |
| } |
| |
| |
| 参数主要传:开启工具的 id 值 或者 name 值 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)