Woocommerce Products by Author

Georgina Benedetti

I am trying to list woocommerce product by author. I followed this steps:

entered this code on theme/functions.php

add_action('init', 'wpse_74054_add_author_woocommerce', 999 );

function wpse_74054_add_author_woocommerce() {
add_post_type_support( 'product', 'author' );
}

This let me assigned an author to each product on the edit product tab.

I am able also able to display the product title the author and description using this code:

<?php $loop = new WP_Query( array ( 'post_type' => 'product', 'posts_per_page' => 10 ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>

<span class="postauthortop author vcard">
<i class="icon-user"></i> <?php echo __('by', 'virtue');?> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" rel="author"><?php echo get_the_author() ?></a>
<div class="short_description">
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
</div>
<?php endwhile; ?>

However, when people click on author's name only regular post will be shown. Not products.

I want to creat an author page where people can see a list of product by a singular author.

I have tried creating an author.php file using the following code, but is not working.

<?php $author_id = the_author_meta('ID'); ?>

<?php $args = array( $author_id => 'author' , 'post_type' => 'product' ); ?>

<?php $author_posts = get_posts ( $args ); ?>

<?php while( $author_posts->have_posts() ) : $author_posts->the_post(); ?>

<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>

<?php wp_reset_postdata(); ?>

<?php endwhile; ?

If anyone knows to write this code, please help me. Please, be specific as I am not familiar with coding.

Thanks in advance.

Marin Atanasov

Why don't you try to specify the post_type to be product globally when on is_author()?

It would be something like this (add this code to your functions.php):

<?php

add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
    if ($query->is_author() && $query->is_main_query()) {
        $query->set('post_type', 'product');
    }
}

?>

FYI, from the code blocks that you've submitted in your question, leave the first one (as you will still need to set the author for particular products), but you probably won't be needing the second and the third code block.

EDIT

To display products per author in a separate URL, insert the following code into your functions.php:

add_action('generate_rewrite_rules', 'my_custom_rewrite_rules');
function my_custom_rewrite_rules($rewrite) {
    $rewrite->rules = array(
        'author-products/([^/]+)/?$' => 'index.php?author_name=$matches[1]&post_type=product'
    ) + $rewrite->rules;
}

then go to Settings -> Permalinks and click the Save Changes button.

Your URLs will then be like: http://example.com/author-products/admin/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Hide all products with a specific stock status from WooCommerce catalog

来自分类Dev

如何从WooCommerce评论部分删除comment-author- [USER-ID]类?

来自分类Dev

Woocommerce wc_get_products()$ args-如何显示指定了“全部”标签的帖子?

来自分类Dev

使用WooCommerce按价格范围过滤产品wc_get_products

来自分类Dev

Author#new中的NoMethodError

来自分类Dev

/ products /'page'处的MultiValueDictKeyError

来自分类Dev

将@author的使用视为违反代码样式

来自分类常见问题

Javadoc @author标签的良好做法

来自分类Dev

ls --author是什么意思?

来自分类Dev

options(“ devtools.desc.author”)不粘滞,

来自分类Dev

AttributeError:“ LevelingSystem”对象没有属性“ author”

来自分类Dev

ls --author是什么意思?

来自分类Dev

Netbeans:在现有类上更新@author?

来自分类Dev

基于PHP的Author for beardbeard,sab,maraschino

来自分类Dev

Xpages @Author显示2位作者

来自分类Dev

SPARQL:未解析的前缀名称:dbpprop:author

来自分类Dev

Codeigniter products_model 和 products_images_model

来自分类Dev

我如何才能强制prod用户提交--author <the_real_author>

来自分类Dev

WooCommerce结帐

来自分类Dev

Woocommerce预订

来自分类Dev

Woocommerce属性

来自分类Dev

WordPress的-Woocommerce

来自分类Dev

WooCommerce会议

来自分类Dev

Woocommerce样式

来自分类Dev

git commit --author不需要密码吗?

来自分类Dev

如何在PyCharm中编辑默认的__author__ = name行

来自分类Dev

什么时候在PHPdoc中更新标签@author?

来自分类Dev

git log --author不显示所有提交

来自分类Dev

如何创建author-custom_type.php并重写规则?