首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将woocommerce产品连接到帖子

问将woocommerce产品连接到帖子
EN

Stack Overflow用户
提问于 2014-03-25 03:26:34
回答 1查看 4K关注 0票数 2

我在我的wordpress网站上使用woocommerce。我在卖画。这些产品是油画。我有一个艺术家的名单作为帖子。每个艺术家都是一个帖子。我想连接帖子和产品,这样我就可以在绘画页面上显示艺术家的名字,用户可以点击这个名字,它会把他们带到艺术家的帖子。我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2014-03-27 01:05:02

这是一个如何在WooCommerce产品常规选项卡中添加自定义字段的示例。由于艺术家是帖子(没有指定类别),它将收集所有帖子的链接,并将它们放在下拉列表中。此字段的值将在价格下方的单个产品页面上可见(您可以在WooCommerce主题中打开content-single-product.php文件,查看单个产品模板的操作,以及附加的功能,如果您想要更改链接将出现的位置,请更改woocommerce_product_artist功能的优先级)。

代码语言:javascript
复制
<?php

add_action( 'admin_init', 'woocommerce_custom_admin_init' );

function woocommerce_custom_admin_init() {

    // display fields
    add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_add_custom_general_fields' );

    // save fields
    add_action( 'woocommerce_process_product_meta', 'woocommerce_save_custom_general_fields' );

}

function woocommerce_add_custom_general_fields() {

    // creating post array for the options ( id => title)
    $posts = array( '' => __( 'Select Artist' ) );
    array_walk( get_posts( array( 'numberposts' => -1 ) ), function( $item ) use ( &$posts ) {
        $posts[ $item->ID ] = $item->post_title;
    } );

    // creating dropdown ( woocommerce will sanitize all values )
    echo '<div class="options_group">';
    woocommerce_wp_select(
        array(
            'id' => '_artist',
            'label' => __( 'Artist' ),
            'options' => $posts
        )
    );
    echo '</div>';

}


function woocommerce_save_custom_general_fields( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // validate id of artist page and save
    if ( isset( $_POST['_artist'] ) ) {
        $value = filter_input( INPUT_POST, '_artist', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 0 ) ) );       
        update_post_meta( $post_id, '_artist', $value );        
    }

}

add_action( 'init', 'woocommerce_custom_init' );

function woocommerce_custom_init() {    

    // hook the woocommerce_product_artist function on to woocommerce_single_product_summary action ( priority 15 )
    add_action( 'woocommerce_single_product_summary', 'woocommerce_product_artist', 15 );

}

function woocommerce_product_artist() {
    global $post;

    // get the artist page id and show in template ( if exists )
    $artist_id = get_post_meta( $post->ID, '_artist', true );

    if ( $artist_id ) : 
    ?>
        <div class="product-artist"><a href="<?php echo get_permalink( $artist_id ); ?>"><?php echo get_the_title( $artist_id ); ?></a></div>

    <?php endif;
}

?>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22618776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档