将自定义字段添加到Wordpress

时间:2020-02-05 17:59:05

标签: php wordpress custom-fields

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

我阅读了Wordpress文档,尝试按照那里的步骤操作,但是出了点问题。 其他所有有效的东西,例如the_title(),the_post_thumbnail()...只是这个自定义字段,我无法使用:(

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

自定义字段:

Custom Field:

Functions.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>

1 个答案:

答案 0 :(得分:2)

使用get_the_ID()代替$post->ID,并且需要将<? echo $original_price; ?>更改为<?php echo $original_price; ?>