【问题标题】:editing woocommerce template files for custom layout为自定义布局编辑 woocommerce 模板文件
【发布时间】:2013-04-12 03:02:25
【问题描述】:

我正在尝试使用 woocommerce 插件为 Wordpress 主题添加一些自定义。我正在尝试对商店页面进行风格化,以便在产品顶部或左侧有一个类别列表。当您从 woocommerce 管理设置中的目录页面启用类别选项时,它会利用 content_product_cat2.php 模板文件在 woocommerce 产品循环中显示产品类别缩略图。我已经移动了那个 php 文件并开始编辑它并获得结果。我已删除缩略图,但我无法将标题从网格格式或循环之外获取。我愿意为我的functions.php添加一些钩子,但我没有在woocommerce钩子参考中看到任何看起来像他们会做的钩子。可能是编辑此模板并使用挂钩将其放置在循环之前的组合。我不确定这就是我发帖的原因。 Here is a link to the page. 我正在使用它,这里是 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 只会在循环内进行编辑,我需要在循环之前显示我的类别,这样它们就不会落入循环的网格格式.....任何新想法的???

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    另一种选择是您可以禁用默认的 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>';
                            }
                        }
    ?>
    

    【讨论】:

    • 好吧,这听起来不错,但我有点困惑...我要将上述代码添加到哪个文件。
    • /woocommerce/templates/content-product.php 这是woocommerce用来生成产品网格的模板......
    • 非常感谢。我将它复制到 content-product.php 文件中,它没有显示类别。我在多个线路位置尝试过,但没有成功。我认为这是一个更好的解决方案,但我也没有让它发挥作用。
    • 您是否将此文件复制到 yourtheme/woocommerce/content-product.php?
    • 另外,根据您的版本,查看 loop-shop.php 文件以查看调用哪个模板来显示产品并复制其中的代码...
    【解决方案2】:

    你可以添加到你的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来移除最外层的item和list 参数为空字符串。您需要将输出包装在 自己的有序列表 (ol) 或无序列表(参见上面的示例)。 如果您根本不想要列表输出,请将 style 参数设置为 none。

    您也可以使用 get_categories http://codex.wordpress.org/Function_Reference/get_categories 代替 wp_list_categories 并自己编写 html 输出。这将与首先提供正确答案的Jay Bhatt 所做的相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      相关资源
      最近更新 更多