我有一个URL是这样的: example.com/movies/157336/InterstellarWordPress的 - 信息发布的网址与自定义字段值
参考: example.com/movies/%movie_id%/%movie_name %
请注意,movie_id是一个自定义字段,而不是实际的帖子ID。 movie_name是帖子标题slu,,但只是出于搜索引擎优化的原因。
我需要WordPress的加载基于自定义字段中的页面的URL movie_id发现,而不是使用页面名称,如果没有找到movie_id,抛出常规的404错误。
我遇到的主要问题是,我似乎无法得到WordPress的加载基于URL中的自定义字段movie_id一个页面,它总是使用MOVIE_NAME作为参考点。
我如何知道?那么正确的URL将是example.com/movies/157336/Interstellar
,如果我将标题更改为example.com/movies/157336/Intersterlarxyz
,那么WordPress会提供404错误。 如果我更改了ID,但保留了正确的电影名称,如下所示:example.com/movies/123/Interstellar
然后WordPress仍会加载正确的页面。
基于这种行为,它可以安全地说WordPress的加载基于来自URL,而不是电影ID页面塞的页面,而这正是我需要修复。
这是到目前为止我的代码:
电影plugin.php
// Register Custom Post Type "movies"
function register_moviedb_post_type() {
register_post_type('movies',
array(
'labels' => array(
'name' => __('Movies'),
'singular_name' => __('Movies')
),
'taxonomies' => array('category'),
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'movies','with_front' => FALSE),
'supports' => array('title', 'editor', 'custom-fields','comments','page-attributes','trackbacks','revisions', 'thumbnail')
)
);
flush_rewrite_rules();
}
add_action('init', 'register_moviedb_post_type');
// Add custom rewrite tag 'movie_id'
function custom_rewrite_tag() {
add_rewrite_tag('%movie_id%', '([^/]+)', 'movie_id=');
}
add_action('init', 'custom_rewrite_tag');
// Add rewrite rule
function custom_rewrite_basic() {
add_rewrite_rule(
'^movies/([^/]*)/([^/]*)/?',
//'index.php?post_type=movies&movie_id=$matches[1]',
'index.php?post_type=movies&movie_id=$matches[1]&name=$matches[2]',
'top'
);
}
add_action('init', 'custom_rewrite_basic');
// Query var 'movie_id'
function add_query_vars_filter($vars){
$vars[] = "movie_id";
return $vars;
}
add_filter('query_vars', 'add_query_vars_filter');
// Custom Page Template
function custom_movie_page_template() {
if (is_singular('movies')) {
add_filter('template_include', function() {
return plugin_dir_path(__FILE__) . '/movies.php';
});
}
}
add_action('template_redirect', 'custom_movie_page_template');
// Create custom post type link for movies
function movie_post_type_link($link, $post = 0){
if ($post->post_type == 'movies'){
$id = $post->ID;
$post = &get_post($id);
$movie_id = get_post_meta($post->ID,'movie_id', true);
empty ($post->slug)
and $post->slug = sanitize_title_with_dashes($post->post_title);
return home_url(
user_trailingslashit("movies/$movie_id/$post->slug")
);
} else {
return $link;
}
}
add_filter('post_type_link', movie_post_type_link, 1, 2);
如果我在add_rewrite_rule的URL删除MOVIE_NAME,WordPress的只是加载该职位类型的档案页面。 'index.php?post_type=movies&movie_id=$matches[1]',
如果我在URL重写中使用页面名称,它总是根据名称加载页面,而不是根据movie_id加载页面。 'index.php?post_type=movies&movie_id=$matches[1]&name=$matches[2]',
我已经一个movie.php模板文件,该文件类似于你的,但是这是没有问题的。问题是,WordPress总是使用URL中的页面名称来确定要加载哪个页面,而不是使用movie_id。 movie.php文件中的自定义wp_query不会改变该行为。网站标题和其他相关内容仍将是网页中基于网页名称的内容。如果我从URL中删除页面名称,那么wordpress只会加载archive.php文件。 –