2017-03-15 14 views
0

在從頭開始創建帶有WooCommerce功能的第一個WordPress主題的開發階段,我一直將所有相關內容直接編碼到php文件中。不知道這是否與其他人一樣「標準」,但是如果MySql數據庫發生任何事情,我的方法就是保護內容。如何在WordPress FrontEnd中調用最新產品

也就是說,我並沒有將所有相關內容傳送到WordPress的FrontEnd。我已經開始與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 Pages/Post中創建一個選項,您可以選擇它並打開一個包含「您想要顯示多少產品」,「背景顏色」等選項的Box。如果這是沒有那麼多額外的工作,我會很感激任何指標來實現這一點。

+0

我真的不知道你的意思在這裏是什麼...你發佈的代碼是正確的方式來做到這一點... –

+0

謝謝您響應。代碼在硬編碼到index.php文件時起作用,但是我希望將代碼放在WordPress的前端中。因此,通過'<?php the_content?>在index.php文件中調用。當我將上面的代碼放在前端時,編碼就會出現而不是實際的內容。 – Craig

+0

您應該能夠將該代碼複製到index.php文件中,並且如果您輸入了產品,則應該將其呈現出來。如果它不是你的WP安裝有問題。如果你在外部文件中有這樣的代碼,你可以使用'load_template_part()'來加載代碼。 –

回答

1

在對WooCommerce簡碼進行了一些快速研究之後,您可以調用:[recent_products per_page="4" columns="4"],它會顯示最近的產品。只需將其粘貼到您的所見即所得編輯器中即可。

有關WooCommerce簡碼的更多信息,請查看此鏈接:https://docs.woocommerce.com/document/woocommerce-shortcodes/

+0

謝謝你。非常感激。是否沒有辦法繼續使用PHP方法,但通過使用Wysiwyg?如自定義字段等?使用簡碼,我覺得我對整體外觀的控制力較弱。 – Craig

+0

如果你願意,你可以創建自己的短代碼...應該很簡單。查看簡碼API:https://codex.wordpress.org/Shortcode_API如果您願意,請隨時接受此答案:) –

相關問題