php 在帖子和页面的预览链接中添加页面slug的功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 在帖子和页面的预览链接中添加页面slug的功能相关的知识,希望对你有一定的参考价值。

</php
/** function to add page slug in preview link of post and page 
 * 
 * @param type $preview_link
 * @param type $post
 * @return type string 
 */
function custom_preview_post_link($preview_link, $post) {
    /*
      Firstly Check post status, if its draft/pending/auto-draft post then  checks Db for post name. If post name is empty then sanititize page title. If post name and post title both are empty then use post id
      Add resultant of above as a Query param 'url' to preview link of post.
     */
    $pname = $post->post_name;
    if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
        $pname = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
    }

    $query_args = array();
    $query_args['url'] = $pname;
    $preview_link = add_query_arg($query_args, $preview_link);
    return $preview_link;
}

add_filter('preview_post_link', 'custom_preview_post_link', 10, 2);
add_filter('preview_page_link', 'custom_preview_post_link', 10, 2);

以上是关于php 在帖子和页面的预览链接中添加页面slug的功能的主要内容,如果未能解决你的问题,请参考以下文章