本文详细介绍WordPress中链接(URL)的各种调用方法,涵盖从基础到高级的应用场景。

一、基础链接调用函数

1. 网站首页链接

// 获取首页URL
echo home_url();                    // 输出: https://wodepress.com
echo home_url('/contact/');         // 输出: https://wodepress.com/contact/

2. 当前页面/文章链接

// 获取当前文章或页面的 permalink
echo get_permalink();               // 必须在Loop内使用
echo get_permalink($post_id);       // 指定文章ID获取链接

3. 页面链接获取

// 通过页面ID获取链接
echo get_page_link(42);

// 通过页面别名(slug)获取
$page = get_page_by_path('about-us');
echo get_permalink($page->ID);

二、主题文件链接

1. 主题目录相关

// 获取主题根目录URL
echo get_template_directory_uri();           // 父主题
echo get_stylesheet_directory_uri();         // 子主题(如果在子主题中使用)

// 常用组合:引用主题内资源
echo get_template_directory_uri() . '/assets/css/style.css';
echo get_stylesheet_directory_uri() . '/images/logo.png';

2. 包含主题文件

// 安全地包含主题文件
get_header();           // 包含 header.php
get_footer();           // 包含 footer.php
get_sidebar();          // 包含 sidebar.php

// 包含自定义模板部分
get_template_part('content', 'single');     // 包含 content-single.php

三、内容链接处理

1. 附件/上传文件链接

// 获取特色图片URL
$thumbnail_url = get_the_post_thumbnail_url($post_id, 'full');

// 获取附件直接链接
echo wp_get_attachment_url($attachment_id);

// 获取指定尺寸的附件链接
echo wp_get_attachment_image_src($attachment_id, 'medium')[0];

2. 分类/标签链接

// 获取分类链接
echo get_category_link($category_id);
echo get_term_link($term, 'category');

// 获取标签链接  
echo get_tag_link($tag_id);

四、导航菜单链接

1. 自定义菜单调用

// 在主题中注册菜单位置
function register_my_menus() {
    register_nav_menus(array(
        'primary' => '主导航菜单',
        'footer'  => '底部菜单'
    ));
}
add_action('init', 'register_my_menus');

2. 输出导航菜单

// 基础调用
wp_nav_menu(array('theme_location' => 'primary'));

// 完整参数示例
wp_nav_menu(array(
    'theme_location'  => 'primary',
    'menu'            => '',
    'container'       => 'nav',
    'container_class' => 'main-navigation',
    'menu_class'      => 'nav-menu',
    'fallback_cb'     => false,
    'depth'           => 2,
    'walker'          => new Custom_Walker_Nav_Menu()  // 自定义Walker
));

五、高级链接操作

1. 分页链接

// 文章分页(使用 `<!--nextpage-->` 时)
wp_link_pages(array(
    'before' => '<div class="page-links">页码: ',
    'after'  => '</div>',
    'pagelink' => '<span>%</span>'
));

// 文章列表分页(存档页)
echo paginate_links(array(
    'total' => $wp_query->max_num_pages,
    'current' => max(1, get_query_var('paged')),
    'prev_text' => '« 上一页',
    'next_text' => '下一页 »'
));

2. 自定义查询链接

// 添加查询参数生成链接
echo add_query_arg('view', 'list', get_permalink());
// 输出: https://wodepress.com/page-name/?view=list

// 多参数
echo add_query_arg(array(
    'view' => 'grid',
    'sort' => 'price'
), get_permalink());

3. 去除URL中的协议头

// 获取无协议URL(避免混合内容警告)
echo esc_url(home_url('/', 'relative'));    // //wodepress.com/

六、REST API获取链接

通过API获取各类链接

// 获取站点信息(包含首页链接)
fetch('/wp-json/')
  .then(res => res.json())
  .then(data => console.log(data.home));

// 获取文章列表(包含permalink)
fetch('/wp-json/wp/v2/posts')
  .then(res => res.json())
  .then(posts => {
    posts.forEach(post => {
      console.log(post.link);        // 文章链接
      console.log(post.guid.rendered); // 固定链接
    });
  });

七、最佳实践建议

始终使用API函数:不要硬编码URL,使用home_url(),site_url()等函数

转义输出:使用esc_url()函数包裹链接输出,防止XSS攻击

使用相对协议://wodepress.com可自动适配HTTP/HTTPS

缓存菜单:大型站点的复杂菜单建议缓存或使用Transients

这些方法覆盖了WordPress开发中99%的链接调用场景,wordpress模板开发过程中最常用到的,掌握了这些,基础的wordpress模板开发足够用了。