自定义WooCommerce字段的数据值在代码中显示,但在页面加载时不可见

时间:2021-10-13 作者:Dtorr1981

我有下面的代码(不是我自己写的),其中填充了li数据值-但是这不会显示在产品页面上的日期旁边,日期显示正确,但选择下拉列表时位置不会显示。

这比我所知的要高得多,函数php文件中有很多代码我不理解,所以我想知道是否有什么明显的东西?

使用此代码的示例页面为here

<div class="custom-meta-field">
    <div class="input-group">                                            
    <input type="hidden" id="datebox" class="form-control">
    <input type="hidden" id="locationbox" class="form-control">
    <div class="input-group-btn open">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="true">
            Select Booking Date
            <span class="fa fa-caret-down"></span>
        </button>
        <ul id="demolist" class="dropdown-menu">
                        <li data-value="Norwich, Norfolk" style="
    width: 100%;
"><a href="#">Sunday, 09 January 2022</a></li>
                        <li data-value="Leicester"><a href="#">Friday, 29 October 2021</a></li>
                    </ul>
    </div>
    </div>
    </div>
下面是php函数的代码。我可以看到这是在哪里生成的,但我不知道如何在页面上描绘位置,使其可见。

function custom_meta_option() {

    $_date1 = get_post_meta(get_the_ID(),\'_date1\', true);
    $_date2 = get_post_meta(get_the_ID(),\'_date2\', true);
    $_date3 = get_post_meta(get_the_ID(),\'_date3\', true);
    
    $_location1 = get_post_meta(get_the_ID(),\'_location1\', true);
    $_location2 = get_post_meta(get_the_ID(),\'_location2\', true);
    $_location3 = get_post_meta(get_the_ID(),\'_location3\', true);
    
    $product_id = get_the_ID();
    $status = get_post_meta( $product_id, \'_stock_status\', true );
    
    /*$terms = get_the_terms ( $product_id, \'product_cat\' );
    foreach ( $terms as $term ) {
         echo $cat_id = $term->term_id;
    }*/

    if($status == \'instock\' && !empty($_date1) || !empty($_date2) || !empty($_date3))
    {
    ?>    
    <div class="custom-meta-field">
    <div class="input-group">                                            
    <input type="hidden" ID="datebox" Class="form-control"></input>
    <input type="hidden" ID="locationbox" Class="form-control"></input>
    <div class="input-group-btn">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown">
            Select Booking Date
            <span class="fa fa-caret-down"></span>
        </button>
        <ul id="demolist" class="dropdown-menu">
            <?php if($_date1): ?>
            <li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_date1; ?></a></li>
            <?php endif; if($_date2):?>
            <li data-value="<?php echo $_location2; ?>"><a href="#"><?php echo $_date2; ?></a></li>
            <?php endif; if($_date3):?>
            <li data-value="<?php echo $_location3; ?>"><a href="#"><?php echo $_date3; ?></a></li>
            <?php endif; ?>
        </ul>
    </div>
    </div>
    </div>
    <?php
    }
    //echo \'<p>No dates currently available</p>\';
}

1 个回复
最合适的回答,由SO网友:tiago calado 整理而成

the data-value doesnt show, is to use. if you want to show it, it should be inside the a tag, once it is php code with variables must be inside the php "tags"

so should be something like this

<li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_date1.\' \'.$_location1; ?></a></li>

this will show this "Friday, 29 October 2021 Leicester" if you want first the location can be done like this

<li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_location1.\' - \'.$_date1; ?></a></li>

this will show this "Leicester - Friday, 29 October 2021" I had put a \' - \' because seems better. However its a choise.

the inteire code with the last example should be this one


    $_date1 = get_post_meta(get_the_ID(),\'_date1\', true);
    $_date2 = get_post_meta(get_the_ID(),\'_date2\', true);
    $_date3 = get_post_meta(get_the_ID(),\'_date3\', true);
    
    $_location1 = get_post_meta(get_the_ID(),\'_location1\', true);
    $_location2 = get_post_meta(get_the_ID(),\'_location2\', true);
    $_location3 = get_post_meta(get_the_ID(),\'_location3\', true);
    
    $product_id = get_the_ID();
    $status = get_post_meta( $product_id, \'_stock_status\', true );
    
    /*$terms = get_the_terms ( $product_id, \'product_cat\' );
    foreach ( $terms as $term ) {
         echo $cat_id = $term->term_id;
    }*/

    if($status == \'instock\' && !empty($_date1) || !empty($_date2) || !empty($_date3))
    {
    ?>    
    <div class="custom-meta-field">
    <div class="input-group">                                            
    <input type="hidden" ID="datebox" Class="form-control"></input>
    <input type="hidden" ID="locationbox" Class="form-control"></input>
    <div class="input-group-btn">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown">
            Select Booking Date
            <span class="fa fa-caret-down"></span>
        </button>
        <ul id="demolist" class="dropdown-menu">
            <?php if($_date1): ?>
            <li data-value="<?php echo $_location1; ?>"><a href="#"><?php echo $_location1.\' - \'.$_date1; ?></a></li>
            <?php endif; if($_date2):?>
            <li data-value="<?php echo $_location2; ?>"><a href="#"><?php echo $_location2.\' - \'.$_date2; ?></a></li>
            <?php endif; if($_date3):?>
            <li data-value="<?php echo $_location3; ?>"><a href="#"><?php echo $_location3.\' - \'.$_date3; ?></a></li>
            <?php endif; ?>
        </ul>
    </div>
    </div>
    </div>
    <?php
    }
    //echo \'<p>No dates currently available</p>\';
}

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: