你好@daxitude:
首先我建议你重新考虑一下。如果您没有针对每个常见问题的单独常见问题页面:
减少曲面是为了搜索引擎优化,减少可能获得的潜在流量,以及
你让某人无法通过电子邮件与朋友分享特定的FAQ,也无法在Facebook、Twitter等网站上与朋友分享FAQ(作为一名用户,我总是对网站开发人员感到失望,他们不允许我拥有项目的直接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\'])
这是一种黑客行为,但这是做你想做的事情所必需的黑客行为。使用
\'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\'
挂钩是really bad practice 而且应该只执行一次,所以我实现了一个完整且自包含的插件,名为FAQ_Post_Type
把它做好。此插件添加一个FAQ帖子类型,其中包含您想要的URL规则,并使用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);
}
您的选择。
无论如何,如果您发现有一些用例无法解决,请告诉我。