【问题标题】:Wordpress loop - get value of custom fields from a custom post type in a categoryWordpress 循环 - 从类别中的自定义帖子类型获取自定义字段的值
【发布时间】:2018-12-29 04:22:06
【问题描述】:

我正在创建一个页面,该页面通过自定义帖子类型的帖子循环访问该自定义帖子的类别(使用目录引擎主题)

我尝试使用 wp_query 并且能够获得标题,但没有任何其他字段 (在functions.php中创建一个短代码)

add_shortcode("posts_by_category", "shortcode_posts_by_category");
function shortcode_posts_by_category() {
  $query = new WP_Query( array(
    'post_type' => 'place',  // name of post type.
    'tax_query' => array(
        array(
            'taxonomy' => 'place_category',   // taxonomy name
            'field' => 'term_id',           // term_id, slug or name
            'terms' => 94,             // term id, term slug or term name
        )
    )
) );
while ( $query->have_posts() ) : $query->the_post();
?>
<article class="boc">
    <div class="boc__wrapper">
        <?php echo get_the_post_thumbnail_url($post_id, 'thumbnail'); ?>
    </div>
    <div class="boc__wrapper">
        <h3><?php echo the_title(); ?></h3>
        <div class="content-description">
            <p><?php echo get_post_field('post_content', $post_id); ?></p>
        </div>
    </div>
</article>
<?php
endwhile; 
wp_reset_query();}

我注意到正在使用的主题有一个用于创建自定义帖子类型的自定义对象,所以我尝试访问它但没有运气$ae_post_factory

add_shortcode("posts_by_category", "shortcode_posts_by_category");
function shortcode_posts_by_category() {
$query = new WP_Query( array(
    'post_type' => 'place',  // name of post type.
    'tax_query' => array(
        array(
            'taxonomy' => 'place_category',   // taxonomy name
            'field' => 'term_id',           // term_id, slug or name
            'terms' => 94,             // term id, term slug or term name
        )
        )
    )
);
while ( $query->have_posts() ) : $query->the_post();
global $ae_post_factory, $user_ID;
$place_obj = $ae_post_factory->get('place');
$address = get_post_meta($query->post->ID, 'address', true);
?>
    <article class="boc">
        <div class="boc__wrapper">
        <?php echo get_the_post_thumbnail_url($post_id, 'thumbnail'); ?>
    </div>
    <div class="boc__wrapper">
        <h3><?php echo the_title(); ?></h3>
        <div class="content-description">
            <p><?php echo $address ?></p>
        </div>
    </div>
</article>
<?php
endwhile; 
wp_reset_query();}

我转储了$place_obj,这就是我得到的 (抱歉格式化 - 添加数组作为代码 sn-p 不起作用 0:))

AE_Posts Object (
  [meta] => Array (
    [0] => address
    [1] => avatar
    [2] => post_count
    [3] => comment_count
    [4] => et_featured
    [5] => et_expired_date
    [6] => et_paid
    [7] => et_phone
    [8] => et_emailaddress
    [9] => et_url
    [10] => et_fb_url
    [11] => et_google_url
    [12] => et_twitter_url
    [13] => et_video
    [14] => et_full_location
    [15] => et_location_lat
    [16] => et_location_lng
    [17] => open_time
    [18] => close_time
    [19] => serve_day
    [20] => serve_time
    [21] => cover_image
    [22] => cover_image_url
    [23] => video_position
    [24] => reject_message
    [25] => et_ad_order
    [26] => rating_score
    [27] => rating_score_comment
    [28] => reviews_count
    [29] => et_payment_package
    [30] => et_featured
    [31] => et_claimable
    [32] => et_claim_approve
    [33] => et_claim_info
  )
  [current_post] => 
  [current_main_post] => 
  [wp_query] => 
  [post_type] => place
  [taxs] => Array (
    [0] => place_category
    [1] => location
  )
  [convert] => Array (
    [0] => post_parent
    [1] => post_title
    [2] => post_name
    [3] => post_content
    [4] => post_excerpt
    [5] => post_author
    [6] => post_status
    [7] => ID
    [8] => post_type
    [9] => comment_count
    [10] => guid
  )
  [localize] => Array (
  )
)

我希望做什么

我需要访问“地点”自定义帖子类型并创建一个查询循环,在该自定义帖子类型中按类别显示 5 个帖子。然后输出自定义字段,如标题、地址、帖子内容、特色图片等。

非常感谢您的时间。我已经为此苦苦挣扎了一段时间。谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    除了元字段值之外,您可以获取默认情况下带有 Post 的所有字段。我在您的代码中发现了一个小问题,您正在尝试获取特色图片,

    &lt;?php echo get_the_post_thumbnail_url($post_id, 'thumbnail'); ?&gt;

    &lt;?php echo get_the_post_thumbnail_url('thumbnail'); ?&gt;

    在循环内部,您不需要帖子 id 作为参数,即使函数也不接受 id 作为参数。该函数将仅打印 url。您需要将其设置为图像标签。如下所示:

    <img src="<?php echo get_the_post_thumbnail_url('thumbnail'); ?>">
    

    如果您需要获取帖子元值,您可以使用以下代码作为参考:

    $variable_name = get_post_meta(get_the_ID(), '<field_name>', true);
    

    希望这会对你有所帮助。

    【讨论】:

    • 你也不需要用 the_title() 写回显它会自动回显标题。
    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2016-10-28
    • 2019-05-12
    • 2011-06-24
    • 1970-01-01
    相关资源
    最近更新 更多