php 从自定义帖子类型中删除Slug

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 从自定义帖子类型中删除Slug相关的知识,希望对你有一定的参考价值。

<?php

add_filter('get_sample_permalink_html', 'my_hide_permalinks', 10, 5);
function my_hide_permalinks($return, $post_id, $new_title, $new_slug, $post)
{
	if($post->post_type == DIGI_POST_TYPE) {
		return '';
	}
	return $return;
}


/*
 * Add Style for Table Custom Post Type
 */
add_action( 'admin_head-edit.php', 'custom_post_type_add_style_table' );
function custom_post_type_add_style_table()
{
	global $post_type;
	if($post_type ==DIGI_POST_TYPE) {

		//Add Jquery Remove Password Field
		echo '
             <script type="text/javascript">         
                jQuery(document).ready( function($) {
                    $(\'span:contains("'.__('Password').'")\').each(function (i) { $(this).parent().parent().remove();});
                    $(\'span:contains("'.__('Slug').'")\').each(function (i) { $(this).parent().parent().remove();});
                });
            </script>
            ';

	}
}

function append_query_string( $url, $post, $leavename=false ) {
	if ( $post->post_type == DIGI_POST_TYPE ) {
		$url = get_page_link(63).'id/'.$post->ID;
	}
	return $url;
}
add_filter( 'post_type_link', 'append_query_string', 10, 3 );

add_action('init', 'wpse42279_add_endpoints');
function wpse42279_add_endpoints()
{
	add_rewrite_endpoint('id', EP_PAGES );
}

//go to 404 if not id data sheet
function my_page_template_redirect()
{
	if( is_page( 63 ) )
	{
		global $wp_query;
		$id =  get_query_var("id");

		//not
		if(!$id) {
			wp_redirect( home_url() );
			exit;
		}

		//check post id exist
		$get_post = get_post($id);
		if( is_null( $get_post ) ){
			wp_redirect( home_url() );
			exit;
		} else {
			//check post type
			if ( $get_post->post_type == DIGI_POST_TYPE ) {
				//Check Published Post
				if($get_post->post_status =="publish") {
					/*
				 * every thing is ok
				 */
				} else {
					wp_redirect( home_url() );
					exit;
				}
			} else {
				wp_redirect( home_url() );
				exit;
			}
		}
	}

}
add_action( 'template_redirect', 'my_page_template_redirect' );


add_action('loop_start','condition_filter_title');
function condition_filter_title($query){
	global $wp_query;
	if($query === $wp_query){
		add_filter('the_title', 'filter_new_title', 10, 2);
	}else{
		remove_filter('the_title', 'filter_new_title', 10, 2);
	}
}

function filter_new_title($title, $id) {
	if(is_page(63)) {
		$pid = get_query_var("id");
		$title = get_the_title($pid);
	}
	return $title;
}


//https://wordpress.stackexchange.com/questions/9185/not-able-to-change-wp-title-using-add-filter
//https://make.wordpress.org/core/2015/10/20/document-title-in-4-4/
function custom_title($title) {
	if(is_page(63)) {
		$id = get_query_var("id");
		$title = get_the_title($id);
	}
	return $title;
}
add_filter( 'pre_get_document_title', 'custom_title' );

以上是关于php 从自定义帖子类型中删除Slug的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress 从自定义帖子中删除永久链接但保留存档

Wordpress ACF 字段如何从自定义帖子类型中获取选项

Wordpress // 删除 URL 的帖子类型 slug

自定义帖子类型和分类法使用相同的 slug 和重写

php 从发布的帖子永久链接 - Wordpress中删除slug

WordPress - 无法从自定义帖子类型中的元框获取价值