【问题标题】:Add custom filed to Wordpress将自定义字段添加到 Wordpress
【发布时间】:2020-02-06 08:32:27
【问题描述】:

我正在尝试将自定义字段添加到我的 Wordpress 页面,但它不起作用。当我设置它的值时,HTML 上没有任何反应。

我阅读了 Wordpress 文档并尝试按照其中的步骤操作,但出现了问题。 它正在工作的所有其他内容,例如 the_title()、the_post_thumbnail()...只是这个自定义字段我没有工作:(

https://wordpress.org/support/article/custom-fields/ https://codex.wordpress.org/Function_Reference/register_post_type

自定义字段:

函数.php

$supports = array (

'title',
'editor',
'thumbnail',
'custom-fields'

);

HTML

<?php  

     $args = array ('post_type' => 'produtos');
          $loop = new WP_Query( $args );

                  if ($loop->have_posts() ) {
                          while ($loop->have_posts() ) {
                            $loop->the_post();
                          ?>

    <div class="col-xl-2 col-lg-3 col-md-3 col-sm-4 col-md-3 col-12">
        <div class="produtos-head">
            <div class="img-fluid produtos-img">
                <?php the_post_thumbnail();  ?>
            </div>
        </div>
        <div class="produtos-titulo d-flex align-items-center justify-content-center">
            <?php the_title(); ?>

        </div>
        <div class="preco-original"> Price:R$
            <?php $original_price = get_post_meta($post->ID, 'original_price', true);
                                                if($original_price){ ?>
                <p>
                    <? echo $original_price; ?>
                </p>
                <?php 

  }else{ 

  }
?>
        </div>
        <div class="preco-promocional"> <span> Sale: </span>
            <span class="preco-promocional-number"> $ </span>

        </div>
        <button class="btn-vendedor"> Contact </button>
    </div>

    <?php
                }
              }
               ?>

        </div>
        </div>

【问题讨论】:

    标签: php wordpress custom-fields


    【解决方案1】:

    使用get_the_ID()代替$post-&gt;ID,需要将&lt;? echo $original_price; ?&gt;改为&lt;?php echo $original_price; ?&gt;

    【讨论】:

    • &lt;? echo&lt;?php echo 不是必需的,但建议使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多