获取特定页面的链接
登录页面
<?php echo esc_url( wp_login_url() ); ?>
忘记密码页面
<?php echo esc_url( wp_lostpassword_url() ); ?>
站点根目录
<?php echo site_url(); ?>
申明对woocommerce插件的支持
在主题的functions.php文件中的任意位置添加代码
add_theme_support( 'woocommerce' );
后台侧边栏添加一级菜单
在自定义主题的functions.php中,使用add_menu_page()函数来添加菜单的内容
// 添加自定义菜单
function my_add_pages() {
// 第一个参数'Help page'为菜单名称,
// 第二个参数'使用帮助'为菜单标题
// 'manage_options' 参数为用户权限
// 'my_toplevel_page' 参数用于调用my_toplevel_page()函数,来显示菜单内容
add_menu_page('Help page', '我的菜单', 'manage_options', '自定义参数', 'my_toplevel_page');
}
function my_toplevel_page() {
echo '这里填菜单页面的HTML代码';
}
// 通过add_action来自动调用my_add_pages函数
add_action('admin_menu', 'my_add_pages');
后台侧边栏添加二级菜单
和添加一级菜单类似,如果是为一级菜单设置
添加,只需要将add_menu_page()函数换成add_options_page()即可,其他更多的选项,查询开发手册
add_options_page('Help page', '我的菜单', 'manage_options', '自定义参数', 'my_toplevel_page');
为新增菜单添加页面
关闭wordpress后台更新通知
在主题的functions.php增加下面的代码
add_action('after_setup_theme','remove_core_updates');
function remove_core_updates()
{
if(! current_user_can('update_core')){return;}
add_action('init', create_function('$a',"remove_action( 'init', 'wp_version_check' );"),2);
add_filter('pre_option_update_core','__return_null');
add_filter('pre_site_transient_update_core','__return_null');
}
移动端判断
wordpress自带函数,函数定义在wp-includes\vars.php中,如果是移动端,则返回true,不是返回false
wp_is_mobile()
获取最新发表的10篇文章
wp_get_recent_posts()函数用俩获取最新发表的文章,参数仫为10,我们可以传递自定义的参数进去,函数返回一个数组,数组元素就是文章的详情,包括了和文章相关的各种信息
$recent_posts = wp_get_recent_posts('10');
array
'ID' => int 1
'post_author' => string '1' (length=1)
'post_date' => string '2015-11-11 16:38:45' (length=19)
'post_date_gmt' => string '2015-11-11 08:38:45' (length=19)
'post_content' => string '欢迎使用WordPress。' (length=99)
'post_title' => string '世界,您好!' (length=18)
'post_excerpt' => string '' (length=0)
'post_status' => string 'publish' (length=7)
'comment_status' => string 'open' (length=4)
'ping_status' => string 'open' (length=4)
'post_password' => string '' (length=0)
'post_name' => string 'hello-world' (length=11)
'to_ping' => string '' (length=0)
'pinged' => string '' (length=0)
'post_modified' => string '2015-11-11 16:38:45' (length=19)
'post_modified_gmt' => string '2015-11-11 08:38:45' (length=19)
'post_content_filtered' => string '' (length=0)
'post_parent' => int 0
'guid' => string 'http://localhost/wordpress/?p=1' (length=31)
'menu_order' => int 0
'post_type' => string 'post' (length=4)
'post_mime_type' => string '' (length=0)
'comment_count' => string '1' (length=1)
'filter' => string 'raw' (length=3)
循环显示文章标题带链接
get_permalink()接受一个文章的id,返回文章的链接
$recent_posts = wp_get_recent_posts('10');
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
获取指定分类下面文章的数量
当前为获取分类名为news下面文章的数量
<?php get_category_by_slug('news')->count; ?>
获取指定分类下面的文章列表
请输入代码
获取分类名列表
wp_list_categories();
获取指定id的文章的分类名
$post=get_posts();
get_the_category($post[0]->ID);