【问题标题】:Remove park of permalink from WP Job Manager listing从 WP Job Manager 列表中删除永久链接公园
【发布时间】:2020-05-08 18:46:26
【问题描述】:

我有一个工作网站,将工作放在以下 URL 上:

https://www.jarsolutions.co.uk/job/housing-solutions-officer-2/

这是来自插件 WP Job Manager,我试图简单地删除 /job/ 部分,所以它看起来像这样:

https://www.jarsolutions.co.uk/housing-solutions-officer-2/

问题在于,如果我将其从永久链接设置中删除并留空,它会使用默认的 /jobs/ 填充它。

我发现这个功能可以让我将其更改为我喜欢的任何内容,但我找不到如何调整它以完全摆脱它或将其更改为无...

function change_job_listing_slug( $args ) {
  $args['rewrite']['slug'] = _x( 'careers', 'Job permalink - resave permalinks after changing this', 'job_manager' );
  return $args;
}

add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );

这段代码如果来自他们自己的网站:https://wpjobmanager.com/document/tutorial-changing-the-job-slugpermalink/,它还提供代码 sn-ps,将内容添加到 url...但同样它没有提供任何关于从他的 URL 中删除内容的信息。

如果有人能指出我正确的方向,那真的很有帮助。

谢谢:)

【问题讨论】:

    标签: php wordpress function plugins


    【解决方案1】:

    这是您可以从术语 URL 中删除自定义分类 slug(«job»)的方法:

    这里是代码的主要部分,你可以将它插入你当前的主题functions.php,只是不要忘记将每个函数中的分类名称/slugs更改为你自己的值。

    add_filter('request', 'rudr_change_term_request', 1, 1 );
    
    function rudr_change_term_request($query){
    
        $tax_name = 'product_cat'; // specify you taxonomy name here, it can be also 'category' or 'post_tag'
    
        // Request for child terms differs, we should make an additional check
        if( $query['attachment'] ) :
            $include_children = true;
            $name = $query['attachment'];
        else:
            $include_children = false;
            $name = $query['name'];
        endif;
    
    
        $term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists
    
        if (isset($name) && $term && !is_wp_error($term)): // check it here
    
            if( $include_children ) {
                unset($query['attachment']);
                $parent = $term->parent;
                while( $parent ) {
                    $parent_term = get_term( $parent, $tax_name);
                    $name = $parent_term->slug . '/' . $name;
                    $parent = $parent_term->parent;
                }
            } else {
                unset($query['name']);
            }
    
            switch( $tax_name ):
                case 'category':{
                    $query['category_name'] = $name; // for categories
                    break;
                }
                case 'post_tag':{
                    $query['tag'] = $name; // for post tags
                    break;
                }
                default:{
                    $query[$tax_name] = $name; // for another taxonomies
                    break;
                }
            endswitch;
    
        endif;
    
        return $query;
    
    }
    
    
    add_filter( 'term_link', 'rudr_term_permalink', 10, 3 );
    
    function rudr_term_permalink( $url, $term, $taxonomy ){
    
        $taxonomy_name = 'product_cat'; // your taxonomy name here
        $taxonomy_slug = 'product_cat'; // the taxonomy slug can be different with the taxonomy name (like 'post_tag' and 'tag' )
    
        // exit the function if taxonomy slug is not in URL
        if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;
    
        $url = str_replace('/' . $taxonomy_slug, '', $url);
    
        return $url;
    }
    

    不要忘记旧网址的 301 重定向,它是您网站 SEO 所必需的。

    add_action('template_redirect', 'rudr_old_term_redirect');
    
    function rudr_old_term_redirect() {
    
        $taxonomy_name = 'product_cat';
        $taxonomy_slug = 'product_cat';
    
        // exit the redirect function if taxonomy slug is not in URL
        if( strpos( $_SERVER['REQUEST_URI'], $taxonomy_slug ) === FALSE)
            return;
    
        if( ( is_category() && $taxonomy_name=='category' ) || ( is_tag() && $taxonomy_name=='post_tag' ) || is_tax( $taxonomy_name ) ) :
    
                wp_redirect( site_url( str_replace($taxonomy_slug, '', $_SERVER['REQUEST_URI']) ), 301 );
            exit();
    
        endif;
    
    }
    

    代码已使用不同的分层和非分层分类法进行了测试,并且非常适合此永久链接设置。

    【讨论】:

    • 非常感谢这个 Navnish!为迟到的回复道歉......分类法被称为:job_listing_category - 我试图用job_listing_category替换product_cat,但还没有 - 我确定它不是那么简单,我错过了一些东西!所以我需要为我拥有的每个类别添加 slug?抱歉没看懂。
    • @ShaunTaylor 请分享您在 functions.php 中编写的代码,用于进行分类。所以可以指导你准确的话
    • 嗨 :) 工作部分实际上是一个插件:wpjobmanager.com 但查看插件文件,自定义帖子类型是:job_listing,分类为:job_listing_type - job_listing_category - 似乎.. ..
    • 工作类别的 slug 是:教育 - 入门级 - 环境健康 - 高管搜索 - 财务会计 - 研究生 - 住房 - 人力资源 - 办公室支持 - 采购 - 供应 -连锁-物业-管理-建设-支持-住房-技术-这是你的意思吗?再次感谢您的帮助:)
    【解决方案2】:

    要从 URL 中删除“job/”,您必须按照以下步骤操作

    第 1 步:将永久链接设置更改为“帖子名称” 第 2 步:将职位创建为“帖子”

    如果您不想在 Post 中创建,则无法从 URL 中删除“job/”,除非您在 URL 中添加“job-”作为前缀(https://www.jarsolutions.co.uk/job-housing-solutions-officer-2/

    【讨论】:

      【解决方案3】:

      从您引用的插件作者的网站上,我注意到它说:

      注意:由于 WP Job Manager 版本 1.27.0,作业永久链接可以是 在“可选”部分下的“设置”->“永久链接”中进行了更改。

      对于以前版本的 WP Job Manager,请参阅下面的指南。谢谢!

      其中列出了七个不同的代码示例。

      在我自己玩这个的过程中,我无法让任何代码示例工作(它们以奇怪的方式中断)。

      所以我猜如果您运行的版本超过 1.27.0,那么这些代码示例将不再有效。

      我尝试使用一些重写规则来从 url 中删除 /job,但问题是,插件的所有内容都在寻找一个关键字(默认值:job),它将通过它来搜索内容。

      如果它被删除,WordPress 将期望该 url 必须是一个页面。可以将其删除,但需要付出很大的努力。

      最好的办法是向 WP Job Manager 提交支持票,并希望他们将其放在他们的路线图上,或者付钱给像我这样的插件开发人员,让他们编写必要的代码来让你这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-15
        • 2011-05-22
        • 2013-04-19
        • 2016-11-03
        • 2020-01-20
        • 2016-08-01
        • 1970-01-01
        • 2012-07-01
        相关资源
        最近更新 更多