需要有关WP Job Manager代码段的帮助

时间:2017-04-21 07:25:25

标签: wordpress

我想知道您是否能够帮助我完成以下WordPress定制。我们正在使用WP作业管理器插件(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' );

1 个答案:

答案 0 :(得分:-1)

好吧,我曾经陷入某种类型的错误。但就我而言,我创造了自己的一切。所以我相应地更改了模板。

但是,在您的情况下,我认为它与URL重写有关。请查看以下链接。我希望那些人会有所帮助。

http://www.wpbeginner.com/plugins/how-to-change-custom-post-type-permalinks-in-wordpress/

https://codex.wordpress.org/Rewrite_API/add_rewrite_rule https://paulund.co.uk/rewrite-urls-wordpress