wordpress链接的调用方法

本文详细介绍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' => '

    ',
    'pagelink' => '%'
    ));

    // 文章列表分页(存档页)
    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模板开发足够用了。

原文

https://www.jianzhanpress.com/?p=9273

相关推荐
李纲明1 小时前
WordPress 定时发布不跑、备份邮件延误:先核 WP-Cron 触发与回环,再改系统 Cron
运维·wordpress·定时任务·devdiy·wp-cron
李纲明10 小时前
WordPress 媒体库上传图片失败:先核 PHP 体积与目录权限,再查安全插件与对象存储
php·上传·wordpress·媒体库·devdiy
李纲明2 天前
WordPress 报 Allowed memory size exhausted:先抓堆栈找吃内存的插件,再改 memory_limit
性能优化·php·内存·插件·wordpress·devdiy
李纲明3 天前
WordPress 前台改完还是旧页:浏览器、CDN、页面缓存、对象缓存怎么分层清
缓存·cdn·wordpress·devdiy
李纲明5 天前
WordPress 结账页一直转圈不跳支付:AJAX、缓存排除、支付回调和 REST 怎么逐层核
运维·ajax·缓存·wordpress·woocommerce·devdiy·结账
李纲明6 天前
WordPress 安装或更新插件提示「无法创建目录」:权限、磁盘空间与 FS_METHOD 排查
wordpress·插件安装·devdiy·无法创建目录·fs_method
李纲明6 天前
WordPress 后台登录马上被踢回登录页:Cookie 域、HTTPS、盐值和页面缓存怎么查
运维·缓存·https·php·wordpress·cookie
用户60968624266086 天前
页面构建器做的页面翻译:短代码、区块、动态内容各自会踩什么坑
php·wordpress
李纲明7 天前
WordPress 挂上 Cloudflare 后询盘和后台异常:缓存规则、SSL 模式和真实 IP 怎么核
运维·缓存·https·cdn·wordpress·cloudflare
DEVDIY7 天前
WordPress TTFB 高怎么排查:7 步定位服务端瓶颈
wordpress