【问题标题】:Woocommerce Products by Author作者的 Woocommerce 产品
【发布时间】:2014-09-13 17:43:11
【问题描述】:

我正在尝试按作者列出 woocommerce 产品。 我按照以下步骤操作:

theme/functions.php输入此代码

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

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

这让我可以在编辑产品选项卡上为每个产品分配一个作者。

我还可以使用此代码显示产品标题、作者和描述:

<?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; ?>

但是,当人们点击作者姓名时,只会显示常规帖子。不是产品。

我想创建一个作者页面,人们可以在其中查看单个作者的产品列表。

我尝试使用以下代码创建 author.php 文件,但没有成功。

<?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; ?

如果有人知道编写此代码,请帮助我。请具体说明,因为我不熟悉编码。

提前致谢。

【问题讨论】:

  • 好吧,我在书店 woo 网站上做了类似的事情,尽管您尝试做的事情很复杂,但我只使用了一个名为 Authors 的属性,并且我将所有作者的姓名作为该分类法的术语(作者) .因此,只需将作者添加到产品中即可。 Rest 与仅循环特定作者的产品非常相似,例如类别或属性(分类法)。您可以按属性添加 woo 过滤器导航,单击作者将显示所有属于他的产品。实际上,您可以从管理员处完成所有这些操作,而无需触摸主题上的任何单个文件。我当然认为:)

标签: wordpress post woocommerce author


【解决方案1】:

为什么不尝试在is_author() 上将post_type 全局指定为product

会是这样的(将此代码添加到您的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');
    }
}

?>

仅供参考,从您在问题中提交的代码块中,保留第一个(因为您仍需要为特定产品设置作者),但您可能不需要第二个和第三个代码块。

编辑

要在单独的 URL 中显示每个作者的产品,请将以下代码插入您的 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;
}

然后转到Settings -&gt; Permalinks 并单击Save Changes 按钮。

您的网址将类似于:http://example.com/author-products/admin/

【讨论】:

  • 嗨,马林,你是我的英雄!非常感谢!还有一件事:是否可以在替代作者页面上显示这些 post_types?像 domain.com/author-products/author-name 这样的东西?并保留原来的 domain.com/author/author-name 页面,显示常规帖子?再次感谢您的慷慨帮助!
  • 我在上面编辑了我的答案,为您提供了一个示例解决方案。
  • 非常感谢!!我希望你不介意我再问一件事:如果我想在个人资料页面上为每个作者提供指向产品作者列表的链接,我应该写什么。我有这个 将链接到常规邮政。除了“产品”,还有什么是相同的?再次感谢您的所有帮助!
  • 使用:&lt;a href="&lt;?php echo str_replace('/author/', '/author-products/', get_author_posts_url(get_the_author_meta('ID'))); ?&gt;" rel="author"&gt;&lt;?php echo get_the_author() ?&gt;&lt;/a&gt;。如果您觉得我的回答有帮助,请不要忘记投票并接受答案。
  • Marin,很抱歉再次打扰您。有没有办法只显示 domain/author/author-name 上的常规帖子?此页面显示常规帖子和产品。而且我已经将域/作者产品/作者名称页面设置为仅显示产品。你能提供最后的帮助吗?非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多