WordPress如何获取当前文章别名或指定文章ID别名

追格官方小助手/ 2022年03月19日/ WordPress/ 浏览 1449

追格小程序小编前段时间发过一篇《WordPress如何获取当前tag页面的标签ID和别名》的教程,今天我们来说说WordPress如何获取当前文章的别名。


WordPress本身是可以通过WordPress函数 the_title() 获取文章标题,但没有文章别名的函数,我们可以试试下面方法,当前WordPress主题的functions.php文件中添加以下代码:


function the_slug($postid = null) {
    if($postid == 'null') $postid = $post->ID;
    $postData = get_post($postid, ARRAY_A);
    $post_slug = $postData['post_name'];
    return $post_slug; 
}


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


<?php echo the_slug();?>


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


<?php echo the_slug(888);?>


发表评论

暂无评论,抢个沙发...

客服 工单