【问题标题】:Need assistance with a snippet for WP Job Manager需要 WP Job Manager 片段的帮助
【发布时间】: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


【解决方案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

【讨论】:

  • 感谢您的建议,但我无法编辑“工作”信息。请查看此截图:imgur.com/4uU7cj3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 2016-04-01
  • 2013-12-10
  • 2019-12-26
相关资源
最近更新 更多