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() 获取文章的链接

1,没有参数,没有返回值
2,获取文章的链接

the_content() 显示当前页面的文章内容

# 显示当前页面的文章内容
参数一:$more_link_text  ,文章内容太多的时候,只显示部分内容会出现一个更多按钮点击更多可以显示全文 默认为[更多...] 可替换为你自己的参数[点击展示更多]
参数二:使用不到
例如:the_content("点击显示更多")

the_excerpt()

# 获取文章的摘要,如果文章没有摘要获取文章的部分内容当中的55个字符当做摘要
# 无参数

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( $categories[0]->name ).'</a>';
		echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $tags[0]->name ) . '</a>';
	}
?>

the_tags() 标签

# 该函数只能在主循环里面使用,获取文章对应的标签,以超链接的方式给出
参数一:追加内容到标签的前面,默认值是标签
参数二:标签之间的分隔符默认逗号分开
参数三:在标签之后追加内容
the_tags('所属标签:', '-')

get_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>

comments_popup_link() 评论链接

# 该函数只能在主循环里面使用,生成一个链接,调到评论框
参数一:(参数为任何值)当没有任何评论的时候修改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自动链接

bloginfo('rss_url'); // 获取链接

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' ) ); //仅在post中使用缩略图功能
add_theme_support( 'post-thumbnails', array( 'page' ) );//仅在page中使用缩略图功能
add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); //仅在 post 和 movies 中使用 

// 调用文章特色图像 在主循环里面使用
the_post_thumbnail(); 


// 自定义尺寸
the_post_thumbnail(array(100,300)); 

// 所有参数 Default WordPress
the_post_thumbnail( 'thumbnail' );     // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' );        // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' );  // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' );         // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' );          // Full resolution (original size uploaded)

//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
the_post_thumbnail( 'shop_catalog' );   // Shop catalog (300 x 300 hard cropped)
the_post_thumbnail( 'shop_single' );    // Shop single (600 x 600 hard cropped)

has_post_thumbnail() 判断有没有缩略图

参数一:指定文章的ID

register_nav_menu()导航菜单

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() 调用导航

wp_nav_menu($args=array())
$defaults = array(
	'theme_location'  => '',  	 	// 调用哪个位置的导航,使用导航菜单的名称调用菜单,可以是 id, slug, name (按顺序匹配的) 。
	'menu'            => '',			// 管理位置-》编辑 导航里面 menu 的值
	'container'       => 'div',		// 需不需要设置一个html元素包围ul元素,默认值: div
	'container_class' => '',			// ul 父节点的 class 属性值 默认值: menu-{menu slug}-container
	'container_id'    => '',			// ul 父节点的 id 属性值  默认值: None
	'menu_class'      => 'menu',		// ul 节点的 class 属性值。  默认值: menu
	'menu_id'         => '',				// ul 节点的 id 属性值。
	'echo'            => true,  		// 确定直接显示导航菜单还是返回 HTML 片段, 默认值: true (直接显示)
	'fallback_cb'     => 'wp_page_menu',  // 用于没有在后台设置导航时调的回调函数。默认值: wp_page_menu (显示页面列表作为菜单)
	'before'          => '',  // 显示在每个菜单链接前的文本。默认值: None
	'after'           => '',  // 显示在每个菜单链接后的文本。
	'link_before'     => '',  // 显示在每个菜单链接文本前的文本。(a链接里面的文案前面显示)
	'link_after'      => '',	// 显示在每个菜单链接文本后的文本。(a链接里面的文案后面显示)
	'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',  // 使用字符串替换修改ul的class。
	'depth'           => 0, 	// 显示菜单的深度, 当数值为 0 时显示所有深度的菜单。
	'walker'          => ''	  // 默认值:  new Walker_Nav_Menu
);
wp_nav_menu( $defaults );

# 简单使用
$defaults = array(
	'theme_location'  => 'header_menu'  // 开启导航设置的key值
);
wp_nav_menu( $defaults );

register_sidebar() 侧边栏小工具的开启

// 开启主题侧边栏的功能 小工具 参数: 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 一定要改
    ));

dynamic_sidebar() 调用开启的小工具

if(dynamic_sidebar("sidebar-1")) {  // 有效小工具返回真,没有小工具返回假
	dynamic_sidebar("sidebar-1")  // 侧边栏返回为真的时候显示侧边栏
}


参数主要传:开启工具的 id 值 或者 name 值