簡體   English   中英

編輯 woocommerce 模板文件以進行自定義布局

[英]editing woocommerce template files for custom layout

我正在嘗試使用 woocommerce 插件為 Wordpress 主題添加一些自定義。 我正在嘗試對商店頁面進行風格化,以便在產品頂部或左側列出一個類別列表。 當您從 woocommerce 管理設置中的目錄頁面啟用類別選項時,它會利用 content_product_cat2.php 模板文件在 woocommerce 產品循環中顯示產品類別縮略圖。 我已經移動了那個 php 文件並開始編輯它並獲得結果。 我已經刪除了縮略圖,但我無法從網格格式或循環之外獲取標題。 我願意向我的functions.php 添加一些鈎子,但我沒有在woocommerce 鈎子參考中看到任何看起來像他們會做的鈎子。 也許是編輯此模板並使用鈎子將其放置在循環之前的組合。 我不確定這就是我發布的原因。 這是該頁面的鏈接。 我正在使用它,這里是 php 文件的內容:

<?php
/**
 * The template for displaying product category thumbnails within loops.
 *
 * Override this template by copying it to yourtheme/woocommerce/content-product_cat.php
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */




global $woocommerce_loop;

// Custom Edits

if ( is_shop() && in_array( $category->slug, array( 'fakeie' ) ) ) {
return;
}


// Store loop count we're currently on
if ( empty( $woocommerce_loop['loop'] ) )
$woocommerce_loop['loop'] = 0;

// Store column count for displaying the grid
if ( empty( $woocommerce_loop['columns'] ) )
$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );

// Increase loop count
$woocommerce_loop['loop']++;
?>
<li class="product <?php
if ( $woocommerce_loop['loop'] % $woocommerce_loop['columns'] == 0 )
    echo 'last';
elseif ( ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] == 0 )
    echo 'first';
?>">

<?php do_action( 'woocommerce_before_subcategory', $category ); ?>

<a href="<?php echo get_term_link( $category->slug, 'product_cat' ); ?>">

    

    <h3 style="float:left;">
        <?php echo $category->name; ?>
        <?php if ( $category->count > 0 ) : ?>
            <mark class="count">(<?php echo $category->count; ?>)</mark>
        <?php endif; ?>
    </h3>

    <?php
        /**
         * woocommerce_after_subcategory_title hook
         */
        do_action( 'woocommerce_after_subcategory_title', $category );
    ?>

</a>

<?php do_action( 'woocommerce_after_subcategory', $category ); ?>

</li>

謝謝你們...我知道有人會有這方面的經驗。

編輯 - 好吧,我意識到編輯 content_product_cat.php 文件可能不是最好的方法。 Jay 在下面提到最好編輯 content_product.php 文件。 雖然我最初認為這也是一個很好的解決方案,但我意識到它不會完成我想要做的事情。 編輯 content_product.php 只會在循環內進行編輯,我需要在循環之前顯示我的類別,以便它們不會落入循環的網格格式......任何新想法?

一種替代方法是您可以禁用默認的 woocommerce 類別視圖並且不使用模板。 相反,您可以編寫自定義代碼並獲取類別列表並按照您想要的方式設置樣式並將其添加到 woocommerce 產品網格中。 下面是你如何做到的。

<?php $all_categories = get_categories( 'taxonomy=product_cat&hide_empty=0&hierarchical=1' );
                    foreach ($all_categories as $cat) {
                        if($cat->category_parent == 23) {                           
                                                        $category_id = $cat->term_id;
                            $thumbnail_id   = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                            $image = wp_get_attachment_url( $thumbnail_id );
                                echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'"><img src="'.$image.'" alt="'. $cat->name .'"/><div>'. $cat->name .'</div></a>';
                        }
                    }
?>

你可以添加到你的functions.php:

add_action('woocommerce_before_shop_loop','showcats');


function showcats()
{
    //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)

$taxonomy     = 'product_cat';
$orderby      = 'name';
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title        = '';

$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title
);

?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
<?
}

function showcatlist()
{
    if(is_shop()) showcats();
}   

showcats 功能來自: http ://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-category-list。 閱讀更多信息: http : //codex.wordpress.org/Template_Tags/wp_list_categories 要更改列表的樣式,您將閱讀:

您可以通過將 title_li 參數設置為空字符串來刪除最外面的項目和列表。 您需要自己將輸出包裝在有序列表 (ol) 或無序列表中(請參閱上面的示例)。 如果您根本不需要列表輸出,請將 style 參數設置為 none。

除了 wp_list_categories,您還可以使用 get_categories http://codex.wordpress.org/Function_Reference/get_categories並編寫自己的 html 輸出。 這與首先​​提供正確答案的Jay Bhatt所做的相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM