Php 编辑自定义布局的模板文件
我正在尝试使用woocommerce插件为Wordpress主题添加一些定制。我正在尝试将商店页面样式化,以便在产品顶部或左侧有一个类别列表。当您从woocommerce管理设置中的catalog页面启用category选项时,它将利用content\u product\u cat2.php模板文件在woocommerce产品循环中显示产品类别缩略图。我已经移动了那个php文件,并开始编辑它并得到结果。我已经删除了缩略图,但我无法将标题从网格格式或循环外取出。我很乐意在functions.php中添加一些钩子,但在woocommerce钩子引用中,我没有看到任何钩子看起来会这样做。可能是编辑此模板和使用挂钩将其放置在循环之前的组合。我不确定这就是我发帖的原因。我正在使用它,下面是php文件的内容:Php 编辑自定义布局的模板文件,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试使用woocommerce插件为Wordpress主题添加一些定制。我正在尝试将商店页面样式化,以便在产品顶部或左侧有一个类别列表。当您从woocommerce管理设置中的catalog页面启用category选项时,它将利用content\u product\u cat2.php模板文件在woocommerce产品循环中显示产品类别缩略图。我已经移动了那个php文件,并开始编辑它并得到结果。我已经删除了缩略图,但我无法将标题从网格格式或循环外取出。我很乐意在functions.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>
另一种选择是,您可以禁用默认的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类别视图,而不使用模板。相反,您可以编写自定义代码,获取类别列表,并按照您想要的方式设置其样式,然后将其添加到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();
}
add_action('woocommerce_-before_-shop_-loop','showcats');
函数showcats()
{
//使用wp_list_categories列出给定分类法中的术语(如果使用PHP代码插件,也可用作小部件)
$taxonomy='product_cat';
$orderby='name';
$show_count=0;//1表示是,0表示否
$pad_counts=0;//1表示是,0表示否
$hierarchy=1;//1表示是,0表示否
$title='';
$args=数组(
“分类法”=>$taxonomy,
'orderby'=>$orderby,
'show_count'=>$show_count,
“pad_计数”=>$pad_计数,
“分层”=>$hierarchical,
“title_li”=>$title
);
?>
您可以在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();
}
add_action('woocommerce_-before_-shop_-loop','showcats');
函数showcats()
{
//使用wp_list_categories列出给定分类法中的术语(如果使用PHP代码插件,也可用作小部件)
$taxonomy='product_cat';
$orderby='name';
$show_count=0;//1表示是,0表示否
$pad_counts=0;//1表示是,0表示否
$hierarchy=1;//1表示是,0表示否
$title='';
$args=数组(
“分类法”=>$taxonomy,
'orderby'=>$orderby,
'show_count'=>$show_count,
“pad_计数”=>$pad_计数,
“分层”=>$hierarchical,
“title_li”=>$title
);
?>
好的,听起来不错,但我有点困惑…我要将上述代码添加到哪个文件中。/woocommerce/templates/content-product.php这是woocommerce用来生成产品网格的模板…非常感谢。我将其复制到content-product.php文件中,但它没有显示类别。我在多行位置尝试了它,没有s成功。我认为这是一个更好的解决方案,但我也没有让它发挥作用。你是否将此文件复制到你的Theme/woocommerce/content product.php?还取决于你的版本,查看loop-shop.php文件,查看调用哪个模板来显示产品并复制其中的代码…好的,听起来不错,但我有点困惑…我是什么文件将上述代码添加到./woocommerce/templates/content-product.php这是woocommerce用来生成产品网格的模板……非常感谢。我将其复制到content-product.php文件中,但它没有显示类别。我在多行位置尝试了它,但没有成功。我认为这是一个更好的解决方案,但我没有得到我想要的t也不起作用。您是否将此文件复制到yourtheme/woocommerce/content product.php?还可以根据您的版本查看loop-shop.php文件,查看调用哪个模板来显示产品并复制其中的代码。。。