Wordpress Woocommerce 产品简码

Posted

技术标签:

【中文标题】Wordpress Woocommerce 产品简码【英文标题】:Wordpress Woocommerce product shortcodes 【发布时间】:2014-09-20 16:25:46 【问题描述】:

我只是在查看是否有人知道通过短代码或任何其他没有图像和描述等的方法仅显示产品描述的方法。

我有很多关于 woo-commerce 短代码的文章,但没有这么具体的文章。我将它用作滑块的一部分,我不希望产品的图像在那里显示,所以只显示产品名称和描述,也许还有价格?

所以我正在编辑archive-product.php

我在此页面中有一个滑块,我想显示一个“特色项目”,但没有图片,只有描述:

  <?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * Override this template by copying it to yourtheme/woocommerce/archive-product.php
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

get_header( 'shop' ); ?>








<!-- Featured product title and desciption -->

<?php
ini_set('max_execution_time', 0); //I saw maximum execution time error on your image - this is for that
$args = array(
   'post_status'    => 'publish',
   'post_type'      => 'product',
   'meta_value'     => 'yes',
   'posts_per_page' => 1,
   'product_cat'    => 'grammar'
);

$product_query = new WP_Query( $args );

?>
<div class="shopBanner">
    <div class="container">
    <div id="myCarousel" class="carousel slide">
        <div class="carousel-inner">
            <div class="active item" id="id1">

                <div class="section group">
                   <?php  if ($product_query->have_posts()) : ?>
                      <?php while ($product_query->have_posts()) : ?>
                          <div class="col span_2_of_3">
                          <h1><?php echo $product_query->post->post_title; ?></h1>
                          <h2 style="margin-top:-20px;">
                              <?php echo $product_query->post->post_excerpt; ?>
                          </h2>
                          </div>
                       <?php endwhile; ?>
                    <?php endif; ?>
                    <div class="col span_1_of_3">
                        <span class="icon lush-comments" id="heroIcon"></span>
                    </div>

                </div> 
            </div> 
        </div>
    </div>
    </div>             
</div>  






    <!-- Link button to featured product -->

           <div class="section1" id="section1">
            <div class="container">
            <button class="mainbtn" style="float: left; margin-top: -82px;">View product</button>
                <div class="section group">
        <div class="col span_2_of_3">
              Take a look of our range of publications. Selected publications can be purchased via a download also.


        </div>
        <div class="col span_1_of_3">
        <div style="float:right;">
        <a href="https://twitter.com/" target="_blank"><button class="social" style="background-image:url(img/twitterIcon.png);"></button></a>
        <a href="https://www.facebook.com/" target="_blank"><button class="social" style="background-image:url(img/facebookIcon.png);"></button></a>
        </div>
        </div>



            </div>
        </div>
        </div>




    <div class="section1"  style="padding: 0px 0 50px 0;">   
        <div class="container">
            <div style="background-color: #fff; width:100%; color: #777777;">
                <div style="padding: 20px; margin-bottom:-15px;">      



        <?php
            /**
             * woocommerce_before_main_content hook
             *
             * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
             * @hooked woocommerce_breadcrumb - 20
             */
            do_action( 'woocommerce_before_main_content' );
        ?>





            <!--<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

                        <h3 style="color: #1dae5e; text-shadow:none; font-weight:600; font-size: 1.3em; margin-bottom:20px;"><?php woocommerce_page_title(); ?></h3>

            <?php endif; ?>-->



            <span style="float: right; margin-top:-40px;">

            <a class="shopNav" href="?post_type=product">Back to Shop</a>&nbsp;&nbsp;&nbsp;
            <a class="shopNav" href="?page_id=13">My Account</a>&nbsp;&nbsp;&nbsp;
            <a class="shopNav" href="?page_id=11">Basket</a>&nbsp;&nbsp;&nbsp;
            <a class="shopNav" href="?page_id=12">Checkout</a>

            </span>




      </div></div></div></div>  













    <div class="section1" style="padding: 40px 0 50px 0;">   
        <div class="container">
            <div style="background-color: #fff; width:100%; color: #777777;">
                <div style="padding: 20px;">           





            <?php do_action( 'woocommerce_archive_description' ); ?>

            <?php if ( have_posts() ) : ?>

                <?php
                    /**
                     * woocommerce_before_shop_loop hook
                     *
                     * @hooked woocommerce_result_count - 20
                     * @hooked woocommerce_catalog_ordering - 30
                     */
                    do_action( 'woocommerce_before_shop_loop' );
                ?>

                <?php woocommerce_product_loop_start(); ?>

                    <?php woocommerce_product_subcategories(); ?>

                    <?php while ( have_posts() ) : the_post(); ?>

                        <?php wc_get_template_part( 'content', 'product' ); ?>

                    <?php endwhile; // end of the loop. ?>

                <?php woocommerce_product_loop_end(); ?>

                <?php
                    /**
                     * woocommerce_after_shop_loop hook
                     *
                     * @hooked woocommerce_pagination - 10
                     */
                    do_action( 'woocommerce_after_shop_loop' );
                ?>

            <?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

                <?php wc_get_template( 'loop/no-products-found.php' ); ?>

            <?php endif; ?>

        <?php
            /**
             * woocommerce_after_main_content hook
             *
             * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
             */
            do_action( 'woocommerce_after_main_content' );
        ?>

        <?php
            /**
             * woocommerce_sidebar hook
             *
             * @hooked woocommerce_get_sidebar - 10
             */
            do_action( 'woocommerce_sidebar' );
        ?>


            </div>
        </div>    
     </div>   
       </div> 



    <?php get_footer( 'shop' ); ?>

【问题讨论】:

可能能够使用显示内容的简码(这是 WooCommerce 产品描述的内容)。您是否尝试过带有产品 ID 的 this one 之类的插件?使用可变产品会更复杂。 【参考方案1】:

下面的代码将获得 grammer 类别和 status 已发布的 10 个产品。可能这会有所帮助。

<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * Override this template by copying it to yourtheme/woocommerce/archive-product.php
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

get_header( 'shop' ); ?>

<!-- Featured product title and desciption -->

    <?php
    ini_set('max_execution_time', 0); //I saw maximum execution time error on your image - this is for that
    $args = array(
       'post_status'    => 'publish',
       'post_type'      => 'product',
       'meta_value'     => 'yes',
       'posts_per_page' => 10,
       'product_cat'    => 'grammar'
    );
    $product_query = new WP_Query( $args );
    ?>
    <div class="shopBanner">
        <div class="container">
        <div id="myCarousel" class="carousel slide">
            <div class="carousel-inner">
                <div class="active item" id="id1">
                    <div class="section group">
                        <?php while ( $product_query->have_posts() ) : $product_query->the_post(); global $product; ?>
                              <div class="col span_2_of_3">
                              <h1><?php the_title(); ?></h1>
                              <h2 style="margin-top:-20px;">
                                  <?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ); ?>
                              </h2>
                              </div>
                        <?php endwhile; ?>
                        <div class="col span_1_of_3">
                            <span class="icon lush-comments" id="heroIcon"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        </div>
    </div>
    <!-- Link button to featured product -->

    <div class="section1" id="section1">
        <div class="container">
            <button class="mainbtn" style="float: left; margin-top: -82px;">View product</button>
                <div class="section group">
            <div class="col span_2_of_3">
              Take a look of our range of publications. Selected publications can be purchased via a download also.
            </div>
            <div class="col span_1_of_3">
                <div style="float:right;">
                <a href="https://twitter.com/" target="_blank"><button class="social" style="background-image:url(img/twitterIcon.png);"></button></a>
                <a href="https://www.facebook.com/" target="_blank"><button class="social" style="background-image:url(img/facebookIcon.png);"></button></a>
                </div>
            </div>
            </div>
        </div>
    </div>

    <div class="section1"  style="padding: 0px 0 50px 0;">
        <div class="container">
            <div style="background-color: #fff; width:100%; color: #777777;">
                <div style="padding: 20px; margin-bottom:-15px;">
                    <?php
                        /**
                         * woocommerce_before_main_content hook
                         *
                         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
                         * @hooked woocommerce_breadcrumb - 20
                         */
                        do_action( 'woocommerce_before_main_content' );
                    ?>

                    <?php //if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
        <!--                        <h3 style="color: #1dae5e; text-shadow:none; font-weight:600; font-size: 1.3em; margin-bottom:20px;"><?php woocommerce_page_title(); ?></h3>-->
                    <?php //endif; ?>

                    <span style="float: right; margin-top:-40px;">
                        <a class="shopNav" href="?post_type=product">Back to Shop</a>&nbsp;&nbsp;&nbsp;
                        <a class="shopNav" href="?page_id=13">My Account</a>&nbsp;&nbsp;&nbsp;
                        <a class="shopNav" href="?page_id=11">Basket</a>&nbsp;&nbsp;&nbsp;
                        <a class="shopNav" href="?page_id=12">Checkout</a>
                    </span>
                </div>
            </div>
        </div>
    </div>

    <div class="section1" style="padding: 40px 0 50px 0;">
        <div class="container">
            <div style="background-color: #fff; width:100%; color: #777777;">
                <div style="padding: 20px;">

            <?php do_action( 'woocommerce_archive_description' ); ?>

            <?php if ( have_posts() ) : ?>

                <?php
                    /**
                     * woocommerce_before_shop_loop hook
                     *
                     * @hooked woocommerce_result_count - 20
                     * @hooked woocommerce_catalog_ordering - 30
                     */
                    do_action( 'woocommerce_before_shop_loop' );
                ?>

                <?php woocommerce_product_loop_start(); ?>

                    <?php woocommerce_product_subcategories(); ?>

                    <?php while ( have_posts() ) : the_post(); ?>

                        <?php wc_get_template_part( 'content', 'product' ); ?>

                    <?php endwhile; // end of the loop. ?>

                <?php woocommerce_product_loop_end(); ?>

                <?php
                    /**
                     * woocommerce_after_shop_loop hook
                     *
                     * @hooked woocommerce_pagination - 10
                     */
                    do_action( 'woocommerce_after_shop_loop' );
                ?>

            <?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

                <?php wc_get_template( 'loop/no-products-found.php' ); ?>

            <?php endif; ?>

        <?php
            /**
             * woocommerce_after_main_content hook
             *
             * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
             */
            do_action( 'woocommerce_after_main_content' );
        ?>

        <?php
            /**
             * woocommerce_sidebar hook
             *
             * @hooked woocommerce_get_sidebar - 10
             */
            do_action( 'woocommerce_sidebar' );
        ?>
            </div>
        </div>
     </div>
       </div>

    <?php get_footer( 'shop' ); ?>

【讨论】:

让我们continue this discussion in chat。 我在我的问题中添加了一张图片以显示结果。 我已经更新了答案。我还看到您的产品没有产品描述。这段代码应该可以工作..让我知道 查看显示项目名称和描述的错误字符串,但只是循环遍历并打印数百万次...... 你能给我你正在使用的这一页或整页代码的完整 .php 文件吗?很难说是什么问题。因为我已经测试了我发给你的代码。

以上是关于Wordpress Woocommerce 产品简码的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress 插件 - WooCommerce - 隐藏产品如果

WordPress 插件与 WooCommerce 产品页面冲突

woocommerce wordpress中缺货产品的自动无索引

如何在 WordPress 网站中更改 WooCommerce 产品图像大小?

Wordpress 区分来自 woocommerce 的实际页面和产品

Wordpress Woocommerce 将所有产品更改为缺货