如何在woocommerce中获得产品的特色图像

时间:2015-09-29 07:19:11

标签: wordpress woocommerce

请告诉我哪里出错了。产品特色图片未显示。

   $args = array( 'post_type' => 'product', 'posts_per_page' => 80, 'product_cat' => 'profiler', 'orderby' => 'rand' );

    $loop = new WP_Query( $args );

   while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

    <div class="dvThumb col-xs-4 col-sm-3 col-md-3 profiler-select profiler<?php echo the_title(); ?>" data-profile="<?php echo $loop->post->ID; ?>">

    <img src="<?php  get_the_post_thumbnail($loop->post->ID); ?>" data-id="<?php echo $loop->post->ID; ?>">

    <p><?php the_title(); ?></p>

    <span class="price"><?php echo $product->get_price_html(); ?></span>                    
    </div>

我已在后端添加了精选图片

8 个答案:

答案 0 :(得分:39)

我得到了解决方案。 我试过这个。

- (void)downloadItems
{
    // Download the json file
    NSURL *jsonFileUrl = [NSURL URLWithString:@"http://myhost.com/test.php"];

    // Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

    // Create the NSURLConnection
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Initialize the data object
    _downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the newly downloaded data
    [_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Create an array to store the locations
    NSMutableArray *_locations = [[NSMutableArray alloc] init];

    // Parse the JSON that came in
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

    // Loop through Json objects, create question objects and add them to our questions array
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSDictionary *jsonElement = jsonArray[i];

        // Create a new location object and set its props to JsonElement properties
        Location *newLocation = [[Location alloc] init];
        newLocation.idS = jsonElement[@"idStatistic"];
        newLocation.temp = jsonElement[@"temp"];
        newLocation.hum = jsonElement[@"hum"];
        newLocation.date_time = jsonElement[@"date_time"];

        // Add this question to the locations array
        [_locations addObject:newLocation];
    }

    // Ready to notify delegate that data is ready and pass back items
    if (self.delegate)
    {
        [self.delegate itemsDownloaded:_locations];
    }
}

答案 1 :(得分:3)

get_the_post_thumbnail函数返回html而不是特色图片的网址。您应该使用get_post_thumbnail_id获取精选图片的帖子ID,然后使用wp_get_attachment_image_src获取精选图片的网址。

试试这个:

<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 80, 'product_cat' => 'profiler', 'orderby' => 'rand' );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
    <div class="dvThumb col-xs-4 col-sm-3 col-md-3 profiler-select profiler<?php echo the_title(); ?>" data-profile="<?php echo $loop->post->ID; ?>">
        <?php $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($loop->post->ID)); ?>
        <?php if($featured_image) { ?>
        <img src="<?php $featured_image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">
        <?php } ?>
        <p><?php the_title(); ?></p>
        <span class="price"><?php echo $product->get_price_html(); ?></span>
    </div>
<?php endwhile; ?>

答案 2 :(得分:3)

我只会使用sudo make -f Makefile_ubuntu /usr/bin/i686-w64-mingw32-g++-win32 -m32 -std=gnu++11 -o ../bin/tandem.exe tandem.o p3mprocess.o saxmzdatahandler.o mspectrumcondition.o masscalc.o mprocess.o mreport.o mscore_tandem.o loadmspectrum.o mplugin.o msequenceserver.o saxtaxhandler.o msequencecollection.o mscore.o mrefine.o xmltaxonomy.o mbiomlreport.o saxtandeminputhandler.o saxhandler.o msequtilities.o base64.o saxmodhandler.o mtermmods.o xmlparameter.o saxsaphandler.o saxmzxmlhandler.o saxmzmlhandler.o mxxcleavage.o p3msequenceserver.o mzid_report.o saxbiomlhandler.o p3.o mpmods.o saxgamlhandler.o stdafx.o MSNumpress.o mpam.o -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a saxhandler.o:saxhandler.cpp:(.text+0x100): undefined reference to `XML_ParserCreate' saxhandler.o:saxhandler.cpp:(.text+0x11d): undefined reference to `XML_SetUserData' saxhandler.o:saxhandler.cpp:(.text+0x13b): undefined reference to `XML_SetElementHandler' saxhandler.o:saxhandler.cpp:(.text+0x151): undefined reference to `XML_SetCharacterDataHandler' saxhandler.o:saxhandler.cpp:(.text+0x1cf): undefined reference to `XML_ParserFree' saxhandler.o:saxhandler.cpp:(.text+0x344): undefined reference to `XML_Parse' saxhandler.o:saxhandler.cpp:(.text+0x37f): undefined reference to `XML_Parse' saxhandler.o:saxhandler.cpp:(.text+0x3bd): undefined reference to `XML_GetErrorCode' saxhandler.o:saxhandler.cpp:(.text+0x3d4): undefined reference to `XML_GetCurrentLineNumber' collect2: error: ld returned 1 exit status Makefile_ubuntu:33: recipe for target '../bin/tandem.exe' failed make: *** [../bin/tandem.exe] Error 1 代替get_the_post_thumbnail_url()

get_the_post_thumbnail()

答案 3 :(得分:2)

在WC 3.0+版本中,图像可以通过以下代码获得。

$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $item->get_product_id() ), 'single-post-thumbnail' );
echo $image_url[0]

答案 4 :(得分:2)

我做到了,效果很好

<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
<?php } ?>

答案 5 :(得分:2)

我遇到了同样的问题,并通过使用默认的woocommerce挂钩显示产品图片来解决了该问题。

while ( $loop->have_posts() ) : $loop->the_post();
   echo woocommerce_get_product_thumbnail('woocommerce_full_size');
endwhile;

可用参数:

  • woocommerce_thumbnail
  • woocommerce_full_size

答案 6 :(得分:1)

这应该可以解决问题。经过测试和工作。

<?php
    $product_meta = get_post_meta($post_id);
    echo wp_get_attachment_image( $product_meta['_thumbnail_id'][0]), 'full' );
?>

您可以根据需要更改参数。

答案 7 :(得分:0)

这里的答案太复杂了。这是我最近使用过的东西:

<?php global $product; ?>
<img src="<?php echo wp_get_attachment_url( $product->get_image_id() ); ?>" />

使用wp_get_attachment_url()显示