如何在Wordpress中输出自定义字段名称和值

问题描述 投票:0回答:2

我创建了一个自定义帖子类型,并向其中添加了一些自定义字段。

目前我的循环如下所示:

<?php
    //* The Query
    $exec_query = new WP_Query( array (
      'post_type' => 'jobs',
      'job_role'  => 'fryking',
      'posts_per_page' => 4,
      'order' => 'ASC'
    ) );

    //* The Loop
    if ( $exec_query->have_posts() ) {       
        while ( $exec_query->have_posts() ): $exec_query->the_post();
            echo '<div class="subcategory">';
            echo '<h3 class="cat_title">';
                the_title();
            echo '</h3>';?>
                <div class="cat_content"> 

                    <div class="left">
                        <?php the_content(); 
                            $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
                            the_field('hake_and_chips');
                        ?>
                    </div>                                  
                    <div class="right">
                        <?php 
                        echo '<div class="menu_image" style="background: url('. $url.')">';
                        echo '</div>';?>
                        </div>
                    </div>
            </div>         
                <?php           
                 endwhile;        

        //* Restore original Post Data
        wp_reset_postdata();
    }
?>

我设法使用此代码获取我的字段值:

the_field('hake_and_chips');

如何获取字段名称?

希望能帮到你

wordpress custom-post-type custom-fields
2个回答
1
投票

这些字段存储在 post 元表中,因此您也可以使用

get_post_meta
函数获取此自定义字段值。

尝试使用此代码来获取自定义字段的单个值:

echo get_post_meta($post->ID, 'hake_and_chips', true);

希望这对您有帮助。谢谢。


0
投票

你不需要

echo get_post_meta($post->ID, 'hake_and_chips', true);

相反,您可以尝试 Ian Dunn 此处提到的简单方法

echo $post->hake_and_chips
© www.soinside.com 2019 - 2024. All rights reserved.