php 编辑 woocommerce 模板文件以进行自定义布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15962352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:16:06  来源:igfitidea点击:

editing woocommerce template files for custom layout

phpwordpresswordpress-pluginwoocommerce

提问by Dave C

I am trying to add some customization to a Wordpress theme using the woocommerce plugin. I am trying to stylize the shop page to have a list of categories across the top of the products or along the left hand side. When you enable the category option from the catalog page in the woocommerce admin settings it utilizes the content_product_cat2.php template file to display product category thumbnails within the woocommerce product loop. I have moved that php file and have begun editing it and getting results. I have removed the thumbnails but I cannot get the titles out of the grid format or outside the loop. Im open to adding some hooks to my functions.php but I didnt see any hooks in the woocommerce hook reference that looked like they would do. Maybe a combination of editing this template and using a hook to place it before the loop. Im not sure thats why im posting. Here is a link to the page.that im using it on and here are the contents of the php file:

我正在尝试使用 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>

Thanks Guys...I know someone will have experience with this.

谢谢你们...我知道有人会有这方面的经验。

EDIT- Ok so what I realized is that editing the content_product_cat.php file is probably not the best way to go about it. Jay mentions below that it is better to edit the content_product.php file. While I initially thought this also to be a good solution what I realized is that it wont accomplish what im looking to do. Editing the content_product.php will only make edits within the loop and I need my categories to display before the loop so that they dont fall inot the grid format of the loop.....any new idea's???

编辑 - 好吧,我意识到编辑 content_product_cat.php 文件可能不是最好的方法。Jay 在下面提到最好编辑 content_product.php 文件。虽然我最初认为这也是一个很好的解决方案,但我意识到它不会完成我想要做的事情。编辑 content_product.php 只会在循环内进行编辑,我需要在循环之前显示我的类别,以便它们不会落入循环的网格格式......任何新想法?

回答by Jay Bhatt

One alternative is that you can disable the default woocommerce category view and don't use the template. Instead you can write custom code and get the category list and style it the way you want and add it to the woocommerce product grid. Below is how you do it.

一种替代方法是您可以禁用默认的 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>';
                        }
                    }
?>

回答by Bass Jobsen

You could add to your functions.php:

你可以添加到你的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();
}   

The function showcats is adopt from: http://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-category-list. Read more on: http://codex.wordpress.org/Template_Tags/wp_list_categories. To change the style of the list you will read:

showcats 功能来自:http://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-category-list 。阅读更多信息:http: //codex.wordpress.org/Template_Tags/wp_list_categories。要更改列表的样式,您将阅读:

You can remove the outermost item and list by setting the title_li parameter to an empty string. You'll need to wrap the output in an ordered list (ol) or unordered list yourself (see the examples above). If you don't want list output at all, set the style parameter to none.

您可以通过将 title_li 参数设置为空字符串来删除最外面的项目和列表。您需要自己将输出包装在有序列表 (ol) 或无序列表中(请参阅上面的示例)。如果您根本不需要列表输出,请将 style 参数设置为 none。

Instead of the wp_list_categories you could also use get_categories http://codex.wordpress.org/Function_Reference/get_categoriesand write the html output your self. This will be the same as done by Jay Bhattwho provide the right answer in the first place.

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