我有一個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文件。 –