我对WordPress相当陌生,我正在开发一个拥有丰富产品类别的网站。我想要创建一个自定义的post类型来添加一组产品。我将展示我正在努力实现的目标,然后详细阐述我所面临的问题。
Here是我的要求:
brand和functionality对我的产品进行分类,它需要翻译成:by品牌:
example.com/products/sub-category/product-name
eg. domain.com/products/toshiba/toshiba-E-Studio-2518Aby功能:
example.com/products/functionality-category/product-name
eg. domain.com/products/color-printer/toshiba-E-Studio-2518A正如您在上面的示例中所看到的,该产品被分类在多个类别下。
eg. domain.com/products/toshiba/ -> show all toshiba products
eg. domain.com/products/color-printer/ -> show all color printers
eg. domain.com/products/ -> show all products
到目前为止,我已经尝试过:
CPT ui创建它,在我的自定义帖子中使用类别,它解决了50%的问题。但是,该类别的URL段塞不能与自定义的后置段塞相同。我在这个问题上经历了将近一个<#>星期的困难,现在真的找不到解决它的办法,所以我希望得到一些帮助。
谢谢
发布于 2020-11-20 01:19:55
欢迎来到WPSE。
可以直接解决的问题。
Step 1
在主题的function.php或插件文件中注册自定义post类型,如下所示:
__('Products', 'textdomain'),
'singular_name' => __('Product', 'textdomain'),
'menu_name' => __('Products', 'textdomain'),
'name_admin_bar' => __('Products', 'textdomain'),
// Archive page name
'all_items' => __('Products', 'textdomain'),
'add_name' => __('Add new products', 'textdomain'),
'add_new_item' => __('Add new products', 'textdomain'),
'edit' => __('Edit products', 'textdomain'),
'edit_item' => __('Edit product', 'textdomain'),
'new_item' => __('New product', 'textdomain'),
'view' => __('View products', 'textdomain'),
'view_item' => __('View product', 'textdomain'),
'search_items' => __('Search products', 'textdomain'),
'parent' => __('Parent product', 'textdomain'),
'not_found' => __('No product found', 'textdomain'),
'not_found_in_trash' => __('No product found in trash', 'textdomain')
);
/* ------------------------------------------ End of Edit */
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'menu_position' => 21,
'menu_icon' => 'dashicons-marker', // Dashicon
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
// 'capabilities' => array(),
'rewrite' => array(
'slug' => $cpt_slug,
'with_front'=> true,
'pages' => true,
'feeds' => false
),
'supports' => $cpt_features
);
register_post_type($cpt_name, $args);
}
add_action('init', 'wpse_register_custom_post_type_products', 21);
?>上面的代码是做什么的:
Step 2
注册您的分类(类别或标签)。如果您想要“标记”而不是类别>,那么将层次结构更改为false。
下面的代码将一个分类法注册到您的定制post类型(产品类别和子类别,我认为这就是您想要的)。
which post type
$cpt_name = 'products';
// Taxonomy Name
$cpt_slug = 'brands';
// Taxonomy Labels
$labels = array(
'name' => __('Brands', 'textdomain'),
'singular_name' => __('Brand', 'textdomain'),
'search_items' => __('Search brands', 'textdomain'),
'popular_items' => __('Popular brands', 'textdomain'),
'all_items' => __('All brands', 'textdomain'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit brand', 'textdomain'),
'update_item' => __('Update brand', 'textdomain'),
'add_new_item' => __('Add new brand', 'textdomain'),
'new_item_name' => __('New brand','textdomain'),
'separate_items_with_commas'=> __('Separate brands', 'textdomain'),
'add_or_remove_items' => __('Add or remove brands', 'textdomain'),
'choose_from_most_used' => __('Choose from most used brands', 'textdomain'),
'not_found' => __('No brands found', 'textdomain'),
'not_found__in_trash' => __('No brands found in trash', 'textdomain'),
'menu_name' => __('Brands', 'textdomain')
);
// Taxonomy Args
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => $cpt_slug
)
);
register_taxonomy($cpt_slug, $cpt_name, $args);
}
add_action('init', 'wpse_register_taxonomy_brands');到现在为止还好。您现在应该有一个名为"products“的定制post类型和一个分类”品牌“。
Step 3
向WordPress提供正确的模板。
如果你不知道从哪里开始,这部分可能会非常棘手。所以我们才来这里:
您需要使用正确的钩子,即template_include,否则它将无法工作。不是 "template_redirect",这是一个不同的钩子。
为了在WordPress中检查/返回正确的模板,您需要如下所示:
yourdomain.com/products/ or search results page
if(is_post_type_archive('products')||(is_search() && $_GET['post_type']=='products')) {
// Here we check if a file exists in the current active theme folder, if not look in a plugin directory.
if(file_exists(get_template_directory() . '/archive-products.php')) {
return get_template_directory() . '/archive-products.php';
} else {
// Look in a plugin directory
return plugin_dir_path(__DIR__) . 'templates/archive-products.php';
}
// Same for Taxonomy
} elseif (is_tax('brands')) {
if(file_exists(get_template_directory(). '/taxonomy-brands.php')){
return get_template_directory(). '/taxonomy-brands.php';
} else {
return plugin_dir_path(dirname(__FILE__)).'templates/taxonomy-brands.php';
}
// Same for single product page
} elseif(is_singular('products')) {
if(file_exists(get_template_directory() . '/single-products.php')) {
return get_template_directory() . '/single-products.php';
} else {
return plugin_dir_path(__DIR__) . 'templates/single-products.php';
}
}
return $original_template;
}
add_action('template_include', 'wpse_custom_post_type_template_include');
?>重要的事情要记住:
函数get_template_directory_uri()返回主题but --这是错误的--我们不想那样。
我们需要get_template_目录(没有uri!)这是正确的函数,它将返回文件的路径。
函数get_template_directory_uri()返回一个url,但我们需要一个路径。因此,始终坚持使用get_template_directory()。
根据上面的代码,您的插件文件夹结构必须如下所示:

这将使您可以在插件中有一个模板(用于默认托运),并且可以被主题覆盖。
您现在应该能够在后端创建一个产品,给它一个类别和/或子类别。
问题的最重要之处在于使“分类法-{$taxonomy}.php”模板工作,该模板负责返回自定义post类型的分类法(品牌/功能)的正确模板。
熟悉WordPress中的“模板层次结构”,并在下面的页面中查找分类法-{$taxonomy}.php:
模板/存档-Products.php
'products',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'name'
);
$loop = new WP_Query( $query_args );
$html = '';
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
// Start of output, like:
$html .= ''.get_the_title().'';
$html .= ''.get_the_excerpt().'';
endwhile;
// reset the query
wp_reset_postdata();
// Spit the html out
echo $html;
} else {
// No posts / products found
echo ''__('No products found' 'textdomain').'';
}
?>模板/分类法-brands.php
'products',
'tax_query' => array(
array (
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $current_term,
)
),
));
while ( $query_args->have_posts() ) :
$query_args->the_post();
// Show Posts ...same as in archive-products.php, generate your markup here..
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
?>在您的例子中,您需要注册两个分类(品牌和功能)。
因此,除了“品牌”之外,还可以在WP层次结构之后注册第二个分类(复制第一个分类代码、更改名称等并添加第二个税务模板)。
请参阅此问题以进一步阅读:如何查询自定义post类型分类法
Step 4
刷新重写规则。
在您的仪表板中,转到>设置> permalinks。完成了。
奖金有时在开始时很难找到:
一旦添加了产品帖子,您可能很难在“显示”>菜单设置中找到正确的链接。

以防你的菜单选项中没有那个面板。然后查看右上角,确保显示了post类型。

理论上,这是完成的,你应该能够添加品牌和分品牌的产品,并应该能够输出他们的正面。
您的不同“需求”是税务查询和模板逻辑中的简单更改,应该可以使用上面给定的信息完成。
复制“分类法品牌”代码,将其更改为功能,添加第二个模板,名为分类法-Funcalities.php,您就完成了。
但是,您还可以查看一些钩子,如:
enter_title_here (更改标题字段占位符)、wp_enqueue_scripts (在前端加载一些额外的js或css文件)、admin_enqueue_scripts (如果您想向元数据的后端添加一些js或css文件,等等)。
调试模板也非常有用:
https://wordpress.stackexchange.com/questions/378422
复制相似问题