1
我一直工作在這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);