shortcode – Variabel global tidak ditampilkan di shotcode

Saya mencoba menampilkan beberapa variabel ketika saya mengarahkan kursor ke gambar produk. Untuk itu saya menggunakan kode pendek untuk menampilkan variabel yang saya perlukan dan fungsi do_shortcode(). Namun variabel yang diperlukan tidak muncul ketika saya mengarahkan kursor ke produk, sedangkan ketika saya memposting kode pendek di deskripsi produk misalnya memberikan hasil yang diharapkan. Dalam kode ini Anda dapat melihat variabel yang saya perlukan $theHeight didefinisikan sebagai global tetapi kosong ketika dipanggil dengan kode pendek di snipet php (menggunakan snipet kode WP). Adakah yang tahu cara memperbaikinya. Saya sebenarnya sangat baru dalam pengembangan php dan wordpress.

add_action( 'woocommerce_before_add_to_cart_form', 'get_iptc_info', 10 ); 
function get_iptc_info(){
    global $product;
    global $theHeight;
    $image_id = $product-> get_image_id();
    $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    $exif = exif_read_data($image_url, 'IFD0');
    echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";
    $exif = exif_read_data($image_url, 0, true);
    echo $image_url."<br />\n";
    foreach ($exif as $key => $section) {
        foreach ($section as $name => $val) {
            //echo "$key.$name: $val<br />\n";
            if($key.$name==="COMPUTED"."Height") {
                echo "$name: $val<br />\n";
                $theHeight =$val;
            }
        }
    }
}

dan ini kode pendeknya

add_shortcode( 'my_show_variable_shortcode', 'iptc_shortcode' );
function iptc_shortcode() {
    global $theHeight;
    return $theHeight;
}

dan terakhir ini adalah kode untuk menampilkan variabel pada gambar produk yang diarahkan

<?php
add_action( 'woocommerce_before_shop_loop_item', 'wc_add_customdesc' );
    global $theHeight;
function wc_add_customdesc() {
    global $product, $theHeight;
    $creationDate = get_field( 'picture_date', $product->get_id() ); 
    $authorName = get_field( 'author', $product->get_id() ); 
    $title = $product->get_title(); 
    $price = $product->get_price();
    $myDescript = $product->post-> post_excerpt;
    ?>
    <div class="descShow" itemprop="description">
        <?php echo '<p class="thedataclass">' .'The shooting description is '. $myDescript . '</p>'; ?>
        <?php echo '<p class="thedataclass">' .'The shooting date is '. $creationDate . '</p>'; ?>
        <?php echo '<p class="thedataclass">' .'The auhtor is '. $authorName . '</p>'; ?>
        <?php echo '<p class="thedataclass">' .'The title is '. $title . '</p>'; ?>
        <?php echo do_shortcode('[my_show_variable_shortcode]');?>
    </div>
<style>
    .thedataclass{
        margin:10px 5px;
    }
    .descShow{
        max-height: 0px;
        white-space:wrap;
        transition: max-height 0.5s ease-out;
        overflow: hidden;
        position: absolute;
        z-index:2;
        color:white;
        line-height:20px;
        background-color: rgba(65,65,65,0.6);
        opacity:0%;
    }
        
    li.product:hover .descShow{
        justify-content:left;
        vertical-align:center;
        white-space:wrap;
        line-height:20px;
        padding-left:5px;
        max-height: 100%;
        max-width: 100%;
        min-height:100%;
        min-width:100%;
        opacity:90%;
        transition: all 0.5s ease-in-out;
    }
            
    @media only screen and (max-width: 922px) {
        li.product:hover .descShow{
        line-height:15px;
        }
    }
    @media only screen and (max-width: 545px){
        li.product:hover .descShow{
        line-height:20px;
        }
    }
</style>

<?php

}

Leave a Reply

Your email address will not be published. Required fields are marked *