douzi1350 2016-11-26 15:22
浏览 42
已采纳

如何在wordpress中对image_meta值执行查询?

In my site I have the next code snippet:

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

The last piece [credit] give me the name of the artist.

Please can anyone tell me how to turn the outputted artist name into a query that searches the WooCommerce product database and show me all the products for that artist?

EDIT: In every image is default some IPTC/EXIF data stored. With the following code line I retrieve the data from the array [image_meta]. <?php $filedimensions = wp_get_attachment_metadata( $post_thumbnail_id, FALSE ); ?>

In this array is a field [credit] what holds the name of the Artist. See screenshot. enter image description here

The name (in this case Adrie Oosterwijk) must be turned into a link. After clicking an archive page has to be shown with all the images from (in this case) Adrie Oosterwijk. So I have to find a way to query all of this [credit] fields and return all images from the respective Artist.

EDIT 2 Eleborate on my comments:

Concerning the search remark: When on the admin dashboard I click on the products tab. Here I see the products listed with the respective taxonomies. On the top-right is a search field to searc the products, for instance by category or tag. However when I search on the taxonomy Photographer (I renamed artist to Photographer (see screenshot below),

enter image description here

it returns no results. (see screenshot below)

enter image description here

Any idea why this is happening?

Concerning the translate remark: I'm using WPML for the translation of my site. At this moment there is English as default language and Dutch as second one. When I add a product the Photographer (or artist as we called it at first) is perfectly added to the Custom Taxonomy 'photographers'. However when I translate the product from English to Dutch and try to translate the Taxonomy Photographer, what by the way shouldn't be nesaccery because it concerns personal names, the taxonomy name is not listed by WPML (see screenshot below)

enter image description here

The behavior of taxonomy translation is shown at the products->edit screen See screenshot below.

enter image description here

As we can see the Taxonomy 'Photographers' is listed but the handling is set to Do Nothing. Normally it is set to Copy. However Copy is not visible here and therefor can't be selected. It would be great when it is set to Copy by default.

At this moment when I list the products for the Dutch language the taxonaomy Photographers remain empty.

Any idea on how to adddress this two things?

  • 写回答

2条回答 默认 最新

  • dongxixia6399 2016-11-27 15:37
    关注

    Add this code to your functions.php or create a new plugin:

    // 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 );
    

    How to get a taxonomy for the frontend:

    // 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 );
    

    Now, how to find a slug only by the plain text/artists name? if you saved the artist name automaticly by this function, so we can use the wp sanitize_title function:

     $artist_name_from_image = 'Leonardo Da Vinci';
     $artist_slug = sanitize_title( $artist_name_from_image );
    // this wil produce: 'leonardo-da-vinci'.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗