欢迎访问我们的网站

选择语言

  • English
  • 简体中文
  • 日本語
  • 한국어
  • Français
  • Deutsch
  • Español
  • Português
  • Italiano
  • Русский
  • العربية
  • Nederlands
  • Türkçe
  • Polski
  • Svenska
  • Dansk

WordPress系列

02常用模板标签和路径函数大全–新手小白,快速使用静态页面建立wordpress主题

09.09.2025 25

1.主题路径相关函数
1.1get_template_directory() – 获取父主题目录的绝对路径

// 输出:/home/user/public_html/wp-content/themes/your-theme
1.2get_template_directory_uri() – 获取父主题目录的URL
1.3get_stylesheet_directory() – 获取子主题目录的绝对路径

2.网站信息函数
2.1 home_url() – 获取网站首页URL
2.2 site_url() – 获取WordPress安装目录的URL
2.3 bloginfo(‘description’) – 显示网站副标题/描述

2.4其他相关函数
bloginfo(‘url’); // 网站URL
bloginfo(‘wpurl’); // WordPress安装URL
bloginfo(‘charset’); // 字符编码
bloginfo(‘version’); // WordPress版本
bloginfo(‘html_type’); // 内容类型
bloginfo(‘language’); // 网站语言
bloginfo(‘stylesheet_url’); // 样式表URL
bloginfo(‘template_url’); // 模板目录URL
bloginfo(‘pingback_url’); // Pingback URL
bloginfo(‘atom_url’); // Atom feed URL
bloginfo(‘rss2_url’); // RSS 2.0 feed URL
bloginfo(‘rss_url’); // RSS feed URL
bloginfo(‘admin_email’); // 管理员邮箱

3.内容相关函数
3.1 the_title() – 显示文章标题
3.2 the_content() – 显示文章内容
3.3 the_excerpt() – 显示文章摘要
3.4 the_permalink() – 显示文章的永久链接

4.特色图像函数
4.1 has_post_thumbnail() – 判断文章是否有特色图像
4.2 the_post_thumbnail() – 显示文章特色图像
4.3 the_post_thumbnail_url() – 获取文章特色图像URL
4.4 the_post_thumbnail_caption() – 显示文章特色图像的标题
4.5 the_post_thumbnail_url() – 获取文章特色图像URL
4.6 the_post_thumbnail_caption() – 显示文章特色图像的标题

5.导航菜单函数

5.1 wp_nav_menu() – 显示导航菜单

PHP
123456789
<?php wp_nav_menu(array(
        'theme_location' => 'primary',
        'container' => 'nav',
        'container_class' => 'nav',
        'menu_class' => 'nav__list',
        'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
        'depth' => 1,
        'fallback_cb' => false,
    )); ?>

5.2 wp_page_menu() – 显示页面菜单

PHP
12345678
<?php wp_page_menu(array(
        'show_home' => true,
        'show_title' => true,
        'title' => 'Pages',
        'menu_class' => 'nav__list',
        'depth' => 1,
        'echo' => true,
    )); ?>

5.3 wp_list_pages() – 显示页面列表

PHP
1234
<?php wp_list_pages(array(
        'title_li' => '',
        'echo' => true,
    )); ?>

5.4 wp_list_categories() – 显示分类目录列表

PHP
1234
   <?php wp_list_categories(array(
        'title_li' => '',
        'echo' => true,
    )); ?>



6.分页导航函数

TEXT
123456789101112131415161718
6.1 paginate_links() - 显示分页导航
    <?php paginate_links(array(
        'prev_text' => __('Previous'),
        'next_text' => __('Next'),
        'echo' => true,
    )); ?>
6.2 previous_posts_link() - 显示上一页链接
    <?php previous_posts_link(__('Previous')); ?>
6.3 next_posts_link() - 显示下一页链接
    <?php next_posts_link(__('Next')); ?>   
6.4 previous_posts_link() - 显示上一页链接
    <?php previous_posts_link(__('Previous')); ?>
6.5 next_posts_link() - 显示下一页链接
    <?php next_posts_link(__('Next')); ?>   
6.6 get_previous_posts_link() - 获取上一页链接
    <?php echo get_previous_posts_link(__('Previous')); ?>
6.7 get_next_posts_link() - 获取下一页链接
    <?php echo get_next_posts_link(__('Next')); ?>   


7.小工具以及侧边栏函数

TEXT
123456
7.1 dynamic_sidebar() - 显示小工具
    <?php dynamic_sidebar('sidebar-1'); ?>
7.2is_active_sidebar() - 检查小工具区域是否激活
    <?php if (is_active_sidebar('sidebar-1')) { ?>
        <?php dynamic_sidebar('sidebar-1'); ?>
    <?php } ?>

8.条件判断函数

TEXT
123456789101112131415161718192021222324252627282930313233343536373839
is_home() - 是否为主页
is_front_page() - 是否为首页
is_single() - 是否为单篇文章
is_page() - 是否为页面
is_category() - 是否为分类存档页
is_tag() - 是否为标签存档页
is_archive() - 是否为存档页
is_search() - 是否为搜索结果页
is_404() - 是否为404页面
has_post_thumbnail() - 是否有特色图像
例子:
    <?php if (is_home()) { ?>
        <p>这是主页</p>
    <?php } elseif (is_front_page()) { ?>
        <p>这是首页</p>
    <?php } elseif (is_single()) { ?>
        <p>这是单篇文章</p>
    <?php } elseif (is_page()) { ?>
        <p>这是页面</p>
    <?php } elseif (is_category()) { ?>
        <p>这是分类存档页</p>           
    <?php } elseif (is_tag()) { ?>
        <p>这是标签存档页</p>
    <?php } elseif (is_archive()) { ?>
        <p>这是存档页</p>
    <?php } elseif (is_search()) { ?>
        <p>这是搜索结果页</p>
    <?php } elseif (is_404()) { ?>
        <p>这是404页面</p>
    <?php } elseif (has_post_thumbnail()) { ?>
        <p>这是有特色图像的文章</p>
    <?php } else { ?>
        <p>其他</p>
    <?php } ?>

简单的判断:
<?php if (is_home()) : ?>
  <p>这是博客首页</p>
<?php endif; ?>

9.日期和时间函数

9.1 the_date() - 显示文章日期
    <?php the_date(); ?>
9.2 the_time() - 显示文章时间
    <?php the_time('F j, Y'); ?>
9.3 get_the_date() - 获取文章日期
    <?php echo get_the_date('F j, Y'); ?>
9.4 get_the_time() - 获取文章时间
    <?php echo get_the_time('F j, Y'); ?>
9.5 get_the_modified_date() - 获取文章修改日期
    <?php echo get_the_modified_date('F j, Y'); ?>
9.6 get_the_modified_time() - 获取文章修改时间
    <?php echo get_the_modified_time('F j, Y'); ?>

10.用户相关函数

10.1 is_user_logged_in() - 检查用户是否登录
    <?php if (is_user_logged_in()) { ?>
        <p>欢迎回来!</p>
    <?php } ?>
10.2 is_user_admin() - 检查用户是否为管理员
    <?php if (is_user_admin()) { ?>
        <p>欢迎管理员!</p>
    <?php } ?>
10.3.wp_register() - 注册链接
    <?php wp_register(); ?>
10.4.wp_loginout() - 登录/注销链接
    <?php wp_loginout(); ?>
10.5.wp_login_form() - 显示登录表单
    <?php wp_login_form(); ?>
10.6.wp_logout_url() - 获取注销链接
    <?php wp_logout_url(); ?>
10.7.the_author() - 显示文章作者
    <?php the_author(); ?>
10.8.the_author_meta() - 显示文章作者信息
    <?php the_author_meta('nickname'); ?>
10.9.the_author_posts_link() - 显示文章作者链接
    <?php the_author_posts_link(); ?>
10.10.the_author_posts_link() - 获取文章作者链接
    <?php echo the_author_posts_link(); ?>
10.11.get_avatar() - 获取用户头像
    <?php echo get_avatar(get_the_author_meta('ID'), 64); ?>

11.评论相关函数

11.1 comments_template() - 加载评论模板
    <?php comments_template(); ?>   
    <?php comments_template( '/comments.php' ); ?>
11.2 comments_number() - 显示评论数量
    <?php comments_number(); ?>
11.3 get_comments_number() - 获取评论数量
    <?php echo get_comments_number(); ?>
11.4 comments_popup_link() - 显示评论弹出链接
    <?php comments_popup_link(); ?>
11.5 get_comments_popup_url() - 获取评论弹出链接
    <?php echo get_comments_popup_url(); ?>
11.6 comment_form() - 显示评论表单
    <?php comment_form(); ?>
11.7 comment_id() - 获取评论ID
    <?php echo comment_id(); ?>

12.其他相关函数

12.1.wp_head() - 在<head>部分输出WordPress需要的内容    
    <?php wp_head(); ?>
12.2.wp_footer() - 在<body>结束标签之前输出WordPress需要内容
    <?php wp_footer(); ?>
12.3.body_class() - 输出body标签的class
    <?php body_class(); ?>
12.4.language_attributes() - 输出语言属性
    <?php language_attributes(); ?>
12.5.do_shortcode() - 执行短代码
    <?php echo do_shortcode('[shortcode]'); ?>


微信二维码
抖音二维码