Link Post Thumbnail Image beim automatischen Erstellen von WooCommerce -Produkt aus einem WordPress -BeitragPhp

PHP-Programmierer chatten hier
Anonymous
 Link Post Thumbnail Image beim automatischen Erstellen von WooCommerce -Produkt aus einem WordPress -Beitrag

Post by Anonymous »

Ich versuche, eine Fuktion zu erstellen, die programmatisch ein Produkt erstellt, wenn ein Beitrag erstellt wird. Ich kann nicht herausfinden, wie man das Miniaturbild des Beitrags mit dem Produktbild verbindet. (Wenn ich das Bild dem Postinhalt anhege, wird es in der Produktbeschreibung angezeigt, aber das wird nicht so eingestellt Künstler, der WordPress sowohl für ein Portfolio -Archiv als auch für eine E -Commerce -Plattform verwendet. Es ist ewig her, seit ich mich in den WordPress -Code eingeteilt habe und WC noch komplizierter ist. Benutzerdefinierte Beitragstyp wird veröffentlicht < /p>
Dies ist der aktuelle Code, der Produkte erstellt und veröffentlichen, die Bild -ID jedoch nicht mit dem Produkt verknüpft: < /p>

Code: Select all

/* create product on post -- */

add_action( 'publish_post', 'auto_create_product_from_post', 100, 2 );
function auto_create_product_from_post($id, $post){
// Create an empty instance of the WC_Product
$product = new WC_Product_Simple(); // set_name( $post->post_title );
$product->set_status( $post->post_status );
$product->set_description( $post->post_content ); // (optional)
$product->set_short_description( $post->post_excerpt ); // (optional)
$product->set_gallery_image_ids(21741); // sets the 'back of print' example image to product gallery
//  $product->set_image_id( xxx ?what variable? xxx ); // the part that's stumping me

// You can use any available WC_Product methods to set other properties
// see: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

$product->save(); // Save (publish) new product
}
< /code>
Ich habe es ausprobiert, ohne Erfolg: < /p>
$image = get_post_thumbnail_id( $post->ID );
< /code>
$image = get_attached_media( 'image', $post->ID );

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post