WP作业管理器自定义固定链接返回404个错误

时间:2016-04-04 作者:Edgar Quintero

我正在构建一个使用WP Job Manager的网站。我对设置它很感兴趣,以便URL显示为:

/category/region/name/
我发现this link that explains how to do just that 并添加了以下代码:

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
    $categories = wp_get_post_terms( $post->ID, \'job_listing_category\', array( \'orderby\' => \'parent\', \'order\' => \'ASC\' ) );
    $regions    = wp_get_post_terms( $post->ID, \'job_listing_region\', array( \'orderby\' => \'parent\', \'order\' => \'ASC\' ) );

    if ( empty( $categories ) ) {
        // If no terms are assigned to this post, use a string instead (can\'t leave the placeholder there)
        $job_listing_category = _x( \'uncategorized\', \'slug\' );
    } else {
        // Replace the placeholder rewrite tag with the first term\'s slug
        $first_term = array_shift( $categories );
        $job_listing_category = $first_term->slug;
    }

    if ( empty( $regions ) ) {
        // If no terms are assigned to this post, use a string instead (can\'t leave the placeholder there)
        $job_listing_region = _x( \'anywhere\', \'slug\' );
    } else {
        // Replace the placeholder rewrite tag with the first term\'s slug
        $first_term = array_shift( $regions );
        $job_listing_region = $first_term->slug;
    }

    $find = array(
        \'%category%\',
        \'%region%\'
    );

    $replace = array(
        $job_listing_category,
        $job_listing_region
    );

    $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%/%region%\';
  return $args;
}
add_filter( \'register_post_type_job_listing\', \'change_job_listing_slug\' );

function add_region_endpoint_tag() {
    add_rewrite_tag( \'%region%\', \'([^/]*)\' );
}
add_action( \'init\', \'add_region_endpoint_tag\' );
这也正是我所需要的,URL显示为/category/region/name/——然而我的画廊图片和博客帖子都显示为404。

即使包括:

// Abort if post is not a job
if ( $post->post_type !== \'job_listing\' ) {
    return $permalink;
}
有谁能帮我一下,我该如何告诉WP Job Manager在修改永久链接时省略博客帖子,我相信这是附件帖子类型?

1 个回复
最合适的回答,由SO网友:Charly 整理而成

我的网站上也有同样的问题http://loctim.com 因为我想要这样的鼻涕虫%category%/%state%/%city%我换了404页functions.php 而今天,这是一个疯狂的画廊。所以我工作了一整晚,发现我忘记了这部分代码:

function add_category_endpoint_tag() {
    add_rewrite_tag( \'%category%\', \'([^/]*)\' );
    }
add_action( \'init\', \'add_category_endpoint_tag\' );
请尝试将前面的代码放在(您的)此代码之前:

function add_region_endpoint_tag() {
    add_rewrite_tag( \'%region%\', \'([^/]*)\' );
        }
add_action( \'init\', \'add_region_endpoint_tag\' );
我希望这对你有帮助。如果您感兴趣,我有更多关于这个slug的代码,例如如何自动替换%state%%country% 如果%state% = %city% (有些城市的情况与美国或葡萄牙相同)

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?