gpt4 book ai didi

wordpress - 作者的 Woocommerce 产品

转载 作者:行者123 更新时间:2023-12-03 18:37:50 30 4
gpt4 key购买 nike

我正在尝试按作者列出 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; ?

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

提前致谢。

最佳答案

为什么不尝试指定 post_type成为 product全局开启 is_author() ?

它会是这样的(将此代码添加到您的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 -> Permalinks并点击 Save Changes按钮。

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

关于wordpress - 作者的 Woocommerce 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25825897/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com