2016-11-26 78 views
0

在我的地盘我有下面的代码片段:如何在wordpress中对image_meta值执行查询?

<span class='legenda'> 
<?php _e('Artist:','twentytwelve_child');?> 
</span> 
<?php printf (' ' . $metadata[image_meta][credit]);?> 

最后一块[credit]给我的艺术家的名字。

请谁能告诉我如何将输出的艺术家姓名转换为查询WooCommerce产品数据库并向我展示该艺术家的所有产品的查询?

编辑:在每个图像中默认存储一些IPTC/EXIF数据。使用下面的代码行,我从数组[image_meta]中检索数据。 <?php $filedimensions = wp_get_attachment_metadata($post_thumbnail_id, FALSE); ?>

在这个数组中是一个字段[信用]什么拥有艺术家的名字。看截图。 enter image description here

名称(在这种情况下,Adrie Oosterwijk)必须变成链接。单击存档页面后,必须显示Adrie Oosterwijk提供的所有图像(本例中)。 所以我必须找到一种方法来查询所有这[信贷]字段,并返回来自各自艺术家的所有图像。

EDIT 2 Eleborate对我的评论:

关于搜索备注: 当在管理仪表板我点击产品标签上。 在这里,我看到了与各自分类法一起列出的产品。在右上角是搜索产品的搜索字段,例如按类别或标签。然而,当我在分类摄影师搜索(我改名艺术家摄影师(见下图),

enter image description here

返回任何结果。(参见下图)

enter image description here

任何想法为什么发生这种情况?

关于翻译备注: 我正在使用WPML翻译我的网站。此时此刻英语为默认语言,荷兰语为第二个。当我添加产品时,摄影师(或者我们最初称之为艺术家)完全被添加到定制分类标准“摄影师”中。然而,当我将产品从英语翻译成荷兰语并尝试翻译分类摄影师时,由于它涉及人名,顺便说一下,该分类标准名称并未被WPML列出(请参见下面的截图)

enter image description here

分类翻译的行为显示在products->编辑屏幕 请参见下面的截图。

enter image description here

正如我们可以看到分类的“摄影师”已列出,但是处理设置什么也不做。通常它被设置为复制。但复制在此处不可见,因此无法选择。当它默认设置为Copy时它会很棒。

在此刻,当我列出荷兰语产品时,摄影师摄影师仍为空。

有关如何添加这两件事的任何想法?

回答

1

此代码添加到您的functions.php或创建一个新的插件:

// action call to register a taxonomy 
add_action('init', 'artist_to_product'); 

// function for registering artist taxonomy 
function artist_to_product() { 
$labels = array(
    'name'      => 'Artists', 
    'singular_name'    => 'Artist', 
    'menu_name'     => 'Artist', 
    'all_items'     => 'All Artists', 
    'new_item_name'    => 'New Artist Name', 
    'add_new_item'    => 'Add New Artist', 
    'edit_item'     => 'Edit Artist', 
    'update_item'    => 'Update Item', 
    'separate_items_with_commas' => 'Separate Artists with commas', 
    'search_items'    => 'Search Artists', 
    'add_or_remove_items'  => 'Add or remove Artists', 
    'choose_from_most_used'  => 'Choose from the most used Artists', 
); 
$args = array(
    'labels'      => $labels, 
    'hierarchical'    => false, // we dont need hierarchical artists 
    'public'      => true, 
    'show_ui'     => true, // you can set it to false if you want 
    'show_admin_column'   => true, // you can set it to false if you want 
    'show_in_nav_menus'   => true, // show in product menu bar? 
    'show_tagcloud'    => true, // you can set it to false if you want 
); 

// register the taxonomys 
register_taxonomy('artist', 'product', $args); 
register_taxonomy_for_object_type('artist', 'product'); 
} 

/** 
* You have now an ARTIST taxonomy in your products 
* Your taxonomy link: http://www.yourdomain.com/artist/the-artist-name/ 
* Dont forget to flush your permalinks: Settings->Permalinks and save as it is. 

* You can add manuel an artist or do it automaticly 
* for automaticly adding on save/update a product 
*/ 

/** 
* Save post metadata when a post is saved. 
* 
* @param int $post_id The post ID. 
* @param post $post The post object. 
* @param bool $update Whether this is an existing post being updated or not. 
*/ 
function save_artist_meta($post_id, $post, $update) { 

    /* 
    * In production code, $slug should be set only once in the plugin, 
    * preferably as a class property, rather than in each function that needs it. 
    */ 
    $post_type = get_post_type($post_id); 

    // If this isn't a 'product' post, don't update it. 
    if ("product" != $post_type) return; 

    // - Update the post's taxonomy. 
    // GET YOUR ARTIST NAME FROM IMAGE 
    // i dont know how you retrierve it 
    // is it from thumbnail or from other images? i dont know 

    //Example: 
    $artist_name = 'Leonardo Da Vinci'; // can be comma seperated like: 'Leonardo Da Vinci, Van Gogh' or an array of artists 

    // set taxonomy to post 
    // make a check 
    if(empty($artist_name)) return; 

    $taxonomy = 'artist'; 
    $append = false; // we dont want to append, we want to replace it. 
    wp_set_object_terms($post_id, $artist_name, $taxonomy, $append); 

} 
add_action('save_post', 'save_artist_meta', 99, 3); 

如何获得针对前端的分类法:

// wp function to get the artists from a post as object 
$post_id = 46772; // your post id or post object 
$taxonomy = 'artist'; 
$artists_from_post = get_the_terms($post_id, $taxonomy); 

// wp function to get the artists url 
$taxonomy_slug = 'deny-de-vito'; // the slug of your artist taxonomy from above 
$taxonomy = 'artist'; 
$artist_url = get_term_link($taxonomy_slug, $taxonomy); 

现在,如何找到一个蛞蝓只有纯文本/艺术家的名字? 如果通过此功能全自动保存艺术家的名字,所以我们可以使用WP sanitize_title功能:

$artist_name_from_image = 'Leonardo Da Vinci'; 
$artist_slug = sanitize_title($artist_name_from_image); 
// this wil produce: 'leonardo-da-vinci'. 
+0

迈克,谢谢你帮助我。这是你代表我做的一件令人印象深刻的工作。奇怪的是,我刚刚得到了你的答案,我确实已经检查过。我会尽快研究并尝试您的解决方案,然后马上回来。我明天和星期三确实有一系列的约会,但我很好奇,在星期四之前我会尝试回复。现在,再次感谢你,我让你知道。 – A3O

+0

这是令人难以置信的!这工作真的很棒。如预期的那样更多!你摇滚!不仅如此,我还在仪表板中看到了艺术家的名字 - >产品标签。超!!!真。但是,当我们走到这么远时,在测试这个解决方案时想到了几件小事。因为现在我无法为艺术家搜索产品的数据库。 Wordpress提出“找不到产品”;第二,我使用WPML进行transltion管理,我发现这种分类法没有用其他语言填写。 – A3O

+0

通常情况下,您可以在分类标签中看到它是否被复制,翻译或无所事事选项。现在它被设置为“无所事事”,因此在其他语言中没有关于“艺术家”分类的数据。如果ther是一个将此分类法复制到每种语言的解决方案,但复制选项不可见,那将是非常好的。我将介绍WPML设置以实现这一目标。最后我不明白你提供的最后3位代码。我在我的函数中评论过它.php和idt很有用。 – A3O

0

我不能评论,所以我写在这里。它的总数基于你的模板或插件。请提供更多有关您的产品如何在您的店铺中保存的信息,每个产品上是否存在元键“艺术家”?或者做你的插件/模板保存艺术家数据在db行/列? 因为我知道woocommerce没有艺术家属性作为默认值。

+0

谢谢您的答复。我编辑了这个问题。我希望它更有意义。 – A3O

+0

你只想要艺术家的图像吗?或产品? 如果您想将艺术​​家名称自动添加到产品中作为分类标准,我建议您在发布后保存功能以获取产品图像并提取艺术家名称并添加或更新艺术家名称作为此产品的分类标准。这种方式将是最简单的方法。通过这种方式,您可以使用WP自分类法列出并执行您想要的操作。 –

+0

不适合写一个函数作为答案。但它的晚餐时间在这里:D –