1,菜单函数 wp_nav_menu()
wp_nav_menu( array( 'theme_location' => '', //[保留]用于在调用导航菜单时指定注册过的某一个导航菜单名,如果没有指定,则显示第一个 'menu' => 'top_main1', //[可删]使用导航菜单的名称调用菜单,可以是 id, slug, name (按顺序匹配的) 。 'container' => false, //[可删]最外层容器标签名 'container_class' => '',//[可删]最外层容器class名 'container_id' => '',//[可删]最外层容器id值 'menu_class' => 'menu', //[可删]ul 节点的 class 属性值 'menu_id' => '', //[可删]ul 节点的 id 属性值 'echo' => true, //[可删]确定直接显示导航菜单还是返回 HTML 片段,如果想将导航的代码作为赋值使用,可设置为false 'fallback_cb' => 'wp_page_menu', //[可删]备用的导航菜单函数,用于没有在后台设置导航时调用 'before' => '', //[可删]显示在导航a标签之前 'after' => '', //[可删]显示在导航a标签之后 'link_before' => '', //[可删]显示在导航链接名之前 'link_after' => '', //[可删]显示在导航链接名之后 'items_wrap' => ' %3$s ', //[可删]使用字符串替换修改ul的class 'depth' => 0, //[可删]显示菜单的深度, 当数值为0时显示所有深度的菜单,-1所有菜单平级显示 'walker' => '' //[可删]自定义的遍历对象,调用一个对象定义显示导航菜单 ));
或者替换子菜单类名:
$nav_str = wp_nav_menu( array( 'theme_location' => '', //[保留]用于在调用导航菜单时指定注册过的某一个导航菜单名,如果没有指定,则显示第一个 'menu' => 'top_main1', //[可删]使用导航菜单的名称调用菜单,可以是 id, slug, name (按顺序匹配的) 。 'container' => false, //[可删]最外层容器标签名 'container_class' => '',//[可删]最外层容器class名 'container_id' => '',//[可删]最外层容器id值 'menu_class' => 'nav-list', //[可删]ul 节点的 class 属性值 'menu_id' => '', //[可删]ul 节点的 id 属性值 'echo' => false, //[可删]确定直接显示导航菜单还是返回 HTML 片段,如果想将导航的代码作为赋值使用,可设置为false 'fallback_cb' => 'wp_page_menu', //[可删]备用的导航菜单函数,用于没有在后台设置导航时调用 'before' => '', //[可删]显示在导航a标签之前 'after' => '', //[可删]显示在导航a标签之后 'link_before' => '', //[可删]显示在导航链接名之前 'link_after' => '', //[可删]显示在导航链接名之后 'items_wrap' => ' %3$s ', //[可删]使用字符串替换修改ul的class 'depth' => 0, //[可删]显示菜单的深度, 当数值为0时显示所有深度的菜单,-1所有菜单平级显示 'walker' => '' //[可删]自定义的遍历对象,调用一个对象定义显示导航菜单 )); echo str_replace('sub-menu', 'select', $nav_str);
2,分页函数 the_posts_pagination()
the_posts_pagination(array( 'mid_size' => 3, 'prev_text' => '<', 'next_text' => '>', 'screen_reader_text' => ' ' //这里一定要设置为空格,不然会有h2的内容显示 ));
3, bloginfo 博客信息函数
'name' - 显示在 设置 > 常规 中设置的“站点标题”。 'description' - 显示在 设置 > 常规 中设置的“副标题”。 'wpurl' - 显示在 设置 > 常规 中设置的 “WordPress 地址 (URL)”。可以考虑使用 site_url() 来代替 'siteurl' / 'url' - 显示在 设置 > 常规 中设置的 “站点地址(URL)”)”。该数据是从 可以考虑使用 home_url() 代替 'admin_email' - 显示在 设置 > 常规 中设置的 “电子邮件地址”。 'stylesheet_url' - 显示当前使用的主题的 CSS文件(通常为 style.css)路径。可以考虑使用 get_stylesheet_uri() 代替。 'stylesheet_directory' - 显示当前使用的主题的样式表路径。可以考虑使用 get_stylesheet_directory_uri() 代替。 'template_url' / 'template_directory' - 当前主题的 URL 路径
4,常用模版文件
index.php 主模板文件。 所有主题都是必需的。 style.css 主要样式表。 它在所有主题中都是必需的,并且包含主题的信息标题。 rtl.css 如果网站语言的文本方向是从右到左,则自动包含从右到左的样式表。 comments.php 评论模板。 front-page.php 首页模板始终用作站点首页(如果存在),无论管理员>设置>阅读上的设置如何。 home.php 默认情况下,主页模板是首页模板。 如果您没有将WordPress设置为使用静态首页,则此模板用于显示最新的帖子。 header.php 标题模板文件通常包含您的站点的文档类型,元信息,样式表和脚本的链接以及其他数据。 singular.php 单独的模板用于没有找到single.php的帖子,或者当没有找到page.php的页面时。 如果没有找到singular.php,则使用index.php。 single.php 当访问者请求单个帖子时,使用单个帖子模板。 single-{post-type}.php 访问者从自定义帖子类型请求单个帖子时使用的单个帖子模板。 例如,single-book.php将用于从定制的帖子类型命名的书中显示单个帖子。 如果不存在自定义帖子类型的特定查询模板,则使用index.php。 archive-{post-type}.php 当访问者请求自定义帖子类型归档时,将使用归档文件类型模板。 例如,archive-books.php将用于显示自定义帖子类型命名书籍的帖子存档。 如果archive-{post-type} .php不存在,则使用archive.php模板文件。 page.php 当访问者请求单独的页面(内置模板)时,将使用页面模板。 page-{slug}.php 访问者请求特定页面时使用页面插件模板,例如使用“about”slug(page-about.php)的页面插件模板。 category.php 当访问者按类别请求帖子时,将使用类别模板。 tag.php 当访问者通过标签请求帖子时,使用标记模板。 taxonomy.php 当访问者在自定义分类法中请求术语时,将使用分类术语模板。 author.php 访问者加载作者页面时,将使用作者页面模板。 archive.php 当访问者按类别,作者或日期请求帖子时,使用归档模板。 注意:如果存在类似于category.php,author.php和date.php的更多特定模板,则此模板将被覆盖。 search.php 搜索结果模板用于显示访问者的搜索结果。 attachment.php 当查看单个附件(如图像,pdf或其他媒体文件)时,将使用附件模板。 image.php 图像附件模板是attachment.php的更具体的版本,在查看单个图像附件时使用。 如果不存在,WordPress将使用attachment.php。 404.php 当WordPress找不到与访问者请求相匹配的帖子,页面或其他内容时,将使用404模板。
5,搜索表单函数 get_search_form($echo)
参数 $echo : (布尔值) (可选) 如果是 true 则输出表单; false 则返回表单的字符串。如果你的主题没有 searchform.php, WordPress 将使用其内置的搜索表单。
6,获取指定分类的文章列表 (全局可用)
<?php query_posts('cat=20&posts_per_page=4'); while(have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?></a></li> <?php $thumbnail_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_post()->ID), 'full'); if(!empty($thumbnail_image)): ?> <img src="<?php echo $thumbnail_image[0]?>" alt="<?php the_title(); ?>"> <?php else: ?> <img src="<?php bloginfo('template_url'); ?>/assets/images/default.jpg"> <?php endif ?> <?php endwhile; wp_reset_query(); ?>
$cat=get_term_by('id', 12, 'category'); // 获取指定分类详情 $cat_links=get_category_link($cat->term_id); // 获取指定分类链接
获取指定分类 或者指定标签的文章
$args = array( "cat" => 2, "posts_per_page" => 3,"tag__in" => array(23) ); query_posts($args); while(have_posts()): the_post();
7,获取标签列表函数
get_tags(array( 'orderby' => '', 'order' => 'ASC' ))
文档 https://developer.wordpress.org/reference/functions/get_tags/
8,禁止 Wordpress 自动保存草稿
在 wp-config.php 中加入如下代码,要加载文件最前面。
define('AUTOSAVE_INTERVAL', 864000); // 设置自动保存草稿时间是10天 define('WP_POST_REVISIONS', false); // WordPress禁用文章修订
9,breadcrumb 面包屑导航,放到模版的 function.php 文件中
function get_breadcrumbs() { global $wp_query; if (!is_home()) { // Start the UL echo ''; // Add the Home link $url = get_bloginfo('siteurl'); echo '<a href="'.$url.'">网站首页</a>'; if (is_category()) { $catTitle = single_cat_title("", false); $cat = get_cat_ID($catTitle); echo " » " . get_category_parents($cat, TRUE, " » ") . ""; } elseif (is_archive() && !is_category()) { echo " » Archives"; } elseif (is_search()) { echo " » Search Results"; } elseif (is_404()) { echo " » 404 Not Found"; } elseif (is_single()) { $category = get_the_category(); $category_id = get_cat_ID($category[0]->cat_name); echo ' » ' . get_category_parents($category_id, TRUE, " » "); echo the_title('', '', FALSE) . ""; } elseif (is_page()) { $post = $wp_query->get_queried_object(); if ($post->post_parent == 0) { echo " » " . the_title('', '', FALSE) . ""; } else { $title = the_title('', '', FALSE); $ancestors = array_reverse(get_post_ancestors($post->ID)); array_push($ancestors, $post->ID); foreach ($ancestors as $ancestor) { if ( $ancestor != end($ancestors) ) { echo ' » ' . strip_tags(apply_filters('single_post_title', get_the_title($ancestor))) . ''; } else { echo ' » ' . strip_tags(apply_filters('single_post_title', get_the_title($ancestor))) . ''; } } } } // End the UL echo ""; } }
10,文章详情页标签
the_title(); // 标题
echo get_post_meta(15, 'views', true); // 点击次数,这个不能用
comments_number('暂无评论', '1条评论', '% 评论' ); // 评论次数
the_time(); // 发布时间
//文章详情 if ( have_posts() ) { while ( have_posts() ) { the_post(); the_content(); // get_template_part( 'template-parts/content', get_post_type() ); } }
11,文章列表页标签
if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); the_author(); the_excerpt(); // 简介 the_permalink(); // 文章链接 if(has_post_thumbnail()){ img src="the_post_thumbnail();" } echo get_post_meta(15, 'views', true);?> 阅读 comments_number('暂无评论', '1条评论', '% 评论' ); the_time() endwhile; endif;
12, 文章列表页 获取 文章分类
the_category(', '); // 只包含 a 标签
the_category();
single_cat_title(); //在分类页面调用当前分类名称
$cat // 在分类页面,可以直接使用
get_the_category() // 这个函数在分类页没有文章内容时,输出为空。 所以,在文章列表页,我使用了 single_cat_title() 获取文章标题。
// 推荐方法 ********
global $wp_query; $cat_ID = get_query_var('cat'); $category = get_category($cat_ID);
13, 文章页 想调用出当前文章所属分类的名称
foreach((get_the_category()) as $category) { echo $category->cat_name; } // 或 $category = get_the_category(); echo $category[0]->cat_name; // 或 if(is_category()) { $cat = get_query_var('cat'); $yourcat = get_category($cat); echo "该分类别名为" . $yourcat->slug; }
the_ID(); get_the_ID(); 当前文章ID标签
14, 菜单相关的
wp_get_nav_menu_items() // 获取所有自定义菜单数组
wp_nav_menu_objects() //
15, 获取当前菜单的父菜单的所有子菜单
/** * 获取父分菜单和父菜单的子菜单 【侧边栏使用】 * 注意1:本函数要求所有文章只能属于一个菜单目录 * 注意2:本函数要求顶级导航不得是文章 * @param [type] $menuName 菜单名称 * @param [type] $object_id * @return void * @author ngtwewy < 62006464@qq.com > * @since 2020-06-13 */ function get_menu_parent_by_id($menuName) { $parentItem = ''; // 获取菜单所有对象 $items = wp_get_nav_menu_items($menuName); // p($items);die(); // 获取当前 $object_id 的类型,是category page article $object_id = 0; $object_type = ''; if (is_category()) { // 分类目录 $object_id = get_queried_object_id(); $object_type = "category"; // echo "<br>xxxxx: 1"; } elseif (is_archive() && !is_category()) { $category = get_the_category(); $object_id = $category[0]->cat_ID; $object_type = "post"; // echo "<br>xxxxx: 2"; } elseif (is_single() && !is_page()) { // 文章页 $category = get_the_category(); $object_id = $category[0]->cat_ID; $object_type = "category"; // echo "<br>xxxxx: 3"; } elseif (is_page()) { // 页面 $object_type = "page"; $object_id = get_queried_object_id(); // echo "<br>xxxxx: 4"; } // 获取 $object_id 对应的 menu_item $currentMenuItem = ''; foreach ($items as $k => $v) { $v = (array)$v; if($object_id == $v['object_id'] && $object_type==$v['object']){ $currentMenuItem = $v; break; } } if($currentMenuItem == ''){ return "object_id 不属于菜单"; } // 获取父级菜单 if($currentMenuItem['menu_item_parent'] == 0){ // 自己本身就是父级 $parentItem = (array)$currentMenuItem; }else{ // 获取父 menu_item $parentId = $currentMenuItem['menu_item_parent']; // 获取父 menu_item ID // echo "<br>获取父 menu_item: ".$parentId; foreach ($items as $k => $v) { $v = (array)$v; if($parentId == $v['ID']){ $parentItem = (array)$v; } } } // 获取所有子菜单 $parentItem['children'] = []; foreach ($items as $k => $v) { $v = (array)$v; if($parentItem['ID'] != 0 && $parentItem['ID'] == $v['menu_item_parent']){ $v['my_current_object_id'] = $object_id; $parentItem['children'][] = $v; } } return $parentItem; } $parentMenu = get_menu_parent_by_id('top_main1', $category_id);
16, 不同分类使用不同分类模版(通过别名判断)
global $wp_query; $cat_ID = get_query_var('cat'); $currentCategory = get_category($cat_ID); // p($currentCategory); $prefix = substr($currentCategory->slug, 0, 8); if($prefix == "product-"){ get_template_part('product'); }else{ get_template_part('list'); }
17, 不同“页面”使用不同模版
在编写页面模版的时候,添加注释:
/* Template Name: Contact 模版 */
添加以后,在编辑页面的时候,模版选项下拉菜单中会出现 “Contact 模版”,保存即可。
不同“文章”使用不同模版
/** * Template Name: 产品页模版 * Template Post Type: post */
18, 获取特色图片
获取特色图像:小
$thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail');
echo $thumbnail_image_url[0];
第二个参数可以是:medium,中。 large,大。 full,完整。 array(100,100),指定100X100。
19, 输出文章页自定义字段
在文章编辑页面->选项->高级面板->自定义字段 激活自定义字段。
echo get_post_meta($post->ID,"制作周期",true);
20, Wordpress 判断 移动端 切换 主题
从WordPress3.4版本开始,已支持 wp_is_mobile() 函数。
主题切换代码。在\wp-content\plugins目录新建mobile_switch_theme.php文件(或放入新文件夹中),可以在代码中指定主题文件名,最后再wp后台启用插件即可。
<?php /*Plugin Name: mobile_switch_theme*/ function mobile_switch_theme($theme){ if( wp_is_mobile() ){ $theme = 'lee3.0-m'; //theme为主题名 } return $theme; } add_filter('template', 'mobile_switch_theme'); add_filter('stylesheet', 'mobile_switch_theme'); ?>
21. 增加分类的自定义字段
在主题的 function.php 中添加
// 分类添加字段 function ems_add_category_field(){ echo '<div class="form-field"> <label for="cat-keywords">关键词</label> <input name="cat-keywords" id="cat-keywords" type="text" value="" size="40"> <p>输入关键词</p> </div>'; } add_action('category_add_form_fields','ems_add_category_field',10,2); // 编辑分类字段 function ems_edit_category_field($tag){ echo '<tr class="form-field"> <th scope="row"><label for="cat-keywords">关键词</label></th> <td> <input name="cat-keywords" id="cat-keywords" type="text" value="'; echo get_option('cat-keywords-'.$tag->term_id).'" size="40"/><br> <p class="description">'.$tag->name.' 关键词</p> </td> </tr>'; } add_action('category_edit_form_fields','ems_edit_category_field',10,2); // 保存数据 function ems_taxonomy_metadate($term_id) { if (isset($_POST['cat-keywords'])) { //判断权限--可改 if (!current_user_can('manage_categories')) { return $term_id; } $cat_key = 'cat-keywords-' . $term_id; // key 选项名为 cat-keywords-1 类型 $cat_value = $_POST['cat-keywords']; // value // 更新选项值 update_option($cat_key, $cat_value); } } add_action('created_category','ems_taxonomy_metadate',10,1); add_action('edited_category','ems_taxonomy_metadate',10,1); /* ------------------------------------------------------------------------------------ */
获取自定义字段:
$term_id = get_queried_object_id(); $cat_img = get_option('cat-keywords-'.$term_id);
22,获取头像
头像尺寸是 int
获取指定ID用户头像
<?php echo get_avatar(用户id, 头像尺寸); ?>
获取文章作者头像
注意,请在循环内使用,要不然出错。
<?php echo get_avatar(get_the_author_meta('email'), 头像尺寸); ?>
获取指定邮箱头像
<?php echo get_avatar('用户邮箱', 头像尺寸); ?>