WordPress备忘录

适用于初学者和开发人员的完整备忘录
Zac
Web360主编
上次更新时间:2021年6月18日

最常见的功能、命令和键盘快捷键,可帮助你完成WordPress主题开发之旅。

WP-CLI备忘录

WP-CLI是WordPress的命令行界面。你可以在不使用网页浏览器的情况下更新插件、配置多站点安装等等。

下载WordPress

wp core download

生成wp-config.php文件

wp core config --dbname=
--dbuser= --dbpass=
--dbprefix=

安装WordPress

wp core install --url=​"your_domain_name"
--title=​"Your Blog Title"​ --admin_user=​"admin"
--admin_password=​"your_password"
--admin_email=​"your_email"

搜索插件

wp plugin search yoast

安装插件

wp plugin install pluginname

列出插件

wp plugin list

列出已安装的主题

wp theme list

搜索新主题

wp theme search keyword

安装主题

wp theme install bootstrap-four

激活主题

wp theme activate bootstrap-four

列出文章

wp post list

编辑文章

wp post edit 1

文章更新

wp post update 1
--post_title=​"Your New title..."

创建文章

wp post create
--post_status=publish
--post_title=​"Second Post"
--edit

登录WordPress数据库

wp db cli

列出WordPress用户

wp db query ​"SELECT user_login, ID FROM wp_users;"

更改WordPress文章作者

wp post update 6 --post_author=1

优化数据库

wp db optimize

更新WordPress

wp core update

更新WordPress数据库

wp core update-db

更新所有插件

wp plugin update --all

WordPress主题开发备忘录

WordPress主题定义

主题的信息保存在主题的style.css文件中。当你在外观>主题或WordPress的主题库上查看主题时,会显示这些信息。

/*
Theme Name: Twenty Seventeen
Theme URI: https://wordpress.org/themes/twentyseventeen/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Twenty Seventeen brings your site to life with immersive featured images and subtle animations. With a focus on business sites, it features multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asymmetrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works great in many languages, for any abilities, and on any device.
Version: 1.0
License: GNU General Public License v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentyseventeen
Tags: one-column, two-columns, right-sidebar, flexible-header, accessibility-ready, custom-colors, custom-header, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/

WordPress主题文件

每个WordPress主题应具有的基本文件:

style​.css          // 主题的主样式表文件
index​.php          // 主模板文件
single​.php         ​// 单个文章文件。
                   // ..仅用于显示单个文章
archive​.php        // 存档或类别模板文件
searchform​.php     // 搜索表单文件 
search​.php         // 搜索结果文件
404​.php            // 404错误页面文件
                   // ..如果找不到页面将显示。
comments​.php       // 评论模板文件
footer​.php         // 页脚内容文件
header​.php         // 页眉内容文件
sidebar​.php        // 侧边栏内容文件
page​.php           // 单页文件。仅用于页面。

WordPress主题剖析

header.php

get_header​();
wp_nav_menu​(); ​// (registered in functions.php)
get_search_form​();

循环

index​.php
home​.php
archive​.php
page​.php
single​.php
comments_template​();
search​.php
author​.php
404​.php

sidebar.php

get_sidebar​()

footer.php

get_footer​()

没有显示

style​.css     // 主题样式
functions​.php // 主题函数
comments​.php  // 评论模版

WordPress模板标签

模板标签在主题中用于从数据库中检索内容。

内容可以是从博客标题到完整侧边栏的任何内容。

模板标签是将内容添加入主题的首选方法,因为:它们可以显示动态内容;它们可以用于多个主题文件;它们将主题分成更小、更容易理解的部分。

the_content​()                      获取文章内容
the_excerpt​()                      获取文章摘要
the_title​()                        获取文章标题
the_permalink​()                    显示文章链接
the_category​(​', '​)                 显示类别中的一篇文章
the_author​()                       显示文章作者
the_ID​()                           显示文章ID
edit_post_link​()                   Show Edit link for a post
next_post_link​(​'%link'​)            显示下一个页面URL
previous_post_link​(​'%link'​)        显示上一个页面URL
get_links_list​()                   检索博客文章列表链接
wp_list_pages​()                    检索所有页面
wp_get_archives​()                  检索网站存档
wp_list_cats​()                     检索所有类别
get_calendar​()                     显示内置的WordPress日历
wp_register​()                      显示注册链接
wp_loginout​()                      显示登录或注销链接(对于注册用户)

包含标签

使用这些标签将模板包含到你的主题中。

          包含header.php并显示其内容
         包含sidebar.php
 >        包含footer.php
   为评论加载特定的模板

常用的页眉函数

site_url​()                              获取WordPress网站网址
wp_title​()                              获取页面标题
bloginfo​(​'name'​)                        获取博客名称
bloginfo​(​'description'​)                 获取博客描述
get_stylesheet_directory_uri​()          获取样式表目录URI
bloginfo​(​'atom_url'​)                    获取Atom提要URL
bloginfo​(​'rss2_url'​)                    RSS2.0网址

循环

循环是WordPress通过主题的模板文件输出文章的默认机制。


  
    // Display post content 
  

WordPress菜单和侧边栏

默认导航菜单

<?php​ wp_nav_menu(); ​?>

特定导航菜单

<?php​ wp_nav_menu( ​array​(​'menu'​ => My Navigation​' )); ?>

基于类别的导航

<​ul​ ​id​=​"menu"​>
  <​li​  class="current-cat" ​​>
    <​a​ ​href​=​""​>Home</​a​>
  </​li​>
  ​
</​ul​>

基于页面的导航

<​ul​ ​id​=​"menu"​>
  <​li​  class="current-page-item" ​​>
    <​a​ ​href​=​""​>Home</​a​>
  </​li​>
​  <?php​ wp_list_pages(​'sort_column=menu_order&depth=1&title_li='​);?​ >
</​ul​>

添加新的侧边栏

将以下代码添加到functions.php文件中,以添加一个新的侧边栏。

add_action( ​'widgets_init'​, ​'theme_slug_widgets_init'​ );
function​ ​theme_slug_widgets_init​() {
  register_sidebar( ​array​(
​    'name'​ => __( ​'My Sidebar'​, ​'theme-slug'​ ),
​    'id'​ => ​'sidebar-1'​,
​    'description'​ => __( ​'Description',​ ​'theme-slug'​ ),
    'before_widget'​ => ​'<li id="%1$s" class="widget %2$s">'​,
    'after_widget'​ => ​'</li>'​,
    ​'before_title'​ => ​'<h2 class="widgettitle">'​,
​    'after_title'​ => ​'<h2>'​,
  ));
}

WordPress键盘快捷键

Alt + Shift + 按键

Windows / Linux:“ALT + SHIFT + 按键”

Mac:“Ctrl + Option (alt) + Key ”。(WordPress 4.2 以下版本使用“Alt + Shift + 按键”)。

n          检查拼写 (需要插件)
l          左对齐
j          对齐文本
c          对齐中心
d          删除线
r          右对齐
u          • 列表
a          插入链接
o          1. 列表
s          删除链接
q          引用
m          插入图像
w          无分心写作模式
t          插入更多标签
p          除插入分页符标签
h          帮助
x          添加/删除代码标签
1          H1
2          H2
3          H3
4          H4
5          H5
6          H6
9          地址

Ctrl + 按键

Windows / Linux:“Ctrl + 按键”

Mac:“Command + 按键”

c          复制
v          粘贴
a          全选
x          剪切
z          撤销
y          重做
b          粗体
i          斜体
u          下划线
k          插入/编辑链接

格式化快捷方式

使用可视化编辑器时的格式化快捷方式。

*         开始一个无序列表
-         开始一个无序列表
1.        开始一个有序列表
1)        开始一个有序列表
##        H2
###       H3
####      H4
#####     H5
######    H6
>         将文字转换为块引用
---       水平线
`..`      将文字转换为代码块

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注