这个问题你好!
目前,我的永久链接设置设置为;职位名称这样,用于文章的默认wordpress帖子将有一个漂亮的URL。
但是,我有这个工作岗位(自定义岗位类型为“thjb\\u job”)permalink结构需要具备以下类型:
用于查看单个职务
1.)(rootURI) / job-post / %job_id%
以及基于雇主的列表(雇主作为一种分类法)
2.)(rootURI) / %taxonomy% / job-post / %job_id%
我优先考虑的是第1项,因为我还没有添加雇主分类法。
我已经使用了wordpress方法,如eadd\\u rewrite\\u tag()和add\\u permastruct(),但它们似乎不能正常工作。
我也尝试过使用add\\u filter(\'post\\u type\\u link\')wordpress挂钩,但它似乎只是操纵permalink字符串记录,因为当我尝试查看帖子时,它返回404(通过在编辑自定义帖子thjb\\u作业时单击permalink)。
此外,我已经阅读了许多涉及修改的答案。htaccess。我希望我不需要碰它。htaccess只是为了实现这个定制的permalink结构。
Thanks in advance for your help on how to achieve the permalink structures above.
<这些代码是我使用上述方法的方法
/**
* AddCustomPostJob = Add Custom Post labeled as Job
* : this class registers a new custom post type called thjb_job (Job)
*/
class AddCustomPostJob {
/**
* init() = Initializer Method
* : this method is the class engine starter
*
*/
public static function init() {
// Register the custom post type
add_action( \'init\', array(get_called_class(), \'registerPostType\') );
// Register custom rewrite rules for this custom post type
add_action( \'init\', array(get_called_class(), \'rewriteRules\') );
// Register custom url tag translations
add_filter( \'post_type_link\', array(get_called_class(), \'translateTags\'), 10, 2 );
}
/**
* registerPostType = Register Post Type callback
* : this method defines the properties for the custom post type
*/
public static function registerPostType() {
// Define custom type child options
// Labels
$labels = array(
\'name\' => __( \'Jobs\' ),
\'singular_name\' => __( \'Job\' )
);
// Default fields
$supports = array( \'title\', \'editor\' );
// Turn off wordpress permalink rewrite
$rewrite = false;
// Define the options for the custom post type
$opts = array(
\'labels\' => $labels,
\'public\' => true,
\'has_archive\' => true,
\'show_ui\' => true,
\'show_in_menu\' => false,
\'show_in_nav_menus\' => false,
\'show_in_admin_bar\' => true,
\'supports\' => $supports,
\'rewrite\' => $rewrite,
);
// Register the post type with key => thjb_job
register_post_type( \'thjb_job\', $opts);
} // registerPostType()
/**
* rewriteRules() = Custom rewrite rules method
* : this method creates a custom rewrite rules exclusively for thjb_job post type
*
*/
public static function rewriteRules() {
// Define the custom permalink structure
$structure = \'/job-post/%job_id%\';
// Map the equivalent query var
add_rewrite_tag("%job_id%", \'([^/]+)\', "p=");
// Add the custom permalink structure to wordpess
add_permastruct(\'thjb_job\', $structure, false);
}
/**
* translateTags() = Translate url tags method
* : this method translates tags (%tags%) to its original value
*
* @param $url
* : this param is the current url to be modified
*
* @param $post
* : this param is the current post being processed
*
* @return modified $url
*
*/
public static function translateTags($url, $post) {
// Apply only for thjb_job post type
if ( $post->post_type == \'thjb_job\') {
// If %job_id% exists, then insert the original value
if ( strpos($url, \'%job_id%\') ) {
return str_replace(\'%job_id%\', $post->ID, $url);
}
}
return $url;
}
} // AddCustomPostJob class
AddCustomPostJob::init();
最合适的回答,由SO网友:tanhernandez 整理而成
答案首先,这个链接对我帮助很大
https://premium.wpmudev.org/blog/building-customized-urls-wordpress
事实证明,我不需要使用add\\u rewrite\\u标记(),而是使用add\\u rewrite\\u rule()来实现我的目标。
I wondered why --
%job_id% (甚至
%post_id%, 都试过了)
在我的permalink结构中无法识别。我早就料到了%job_id% 将替换为我的custom\\u post的id,因为它是我用于add\\u rewrite\\u tag()的标记,指向“p”query var。
仅限%postname% 正在正确识别。
Then, I removed the callback for add_filter(\'post_type_link\')
Result : 当我保存新帖子时,permalink变为“job post/%job\\u id%”,%job\\u id%不是一个变量,而是一个硬编码字符串。
Then I turned off everything
Result : 我发现原来的查询URL是:
www.mysite.com/?thjb_job=THE_NAME_OF_THE_JOB_POST_ITEM
我认为这就是为什么无法识别“p”查询变量的原因,因为只提供了postname。
So I turned everything back on again, and modified the rewriteRules() method
以下是与上述代码相比的修改:
/**
* rewriteRules() = Custom rewrite rules method
* : this method creates a custom rewrite rules exclusively for thjb_job post type
*
*/
public static function rewriteRules() {
// Define the custom permalink structure
$structure = \'/job-post/%job_id%\';
// Add the custom permalink structure to wordpess
add_permastruct(\'thjb_job\', $structure, false);
// Define the original query url that should be executed to grab the post
$query = \'index.php?post_type=thjb_job&p=$matches[1]\';
// Apply the query to be called when user bumps into this url pattern
add_rewrite_rule(\'^job-post/([^/]+)\', $query, \'top\');
}