WordPress教程

WordPress 获取指定ID或当前文章别名的方法代码

阿里云

WordPress 可以通过 the_title() 函数获取文章标题,但是却找不到能直接获取文章别名的 wordpress 函数,所以要想调用文章别名就只能编写调用函数了,代码比较简单。

把下面的代码放在主题的 functions.php 文件:

也想出现在这里?联系我们
创客主机
  1. function the_slug($postid = null) {
  2.     if($postid == 'null') $postid = $post->ID;
  3.     $postData = get_post($postid, ARRAY_A);
  4.     $post_slug = $postData['post_name'];
  5.     return $post_slug; 
  6. }

在 single.php 调用当前文章的别名:

  1. <?php echo the_slug();?>

调用指定文章 ID 为 20 的文章别名

  1. <?php echo the_slug(20);?>

上面的代码也适用于获取单篇页面的别名。

WordPress 获取指定 ID 或当前文章别名的方法代码

已有 152 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!