标签: custom-taxonomy

花哨的URL结构,条件内容和搜索 - 使用CPT,自定义字段和分类

我们正在3个国家/地区建立汽车相关业务目录 - 美国,加拿大和墨西哥.

我创建了一个自定义帖子类型"列表",并尝试继续以下内容.

对于各种SEO目的,我们需要特定的URL结构,如下所示:

1)个人列表页面

没什么太花哨的 - domain.com/usa/listing-slug

2)每州档案

没什么太好看的 - domain.com/usa/texas

3)每个城市的档案

这个更有意思,我们需要以下结构:

  • domain.com/usa/cleveland/oh
  • domain.com/usa/cleveland/nd

第一个是俄勒冈州克利夫兰的档案馆,另一个是内华达州克利夫兰的档案馆.

4)每ZIP存档

很平常 - domain.com/usa/05657

5)混合位置类别档案

  • domain.com/usa/cleveland/oh/car-parts
  • domain.com/usa/ohio/car-parts
  • domain.com/usa/05657/car-parts

6)以上所有存档页面的自定义内容(模板),可能是ZIP

我们试图在某种程度上模仿这个网站.

如果你检查一下,比方说,克利夫兰,俄亥俄州克利夫兰和内华达州的同样的例子:http://www.familydaysout.com/kids-things-to-do-usa/cleveland/ohHTTP://www.familydaysout .com/kids-things-to-do-usa/cleveland/nd你会明白我的意思 - 这些页面在开头就有自定义内容(描述).

以上是每个城市的自定义内容示例.每个州http://www.familydaysout.com/kids-things-to-do-usa/ohio和混合的情况也是如此:http : //www.familydaysout.com/kids-things-to-do-usa/ohio/major-theme-parks http://www.familydaysout.com/kids-things-to-do-usa/cleveland/oh/major-theme-parks http://www.familydaysout.com/kids-things -to-DO-USA/5601 /重大主题,公园

我在添加新列表时可以自动填充位置数据,所以当我粘贴一个完整的地址时,例如"21200 Saint Clair Avenue Euclid,Ohio 44117"并保存我得到这个:

自动填充的自定义字段

如您所见,国家,州,城市和ZIP都在那里.类别(汽车零件)也作为列表类别.

现在,我如何使用这些值来构建存档的URL和页面/模板?我应该自动将自定义字段值转换为自定义分类法并从那里开始吗?我知道我可以使用https://codex.wordpress.org/Plugin_API/Action_Reference/save_posthttps://codex.wordpress.org/Function_Reference/wp_set_object_terms来做到这一点

我知道自定义分类法可以有描述,我知道如何输出这些描述.除此之外,分类法可以是等级的,所以我可以将俄亥俄州和内华达州作为父母,每个克利夫兰作为他们的孩子.

所以我正处于收集和存储所有信息位的位置,但不知道如何开始操作它们来实现上述模型.

问题1:我应该在哪里存储位置数据(国家,州,城市,邮编),以便能够在URL中使用它,如上所述?自定义字段或分类术语?

Q2:如何以我可以为他们自定义模板的方式构建存档页面/模板?

任何其他有用的提示将受到高度赞赏.

wordpress permalinks wordpress-theming custom-fields custom-taxonomy

2
推荐指数
1
解决办法
1105
查看次数

在 Woocommerce 订单详细信息中显示产品类别名称

在 Woocommerce 中,我如何在订单详细信息(以及 Wordpress 的后端)中显示产品类别名称。也可以让它们出现在电子邮件通知中。

php wordpress orders custom-taxonomy woocommerce

2
推荐指数
1
解决办法
2976
查看次数

在 Woocommerce 单个产品页面中显示自定义分类

我使用以下代码向 Woocommerce 添加了一个名为“供应商”的新分类法:

// hook into the init action and call taxonomy when it fires

add_action( 'init', 'create_vendor_taxonomy', 0 );

// create and register vendor taxonomy (hierarchical)

function create_vendor_taxonomy() {

    $labels = array(
        'name'              => _x( 'Vendors', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Vendor', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Vendors', 'textdomain' ),
        'all_items'         => __( 'All Vendors', 'textdomain' ),
        'parent_item'       => __( 'Parent Vendor', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Vendor:', 'textdomain' ),
        'edit_item' …
Run Code Online (Sandbox Code Playgroud)

php wordpress product custom-taxonomy woocommerce

2
推荐指数
1
解决办法
5388
查看次数

在 Woocommerce 中检查产品类别的购物车项目

在 woocommerce 中,我尝试使用以下方法检查特定产品类别的购物车项目:

add_action('woocommerce_before_cart', 'fs_check_category_in_cart');
function fs_check_category_in_cart() {
    // Set $cat_in_cart to false
    $cat_in_cart = false;
    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        echo '<pre>',print_r($product),'</pre>';
        // If Cart has category "download", set $cat_in_cart to true
        if ( has_term( 'downloads', 'product_cat', $product->get_id() ) ) {
            $cat_in_cart = true;
            break;
        }
    }
    // Do something if category "download" is in the Cart      
    if ( $cat_in_cart ) {

        // For …
Run Code Online (Sandbox Code Playgroud)

php wordpress cart custom-taxonomy woocommerce

2
推荐指数
1
解决办法
2561
查看次数

获取 Woocommerce 产品类别图片网址

我使用以下代码来获取 Woocommerce 产品类别的缩略图 URL,但它只输出<img>带有src="unknown".

$cat_slug = t-shirts;
$thumbnail_id = get_woocommerce_term_meta( $cat_slug, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="50" height="50" />'; 
Run Code Online (Sandbox Code Playgroud)

让它发挥作用的最佳方法是什么?

编辑

在第二次调用 jean 类别的缩略图时,它只输出<img src(unknown) alt="" width="50" height="50" />.

<div class="list-item">
    <div class="item-img">

        <?php

        $term_slug    = 't-shirts';
        $taxonomy     = "product_cat";
        $term_id      = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
        $thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
        $image        = wp_get_attachment_url( $thumbnail_id );

        echo '<img src="'.$image.'" alt="" width="50" height="50" />';

        ?> …
Run Code Online (Sandbox Code Playgroud)

php wordpress image custom-taxonomy woocommerce

2
推荐指数
1
解决办法
8625
查看次数

从 WP_Query 中的当前产品类别术语 Id 获取所有 Woocommerce 产品

我有一个简单的 taxonomy-product_cat.php 页面,我试图在其中仅显示当前类别中的产品。现在,它正在显示所有产品,而不仅仅是当前类别中的产品。这是我的代码:

<ul class="products">

    <?php
       $current_cat_id = $wp_query->get_queried_object()->term_id;
       $args = array(
         'taxonomy'       => 'product_cat',
         'post_type'      => 'product',
         'post_status'    => 'publish',
         'posts_per_page' => -1,
         'tax_query'      => array(
             'taxonomy'   => 'product_cat',
             'field'      => 'term_id',
             'terms'      => $current_cat_id,
             'operator'   => 'IN'
         )
       );

       $products = new WP_Query( $args );

       while ( $products->have_posts() ) : $products->the_post();
           echo '<li><a href="'. get_permalink() .'"><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></a></li>';
       endwhile;

       wp_reset_query();
       ?>
</ul>
Run Code Online (Sandbox Code Playgroud)

我的客户不断更改类别的名称/slug,因此我需要按类别 ID 获取产品。知道为什么这会生成所有产品,而不仅仅是当前类别中的产品吗?

php wordpress product custom-taxonomy woocommerce

2
推荐指数
1
解决办法
6683
查看次数

列出从自定义帖子类型分配的所有分类法

我真的很想在这里寻求帮助,从昨天开始就一直在尝试这样做,但仍然没有运气,所以这就是我想要实现的目标

收藏= 自定义帖子类型

  • 材料= 分配给“集合”的自定义分类法

    • Mat1 = 术语
    • Mat2 = 术语
  • 正文= 分配给“集合”的自定义分类法

    • 正文 1 = 术语
    • Body2 = 术语
  • 颜色= 分配给“集合”的自定义分类法

    • 颜色 1 = 术语
    • 颜色 2 = 术语

结构将是这样的

- Material
  - Mat1
  - Mat2
- Body
  - Body1
  - Body2
- Color
  - Color1
  - Color2
Run Code Online (Sandbox Code Playgroud)

我得到了这个代码,但它根本不起作用

$post_type = 'collection';
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => true ) );

foreach( $taxonomies as $taxonomy ){
    echo $taxonomy->name;

    $terms = get_terms( …
Run Code Online (Sandbox Code Playgroud)

wordpress custom-post-type custom-taxonomy

2
推荐指数
1
解决办法
2335
查看次数

在 Woocommerce 中以编程方式设置或更新产品运输类

美好的一天,我一直在试图弄清楚如何使用 php 以编程方式选择运输类。我正在使用表单从前端创建 woocommerce 产品,并在表单提交时创建产品,但我可以选择任何运输类别。下面的屏幕截图显示了从前端创建的产品上的运输类别设置,其中检查元素选项卡显示了运输类别的 id(作为值)

woocommerce 航运类

我正在使用以下代码选择电话运费等级

$pShipping_Class = 25; //Where 25 is the id/value for Phone Shipping Fee Class
update_post_meta( $product_id, 'product_shipping_class', $pShipping_Class );
Run Code Online (Sandbox Code Playgroud)

update_post_meta 适用于所有其他字段,即使是我创建的自定义下拉字段,我可以用来update_post_meta( $product_id, '_list_of_stores', 'custom-order' );从我创建的自定义下拉字段中选择值 custom-order 但是当我为运输类尝试相同的事情时,它不起作用。不知道我做错了什么。

请指出我正确的方向。我如何使用 php 更新运输类。我已经有了 ID 和 slug。

谢谢

更新:我意识到当我手动选择电话运费并点击更新产品按钮时。它添加了 selected 属性(即 selected="selected" ),见下面的截图;

已选择 woocommerce 运输类别

请我如何进行此更新/选择任何运输类别(通过 ID 或 slug),因为运输类别需要即时更新,以便为用户提供他们创建并添加到购物车的产品的运费。

php wordpress product custom-taxonomy woocommerce

2
推荐指数
1
解决办法
2812
查看次数

将带有值的产品属性添加到 Woocommerce 中的产品

我正在使用此代码添加自定义属性

$attributes = array(
    array("name"=>"Size","options"=>array("S","L","XL","XXL"),"position"=>1,"visible"=>1,"variation"=>1),
    array("name"=>"Color","options"=>array("Red","Blue","Black","White"),"position"=>2,"visible"=>1,"variation"=>1)
);
if($attributes){
    $productAttributes=array();
    foreach($attributes as $attribute){
        $attr = wc_sanitize_taxonomy_name(stripslashes($attribute["name"])); // remove any unwanted chars and return the valid string for taxonomy name
        $attr = 'pa_'.$attr; // woocommerce prepend pa_ to each attribute name
        if($attribute["options"]){
            foreach($attribute["options"] as $option){
                wp_set_object_terms($product_id,$option,$attr,true); // save the possible option value for the attribute which will be used for variation later
            }
        }
        $productAttributes[sanitize_title($attr)] = array(
            'name' => sanitize_title($attr),
            'value' => $attribute["options"],
            'position' => $attribute["position"],
            'is_visible' => $attribute["visible"],
            'is_variation' => $attribute["variation"], …
Run Code Online (Sandbox Code Playgroud)

php wordpress custom-taxonomy woocommerce taxonomy-terms

2
推荐指数
1
解决办法
3434
查看次数

如何获取所有 WooCommerce 产品属性分类法和名称

在 WooCommerce 中,我尝试将自定义字段作为插件添加到所有产品属性中……

在下面的代码中,我可以向一个分类法添加自定义字段:

function pippin_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
    <label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label>
    <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
    <p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</div>
    <?php
}

add_action( 'pa_flavor_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );

// Edit term page
function pippin_taxonomy_edit_meta_field($term) {

// put the term ID into a variable
$t_id = $term->term_id;

// retrieve the existing …
Run Code Online (Sandbox Code Playgroud)

php wordpress plugins custom-taxonomy woocommerce

2
推荐指数
1
解决办法
2812
查看次数