我正在尝试使用自定义分类法和类别创建自定义帖子。结构如下:
Product:
function create_alco_custom_post() {
$review_slug = \'reviews\';
$labels = array(
\'name\' => __( \'Recenzje\' ),
\'singular_name\' => __( \'Recenzja\' ),
\'add_new\' => __( \'Nowa recenzja\' ),
\'add_new_item\' => __( \'Dodaj nową recenzje\' ),
\'edit_item\' => __( \'Edytuj recenzje\' ),
\'new_item\' => __( \'Nowa recenzja\' ),
\'view_item\' => __( \'Zobacz recenzje\' ),
\'search_items\' => __( \'Szukaj w recenzjach\' ),
\'not_found\' => __( \'Nie znaleziono recenzji\' ),
\'not_found_in_trash\' => __( \'Nie znaleziono recenzji w koszu\' ),
);
$args = array(
\'labels\' => $labels,
\'menu_icon\' => \'dashicons-format-quote\',
\'public\' => true,
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'%review-category%\',\'with_front\' => true),
\'hierarchical\' => true,
\'menu_position\' => 0,
\'exclude_from_search\' => false,
\'supports\' => array(
\'title\',
\'editor\',
\'thumbnail\'
),
\'taxonomies\' => array(\'category\')
);
register_post_type( \'review\', $args );
}
add_action( \'init\', \'create_alco_custom_post\' );
然后我添加了分类法:
$labels = array(
\'name\' => __( \'Producenci\', \'alkowiki\' ),
\'singular_name\' => __( \'Producent\', \'alkowiki\' ),
\'search_items\' => __( \'Wyszukaj wśród producentów\', \'alkowiki\' ),
\'popular_items\' => __( \'Popularni producenci\', \'alkowiki\' ),
\'all_items\' => __( \'Wszystcy producenci\', \'alkowiki\' ),
\'parent_item\' => __( \'Producent rodzic\', \'alkowiki\' ),
\'edit_item\' => __( \'Edytuj producenta\', \'alkowiki\' ),
\'update_item\' => __( \'Aktualizuj producenta\', \'alkowiki\' ),
\'add_new_item\' => __( \'Dodaj nowego producenta\', \'alkowiki\' ),
\'new_item_name\' => __( \'Nowy producent\', \'alkowiki\' ),
\'separate_items_with_commas\' => __( \'Oddziel producentów przecinkami\', \'alkowiki\' ),
\'add_or_remove_items\' => __( \'Dodaj lub usuń producentów\', \'alkowiki\' ),
\'choose_from_most_used\' => __( \'Wybierz z najczęściej wykorzystywanych producentów\', \'alkowiki\' )
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'hierarchical\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'query_var\' => true
);
register_taxonomy( \'producents\', \'review\', $args );
最后,我改变了链接结构:
add_filter(\'post_link\', \'category_permalink\', 1, 3);
add_filter(\'post_type_link\', \'category_permalink\', 1, 3);
function category_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%review-category%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'category\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'nieskateryzowane\';
return str_replace(\'%review-category%\', $taxonomy_slug, $permalink);
}
我在WP设置中的帖子结构是
/%category%/%postname%/
贴子URL的创建正确,类别档案正确显示贴子,permlinks是根据我的需要创建的,但当我转到产品站点(URL结构站点/类别/产品名称)时,我收到404。当我访问带有分类法而不是类别(站点/分类法/产品名称)的URL时,我被重定向到/类别/产品名称站点,并且以404结尾。
我已经为此挣扎了好几天了,有什么想法吗?
编辑:
不幸的是,我不得不将永久链接切换回默认(%postname%)-我希望默认的WP帖子有这样的URL结构。我还将默认类别(与默认帖子连接)更改为名为“我的类别”的自定义分类法,并将其注册为我的自定义帖子类型。所以现在有两种分类法(与插入的第一种分类法相同)。他们的归档页面给出了404,而产品页面也仍然给出了404。如何设置重写结构?
我将明确我想要实现的目标:
网站。com/%taxonomy1%/-使用此分类法存档自定义帖子。com/%taxonomy2%/-使用此分类法存档自定义帖子。com/%taxonomy1%/自定义帖子名称/-自定义帖子页面(仅与第一个分类链接)