嗨@daxitude:
让我首先建议您重新考虑。如果每个常见问题解答都没有单独的常见问题解答页面:
您减少了搜索引擎优化的工作量,减少了可能获得的潜在流量,并且
您使某人无法通过电子邮件与朋友共享特定的 FAQ和/或在Facebook,Twitter等上与他们的网络共享。(作为用户,网站开发人员总是很沮丧,他们不允许我使用直接URL到项目,而是强迫我链接到列出所有项目的页面。)
但是,如果您仍然想这样做,那么请执行以下两项操作:
1.)使用'post_type_link'
挂钩
使用该'post_type_link'
钩子来修改URL,如以下示例中所示*(我假设您的自定义帖子类型为'faq'
)。将以下内容添加到主题functions.php
文件中:
add_action('post_type_link','yoursite_post_type_link',10,2);
function yoursite_post_type_link($link,$post) {
$post_type = 'faq';
if ($post->post_type==$post_type) {
$link = get_post_type_archive_link($post_type) ."#{$post->post_name}";
}
return $link;
}
2.) unset($wp_rewrite->extra_permastructs['faq'])
这是一个hack,但它是您执行所需操作所必需的hack。用'init'
钩子钩住unset($wp_rewrite->extra_permastructs['faq'])
。它删除register_post_type()
添加的重写规则。我打来了一个电话,register_post_type()
以便为您和其他人提供完整的示例:
add_action('init','yoursite_init');
function yoursite_init() {
register_post_type('faq',array(
'labels' => array(
'name' => _x('FAQs', 'post type general name'),
'singular_name' => _x('FAQ', 'post type singular name'),
'add_new' => _x('Add New', 'faq'),
'add_new_item' => __('Add New FAQ'),
'edit_item' => __('Edit FAQ'),
'new_item' => __('New FAQ'),
'view_item' => __('View FAQ'),
'search_items' => __('Search FAQs'),
'not_found' => __('No FAQs found'),
'not_found_in_trash' => __('No FAQs found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'FAQs'
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug'=>'faqs'),
'capability_type' => 'post',
'has_archive' => 'faqs',
'hierarchical' => false,
'supports' => array('title','editor','author','thumbnail','excerpt')
));
global $wp_rewrite;
unset($wp_rewrite->extra_permastructs['faq']); // Removed URL rewrite for specific FAQ
$wp_rewrite->flush_rules(); // THIS SHOULD BE DONE IN A PLUGIN ACTIVATION HOOK, NOT HERE!
}
就是这样
当然,上述$wp_rewrite->flush_rules()
在'init'
钩子中的使用确实是一种不好的做法,实际上应该只执行一次,因此我实现了一个完整且自包含的插件,称为FAQ_Post_Type
正确完成。此插件添加带有所需URL规则的FAQ帖子类型,并使用register_activation_hook()
刷新刷新规则;激活显然是需要插件代码而不是可以在主题functions.php
文件中运行的代码的少数事情之一。
这是FAQ_Post_Type
插件的代码;随时根据您的要求进行修改:
<?php
/*
Plugin Name: FAQ Post Type
Description: Answers the question "Custom post type, no need for single view, plus want permalink rewrites that include hash in URI" on WordPress Answers.
Plugin URL: http://wordpress.stackexchange.com/questions/12762/custom-post-type-no-need-for-single-view-plus-want-permalink-rewrites-that-incl
*/
if (!class_exists('FAQ_Post_Type')) {
class FAQ_Post_Type {
static function on_load() {
add_action('post_type_link', array(__CLASS__,'post_type_link'),10,2);
add_action('init', array(__CLASS__,'init'));
}
static function post_type_link($link,$post) {
if ('faq'==$post->post_type) {
$link = get_post_type_archive_link('faq') ."#{$post->post_name}";
}
return $link;
}
static function init() {
register_post_type('faq',array(
'labels' => array(
'name' => _x('FAQs', 'post type general name'),
'singular_name' => _x('FAQ', 'post type singular name'),
'add_new' => _x('Add New', 'faq'),
'add_new_item' => __('Add New FAQ'),
'edit_item' => __('Edit FAQ'),
'new_item' => __('New FAQ'),
'view_item' => __('View FAQ'),
'search_items' => __('Search FAQs'),
'not_found' => __('No FAQs found'),
'not_found_in_trash' => __('No FAQs found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'FAQs'
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug'=>'faqs'),
'capability_type' => 'post',
'has_archive' => 'faqs',
'hierarchical' => false,
'supports' => array('title','editor','author','thumbnail','excerpt'),
));
global $wp_rewrite;
unset($wp_rewrite->extra_permastructs['faq']); // Remove URL rewrite for specific FAQ
}
static function activate() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
FAQ_Post_Type::on_load();
register_activation_hook(__FILE__,array('FAQ_Post_Type','activate'));
}
如果您愿意,还可以'init'
通过检查选项值来将刷新规则保留在内:
// Add this code in your 'init' hook at your register_post_type('faq',...)
if (!get_option('faq_rewrite_rules_updated')) {
global $wp_rewrite;
unset($wp_rewrite->extra_permastructs['faq']); // Remove URL rewrite for specific FAQ
$wp_rewrite->flush_rules();
update_option('faq_rewrite_rules_updated',true);
}
你的选择。
无论如何,请让我知道您是否发现用例无法解决。