wordpress Woocommerce 自定义内容-product.php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16637034/
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
Woocommerce custom content-product.php
提问by Apalabrados
This is the basic code for the template content-product.php
这是模板 content-product.php 的基本代码
<li <?php post_class( $classes ); ?>>
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?>
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</a>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
The question is that I need to show much more information for every product in the main page. Here it only shows title, image, price and add to cart button. I need to be able to show, short description, attributes, custom fields, etc...
问题是我需要在主页上显示每个产品的更多信息。这里只显示标题、图片、价格和添加到购物车按钮。我需要能够显示、简短描述、属性、自定义字段等...
How can i accomplish this?
我怎样才能做到这一点?
Regards.
问候。
回答by Craig
It looks like you'll need to add some hooks.
看起来您需要添加一些钩子。
Here is a reference on how to do that: http://codex.wordpress.org/Function_Reference/add_action
这是有关如何执行此操作的参考:http: //codex.wordpress.org/Function_Reference/add_action
I'd suggest making a separate plugin that adds hooks to either 'woocommerce_after_shop_loop_item_title' or 'woocommerce_before_shop_loop_item_title'.
我建议制作一个单独的插件,为“woocommerce_after_shop_loop_item_title”或“woocommerce_before_shop_loop_item_title”添加钩子。
Then inside your custom functions, add the information you need.
然后在您的自定义函数中,添加您需要的信息。
Orbetter yet, I was looking at the source codefor this file, and it says you can simply override the whole file by copying this file and placing it in to your theme at: yourtheme/woocommerce/content-product.php
或者更好的是,我正在查看此文件的源代码,它说您可以通过复制此文件并将其放入您的主题中来简单地覆盖整个文件:yourtheme/woocommerce/content-product.php
That way you can just make the adjustments straight to that file.
这样您就可以直接对该文件进行调整。
To add the short description, you will want to use the the_excerpt()function provided by wordpress.
要添加简短描述,您需要使用wordpress 提供的the_excerpt()函数。
回答by Julius23
To add a short description I invite you to add this piece of code to the desired location:
要添加简短描述,我邀请您将这段代码添加到所需位置:
?>
<p class="xxx">
<?php echo $post->post_excerpt; ?>
</p>
<?php
For example to add it under the title just put it under
例如,将其添加到标题下,只需将其放在
do_action( 'woocommerce_after_shop_loop_item_title' );
And for the style I let you manage in css.
对于我让你在 css 中管理的样式。
Excuse my bad English.
原谅我的英语不好。
回答by Webimetry Solutions
You can copy php scripts from content-single-product.php and display them on the template content-product.php.It is better this ways as you will be able to control elements using HTML and CSS.
您可以从 content-single-product.php 复制 php 脚本并将它们显示在模板 content-product.php 上。这种方式更好,因为您将能够使用 HTML 和 CSS 控制元素。
回答by george
try this code. i customised the order of stuff so image is at the bottom rather then top but all variations will be shown. only thing i cant do is put two classes in a row rather then stacked just to save space but my php is limited. if anyone could add to this to customize into a better table , id love to hear about it.
试试这个代码。我自定义了东西的顺序,因此图像位于底部而不是顶部,但将显示所有变化。我唯一不能做的就是将两个类排成一行而不是堆叠起来以节省空间,但我的 php 是有限的。如果有人可以添加到此以自定义为更好的表,我很乐意听到它。
<?php
/**
* The template for displaying product content within loops.
*
* Override this template by copying it to yourtheme/woocommerce/content-product.php
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product, $woocommerce_loop;
// 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 );
}
// Ensure visibility
if ( ! $product || ! $product->is_visible() ) {
return;
}
// Increase loop count
$woocommerce_loop['loop']++;
// Extra post classes
$classes = array();
if ( 0 == ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 == $woocommerce_loop['columns'] ) {
$classes[] = 'first';
}
if ( 0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) {
$classes[] = 'last';
}
?>
<li <?php post_class( $classes ); ?>>
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<?php
/**
* remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
* remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
* remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
*/
do_action( 'woocommerce_single_product_summary' );
?>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
/**
* woocommerce_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* woocommerce_after_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_rating - 5
* @hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</a>
</li>