Php 如何调用WordPress前端中的最新产品

Php 如何调用WordPress前端中的最新产品,php,html,wordpress,Php,Html,Wordpress,在使用WooCommerce功能创建我的第一个WordPress主题的开发阶段,我从头开始,将所有相关内容直接硬编码到php文件中。不确定这是否是其他人的“标准”,但如果MySql数据库发生任何事情,这是我保护内容的方法 也就是说,我不会将所有相关内容转移到WordPress的前端。我从index.php文件开始,在index.php文件中输入了以下代码: <?php get_header(); ?> <?php if ( have_posts() ):

在使用WooCommerce功能创建我的第一个WordPress主题的开发阶段,我从头开始,将所有相关内容直接硬编码到php文件中。不确定这是否是其他人的“标准”,但如果MySql数据库发生任何事情,这是我保护内容的方法

也就是说,我不会将所有相关内容转移到WordPress的前端。我从index.php文件开始,在index.php文件中输入了以下代码:

<?php get_header(); ?>
    <?php
        if ( have_posts() ):  
            while( have_posts() ): the_post(); 
    ?>
    <?php the_content(); ?>

    <?php 
            endwhile; 
        endif; 
    ?>

<?php get_footer (); ?>
在内容本身中,我希望能够调用发布的最新产品。通过将以下代码输入模板文件,然后在index.php中调用它,我成功地实现了这一点:

<div class="row" id="latest-products">
    <h2 id="latest-products-row"><i class="fa fa-circle" aria-hidden="true"></i><span class="latest-products-title">Latest Products</span><i class="fa fa-circle" aria-hidden="true"></i></h2>
        <ul class="row-fluid">
            <?php
                $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; 
            ?>
                        <li class="latest-products">    
                            <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php 
                                    if (has_post_thumbnail( $loop->post->ID )) 
                                    echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); 
                                    else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; 
                                ?>
                            </a>
                                <h3><?php the_title(); ?></h3>
                                <p><?php the_excerpt(); ?></p>
                                <div class="price-row"><i class="fa fa-circle" aria-hidden="true"></i><span id="product-price"><?php echo $product->get_price_html(); ?></span><i class="fa fa-cirlce" aria-hidden="true"></i></div>
                                <div class="buy-button"><a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">View/Buy</a></div>
                        </li>
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
        </ul>
</div>
现在,我只想在WordPress前端调用上面的代码。目前,我唯一能做到这一点的方法是通过硬编码,这不是我想要实现的


有没有人能给我指出一个正确的方向,我可以从哪里开始实现这一目标?最后,我想在WordPress页面/帖子中创建一个选项,通过该选项,您可以选择并打开一个包含选项的框,例如“您想展示多少产品”、“背景颜色”等。如果这不是那么多额外的工作,我也希望您能提供一些指导

在对WooCommerce短代码进行快速研究后,您可以将其命名为:[每_页面最近的_产品=4列=4],它将显示最近的产品。只需将其粘贴到所见即所得编辑器中


有关WooCommerce短代码的更多信息,请查看此链接:

我不太确定您在这里的意思。。。您发布的代码是正确的方法…感谢您的回复。当硬编码到index.php文件中时,代码可以工作,但是我希望将此代码放在WordPress的前端。因此,当我将上述内容放在前端时,在index.php文件中通过调用,代码只是显示出来,而不是实际内容。您应该能够将代码复制到index.php文件中,如果您输入了产品,则应该将其呈现出来。如果没有,你的WP安装有问题。如果在外部文件中有该代码,可以使用load_template_part加载该代码。当我将代码直接粘贴到WordPress页面时,它在index.php文件中工作,而不是在前端。它只是显示代码。对。因为您无法将PHP代码添加到WYSIWYG编辑器。这类工作必须在模板级别完成。谢谢。非常感谢。除了使用所见即所得(Wysiwyg)之外,我还可以继续使用PHP方法吗?例如自定义字段等?有了短代码,我觉得我对整体外观的控制力变小了。如果你愿意,你可以创建自己的短代码。。。这应该很容易。查看shortcode API:如果您愿意,可以接受以下答案: