我试图获得产品自定义属性值,但我很难做到这一点.
我试着这样做:
global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));
Run Code Online (Sandbox Code Playgroud)
而且我得到了这些原始数据:
[pa_koostis] => Array
(
[name] => pa_koostis
[value] =>
[position] => 0
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
Run Code Online (Sandbox Code Playgroud)
我知道有一个值,因为它在属性部分显示它,但我无法找到一种方法让它在我的自定义代码中显示.
我有一个自定义的帖子类型djs和一个city有两个术语的自定义分类:boston和nyc.所以DJ可以被标记为波士顿或纽约.
DJ型材(单djs柱)位于/nyc-wedding-dj/joe-shmoe/,例如,或用于波士顿DJ, /boston-wedding-dj/jane-doe/.换句话说,该自定义帖子类型的重写slug设置为%city%-wedding-dj.
我有两个常规页面/boston-wedding-djs/和/nyc-wedding-djs/(注意带有s的多个DJ),我使用自定义页面模板来循环来自相应城市的DJ,并显示从该页面的城市到DJ个人档案的链接列表.
我想做的是让重写slug成为%city%-wedding-djs自定义帖子类型单页"出现"(至少谷歌)作为上述那些页面的子页面.
有一个冲突然而,当我更改自定义后类型重写塞djs来%city%-wedding-djs,我得到的网页404 Not Found错误/boston-wedding-djs/和/nyc-wedding-djs/.
我怎样才能有一个与页面同名的重写slug?
我只想展示那些没有分类"产品品牌"的"品牌slu"一词的帖子.
我当前的查询不适用过滤器:
SELECT DISTINCT * FROM $wpdb->posts AS p
LEFT JOIN $wpdb->postmeta AS meta ON p.ID = meta.post_id
LEFT JOIN $wpdb->term_relationships AS rel ON rel.object_id = p.ID
LEFT JOIN $wpdb->term_taxonomy AS tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
LEFT JOIN $wpdb->terms AS term ON tax.term_id = term.term_id
WHERE 1=1
AND p.post_type = 'product'
AND p.post_status = 'publish'
AND p.post_title LIKE '%$trimmed%' OR (meta.meta_key = 'product_model' AND meta.meta_value LIKE '%$trimmed%')
AND (tax.taxonomy = 'product-brand' AND term.slug NOT IN ('$protected'))
Run Code Online (Sandbox Code Playgroud)
分类法或slug条件似乎都不适用于上述查询.
任何帮助表示赞赏!
我正在创建一个新模板,它将获得所有自定义帖子类型(案例研究)内容,包括与之关联的分类法值.
到目前为止,我得到以下内容:
<section>
<h1><?php _e( 'posts', 'casestudies' ); ?></h1>
<?php get_template_part('loop'); ?>
<?php
$args = array('post_type' => 'casestudies', 'posts_per_page' => 3);
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<h2><?php the_title(); ?></h2>
<p>Meta: <?php the_meta(); ?></p>
<p>Excerpt: <?php the_excerpt(); ?></p>
<p>what_to_put_here_to_get_taxonomies_values????</p>
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>
</section>
Run Code Online (Sandbox Code Playgroud)
我如何获得它的分类?我尝试了很多东西,但似乎都失败了,只是变得更加困惑.
我有一个名为campaign的自定义分类法,一个名为的自定义帖子类型asset。对于资产,我希望具有以下永久链接结构:mysite.com/<campaign_name>/<asset_name>。我已经通过以下代码实现了这一点,但是现在如果我进入带有url结构的任何普通页面,mysite.com/<pagename>它将给出404。并且当我注释掉用于注册自定义帖子类型的函数中的rewrite slug部分时,或者添加了它ams/%campaign%,它可以工作,但这不是我想要的自定义帖子类型的URL结构。
注册自定义分类法的代码:
...
'rewrite' => array(
'slug' => '',
'with_front' => true,
),
...
Run Code Online (Sandbox Code Playgroud)
注册自定义帖子类型的代码:
...
rewrite' => array(
'slug' => '%campaign%',
'with_front' => true,
),
...
Run Code Online (Sandbox Code Playgroud)
重写规则的功能:
function ams_asset_add_rewrite_rules( $rules ) {
global $post;
if ($post->post_type == 'asset' ) {
$new = array();
$new['([^/]+)/(.+)/?$'] = 'index.php?asset=$matches[2]';
$new['(.+)/?$'] = 'index.php?campaign=$matches[1]';
return array_merge( $new, $rules );
}
return $rules;
}
add_filter( 'rewrite_rules_array', 'ams_asset_add_rewrite_rules' );
// Handle the '%campaign%' URL placeholder …Run Code Online (Sandbox Code Playgroud) 我已经在WordPress上创建了自定义分类法,我想在列表中显示帖子上的当前帖子分类.
我正在使用以下代码显示名为"Job Discipline"的自定义分类:
<ul>
<?php $args = array('taxonomy' => 'job_discipline'); ?>
<?php $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<li>
Job Discipline: <a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
<?php echo $tax_menu_item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
Run Code Online (Sandbox Code Playgroud)
这只是我想列出的许多分类法之一.
问题是上面的代码显示的是所有"职位规则",其中至少有一个职位,而不是当前的职位分类.
我该如何解决这个问题?
如何从插件中为WooCommerce创建属性?我发现只有:
wp_set_object_terms( $object_id, $terms, $taxonomy, $append);
Run Code Online (Sandbox Code Playgroud)
但这种方法需要某些产品的ID.我需要生成一些没有附加到任何产品的属性.
这是我第一次尝试使用WordPress重写规则,所以请耐心等待.问题是我添加到我的投资组合的所有项目都有多个类别.我想在显示投资组合帖子时从网址中删除该类别.
site.com/portfolio - >作品
site.com/portfolio/category/ - >作品
site.com/portfolio/category/post-added-to-portfolio/ - >工作,但我不希望它
site.com/portfolio/post-added-to-portfolio/ - >不行,但它应该
/* Post Type: Portfolio */
$labels = array(
"name" => __( "Portfolio", "" ),
"singular_name" => __( "Portfolio", "" ),
);
$args = array(
"label" => __( "Portfolio", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => "portfolio",
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" …Run Code Online (Sandbox Code Playgroud) 我在 Woocommerce 中的产品上使用了很多产品属性,并且我正在遍历表格中的所有变体,这些变体可以在产品页面上用短代码显示。
对于这个表,我需要表头中的所有产品属性(这是在遍历变体之前),我使用以下方法获取属性:
$attributes = $product->get_variation_attributes();
foreach ($attributes as $key => $value) {
echo '<td>'.&key.'</td>';
}
Run Code Online (Sandbox Code Playgroud)
这不是很优雅,是吗?
所以这也有效:
$attributes = $product->get_attributes();
foreach ($attributes as $attribute) {
echo '<td>'$attribute['name']'</td>';
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都得到了产品属性的 slug。我需要获取标签名称,因为每个名称都有一个 Polylang 翻译(术语也是)。
如何获取产品属性标签名称而不是分类标头?
我已经在 Wordpress 中注册了一个自定义分类法,但我无法弄清楚为什么它没有显示在标准的 Wordpress 帖子中,因为已经引入了 Gutenberg。我的意思是,在添加或编辑帖子时,它不会显示在文档侧栏中。'Categories' 和 'Tags' 也是如此,它们显然是标准分类法。
我已经确保 'show_in_rest' => true 存在是分类法注册,但它没有任何区别。
似乎他们正在部分注册,因为它们显示在主左侧菜单的“帖子”下,这表明它可能与古腾堡有关?
有任何想法吗?
// Register taxonomy
add_action( 'init', 'register_taxonomy_articles_element' );
function register_taxonomy_articles_element() {
$labels = array(
'name' => _x( 'Elements', 'articles_element' ),
'singular_name' => _x( 'Element', 'articles_element' ),
'search_items' => _x( 'Search Elements', 'articles_element' ),
'popular_items' => _x( 'Popular Elements', 'articles_element' ),
'all_items' => _x( 'All Elements', 'articles_element' ),
'parent_item' => _x( 'Parent Element', 'articles_element' ),
'parent_item_colon' => _x( 'Parent Element:', 'articles_element' ),
'edit_item' => _x( 'Edit …Run Code Online (Sandbox Code Playgroud) custom-taxonomy ×10
wordpress ×10
php ×7
product ×3
woocommerce ×3
foreach ×1
mysql ×1
permalinks ×1
taxonomy ×1