添加重写规则自定义帖子类型

时间:2013-12-08 20:51:58

标签: wordpress url-rewriting

我已经为此工作了4天了,无法弄清楚我错过了什么。我已经搜索并尝试了所有用于重写wp的内容。

它有效,但不能保存。因此,如果我快速编辑并保存,请单击查看它是否有效。如果我刷新并将鼠标悬停在视图%%property_type%缺失并且不起作用。

任何想法我缺少什么?

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite'        => array(
    'slug'       => str_replace( basename( home_url() ), '', 'investment-property' ) . '/%property_type%',
    'with_front' => false
),
'capability_type' => 'page',
'has_archive'   => true,
'hierarchical'  => true,
'comment_status'=> 'closed',
'exclude_from_search' =>false,
'supports' => array('title', 'editor', 'thumbnail'),

);

register_post_type( 'property', $args );

我也有这个...

function my_flush_rules() {
$rules = get_option( 'rewrite_rules' );

if ( ! isset( $rules['(investment-property/%property_type%/)/(.+)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

}

......还有这个

function icn_property_type_link( $permalink, $post, $leavename ) {
global $wp_query, $wpsc_page_titles, $wp_rewrite, $wp_current_filter;
$rewritecode = array(
    '%property_type%',
    $leavename ? '' : '%postname%',
);
if ( is_object( $post ) ) {
    // In wordpress 2.9 we got a post object
    $post_id = $post->ID;
} else {
    // In wordpress 3.0 we get a post ID
    $post_id = $post;
    $post = get_post( $post_id );
}

// Only applies to ICN properties, don't stop on permalinks of other CPTs
if ($post->post_type != 'property')
    return $permalink;

$permalink_structure = get_option( 'permalink_structure' );

// This may become customize-able later
$our_permalink_structure = str_replace( basename( home_url() ), '', 'investment-property' ) . "/%property_type%/%postname%/";
// Mostly the same conditions used for posts, but restricted to items with a post type of "wpsc-product "

if ( '' != $permalink_structure && !in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
    $property_categories = get_the_terms($post_id, 'property_type' );


    $property_category_slugs = array( );
    foreach ( $property_categories as $property_category ) {
            if($property_category->slug == 'featured'){continue;}
        $property_category_slugs[] = $property_category->slug;
    }
    // If the property is associated with multiple categories, determine which one to pick

    if ( count( $property_categories ) == 0 || $property_categories == '') {
        $category_slug = 'real-estate-for-sale';
    } elseif ( count( $property_categories ) > 1 ) {
        if ( (isset( $wp_query->query_vars['property'] ) && $wp_query->query_vars['property'] != null) && in_array( $wp_query->query_vars['property'], $property_category_slugs ) ) {
            $property_category = $wp_query->query_vars['property'];
        } else {
            $link = $property_categories[0]->slug;
            if ( ! in_array( 'wp_head', $wp_current_filter) && isset( $wp_query->query_vars['property_type'] ) ) {
                $current_cat = $wp_query->query_vars['property_type'];
                if ( in_array( $current_cat, $property_category_slugs ) ){
                    $link = $current_cat;
                }
            }

            $property_category = $link;

        }
        $category_slug = $property_category;


    } else {
        // If the property is associated with only one category, we only have one choice
        if ( !isset( $property_categories[0] ) )
            $property_categories[0] = '';

        $property_category = $property_categories[0];

        if ( !is_object( $property_category ) )
            $property_category = new stdClass();

        if ( !isset( $property_category->slug ) )
            $property_category->slug = null;

        $category_slug = $property_category->slug;
    }

    $post_name = $post->post_name;

    if ( get_option( 'property_type_hierarchical_url', 0 ) ) {
        $selected_term = get_term_by( 'slug', $category_slug, 'property_type' );
        if ( is_object( $selected_term ) ) {
            $term_chain = array( $selected_term->slug );
            while ( $selected_term->parent ) {
                $selected_term = get_term( $selected_term->parent, 'property_type' );
                array_unshift( $term_chain, $selected_term->slug );
            }
            $category_slug = implode( '/', $term_chain );
        }
    }

    if( isset( $category_slug ) && empty( $category_slug ) )
        $category_slug = 'property_type';

    $category_slug = apply_filters( 'property_type_permalink_cat_slug', $category_slug, $post_id );

    $rewritereplace = array(
        $category_slug,
        $post_name
    );

    $permalink = str_replace( $rewritecode, $rewritereplace, $our_permalink_structure );
    $permalink = user_trailingslashit( $permalink, 'single' );

    $permalink = home_url( $permalink );
}
return apply_filters( 'property_permalink', $permalink, $post_id );
}
add_filter( 'post_type_link', 'icn_property_type_link', 1, 3 );

1 个答案:

答案 0 :(得分:0)

根据我的经验,关于帖子类型的文档不足。

在大多数情况下,您可以通过安装和使用Types插件,或者通过挖掘代码来借用他们的一些方法来使用Types for WordPress插件。重写帖子类型的URL在插件的开箱即用功能范围内。

我不喜欢他们的广告所以我只是将类添加到wp-admin来隐藏它们。 ;)

{{3}}