我正在使用WPMUDev的MarketPress for WordPress,我无法弄清楚如何根据图片是否缺货来为图像添加类。
我有工作代码,如果产品缺货则返回,这不是问题。
我希望使用以下逻辑将产品列表页面上的CSS类应用到每个产品的缩略图:
如果$ stock == 0则应用$ cssClass1,否则应用$ cssClass2。
MarketPress似乎使用以下内容生成图像缩略图和产品列表:
if (!function_exists('mp_product_image')) :
/*
* Displays the product featured image
*
* @param bool $echo Optional, whether to echo
* @param string $context Options are list, single, or widget
* @param int $post_id The post_id for the product. Optional if in the loop
* @param int $size An optional width/height for the image if contect is widget
* @param string $align The alignment of the image. Defaults to settings.
*/
function mp_product_image($echo = true, $context = 'list', $post_id = NULL, $size = NULL, $align = NULL) {
global $id, $mp;
$post_id = ( NULL === $post_id ) ? $id : $post_id;
// Added WPML
$post_id = apply_filters('mp_product_image_id', $post_id);
$post = get_post($post_id);
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$class = $title = $link = '';
$img_classes = array('mp_product_image_' . $context, 'photo', StockCheck::fetchNewStock($imgGGClass));
if ( !is_null($align) )
$align = 'align' . $align;
if ($context == 'list') {
//quit if no thumbnails on listings
if (!$mp->get_setting('show_thumbnail'))
return '';
//size
if (intval($size)) {
$size = array(intval($size), intval($size));
} else {
if ($mp->get_setting('list_img_size') == 'custom')
$size = array($mp->get_setting('list_img_width'), $mp->get_setting('list_img_height'));
else
$size = $mp->get_setting('list_img_size');
}
//link
$link = get_permalink($post_id);
$title = esc_attr($post->post_title);
$class = ' class="mp_img_link"';
$img_classes[] = is_null($align) ? $mp->get_setting('image_alignment_list') : $align;
} else if ($context == 'single') {
//size
if ($mp->get_setting('product_img_size') == 'custom')
$size = array($mp->get_setting('product_img_width'), $mp->get_setting('product_img_height'));
else
$size = $mp->get_setting('product_img_size');
//link
$temp = wp_get_attachment_image_src($post_thumbnail_id, 'large');
$link = $temp[0];
if ($mp->get_setting('disable_large_image')) {
$link = '';
$title = esc_attr($post->post_title);
} else {
$title = __('View Larger Image »', 'mp');
}
$class = ' class="mp_product_image_link mp_lightbox"';
$img_classes[] = is_null($align) ? $mp->get_setting('image_alignment_single') : $align;
//in case another plugin is loading lightbox
if ($mp->get_setting('show_lightbox')) {
$class .= ' rel="lightbox"';
wp_enqueue_script('mp-lightbox');
}
} else if ($context == 'widget') {
//size
if (intval($size))
$size = array(intval($size), intval($size));
else
$size = array(50, 50);
//link
$link = get_permalink($post_id);
$title = esc_attr($post->post_title);
$class = ' class="mp_img_link"';
}
$image = get_the_post_thumbnail($post_id, $size, array('itemprop' => 'image', 'class' => implode(' ', $img_classes), 'title' => $title));
if (empty($image) && $context != 'single') {
if (!is_array($size)) {
$size = array(get_option($size . "_size_w"), get_option($size . "_size_h"));
}
$img_classes[] = 'wp-post-image';
$image = '
<div itemscope class="hmedia">
<span class="fn">' . get_the_title(get_post_thumbnail_id()) . '</span>
<img width="' . $size[0] . '" height="' . $size[1] . '" itemprop="image" title="' . esc_attr($title) . '" class="' . implode(' ', $img_classes) . '" src="' . apply_filters('mp_default_product_img', $mp->plugin_url . 'images/default-product.png') . '" />
</div>';
}
//force ssl on images (if applicable) http://wp.mu/8s7
if ( is_ssl() ) {
$image = str_replace('http://', 'https://', $image);
}
//add the link
if ($link) {
$image = '
<div itemscope class="hmedia">
<div style="display:none"><span class="fn">' . get_the_title(get_post_thumbnail_id()) . '</span></div>
<a rel="lightbox enclosure" id="product_image-' . $post_id . '"' . $class . ' href="' . $link . '">' . $image . '</a>
</div>';
}
$image = apply_filters('mp_product_image', $image, $context, $post_id, $size);
if ($echo)
echo $image;
else
return $image;
}
endif;
我尝试了以下内容(StockCheck位根据股票价值将变量添加到变量中):
$img_classes = array('mp_product_image_' . $context, 'photo', StockCheck::fetchNewStock($imgGGClass));
但是,上述内容仅适用于列表页面上所有缩略图的一个或另一个类别,根据项目是否有货而不适用。
任何朝着正确方向的推动都将非常感激。 感谢