我想将所有类别分别重定向到特定页面。找到了这段代码,但这仅适用于一个类别到一页,我需要将 20 个类别扩展到 20 个不同的页面。
function custom_category_template_redirect() {
if ( is_product_category() ) {
$category = get_queried_object();
if ( $category->term_id == 29, ) {
$redirect_page_id = 304;
wp_redirect( get_permalink( $redirect_page_id ) );
exit();
}
}
}
add_action( 'template_redirect', 'custom_category_template_redirect' );
有人帮我解决这个案子。
这很简单......您需要在函数中设置一组产品类别术语ID(键)=>重定向页面ID(值)对,例如:
add_action( 'template_redirect', 'product_category_archives_custom_redirects' );
function product_category_archives_custom_redirects() {
if ( is_product_category() ) {
$term_id = (string) get_queried_object_id();
// Array of term IDs (keys) => page IDs (values)
$data = array(
'29' => '304',
'31' => '305',
'32' => '312',
);
if ( array_key_exists( $term_id, $data ) ) {
wp_redirect( get_permalink( intval($data[$term_id]) ) );
exit();
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并工作。