【发布时间】:2017-04-21 07:25:25
【问题描述】:
我想知道您是否能够帮助我进行以下 WordPress 自定义。我们正在使用 WP Job Manager 插件 (https://wpjobmanager.com/),我需要一些帮助来进行 slug/permalink 编辑。
文档中有一篇文章解释了以下内容:在当前情况下,链接生成如下:domain.com/job/job-name。但是,我需要以下结构:domain.com/job-category/job-name。
请查收:https://wpjobmanager.com/document/tutorial-changing-the-job-slugpermalink/
文章对此进行了解释。请检查以下代码:示例:将类别添加到基本 URL。当我在以下代码中删除“工作”时,工作列表工作正常,但我网站的其余部分返回 404 错误(也在保存永久链接后)。
$args['rewrite']['slug'] = 'job/%category%';
到
$args['rewrite']['slug'] = '%category%';
完整代码:
function job_listing_post_type_link( $permalink, $post ) {
// Abort if post is not a job
if ( $post->post_type !== 'job_listing' )
return $permalink;
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
return $permalink;
// Get the custom taxonomy terms in use by this post
$terms = wp_get_post_terms( $post->ID, 'job_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) );
if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$job_listing_category = _x( 'uncat', 'slug' );
} else {
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift( $terms );
$job_listing_category = $first_term->slug;
}
$find = array(
'%category%'
);
$replace = array(
$job_listing_category
);
$replace = array_map( 'sanitize_title', $replace );
$permalink = str_replace( $find, $replace, $permalink );
return $permalink;
}
add_filter( 'post_type_link', 'job_listing_post_type_link', 10, 2 );
function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = 'job/%category%';
return $args;
}
add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );
【问题讨论】:
-
Stack Overflow 上的志愿者对他们的帮助不收取任何费用,因此我已经编辑了您的付款提议。只要问题有足够的细节,而且不是太宽泛的问题,您应该在这里获得帮助。
-
谢谢,我不知道!
标签: wordpress