【问题标题】:Wordpress - Displaying content from custom postsWordpress - 显示来自自定义帖子的内容
【发布时间】:2014-03-20 17:11:10
【问题描述】:

我正在使用高级自定义字段并为推荐设置了自定义帖子类型,在推荐页面上有一个关系字段 (display_on_page),您可以在其中选择在哪个页面上显示推荐。

问题是,在选择页面

时,推荐显示everypage

请问有人知道我在下面做错了什么吗?

<?php query_posts( 'post_type=Testimonial'); ?>
    <?php while ( have_posts() ) : the_post(); ?>

        <?php $posts = get_field('display_on_page');

        if( $posts ): ?>
            <?php the_field('the_testimonial'); ?>

            <?php wp_reset_postdata();  ?>
        <?php endif; ?>

    <?php endwhile; ?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    如果我理解正确,您应该检查您从get_field('display_on_page') 获得的值是否与当前页面 ID 匹配。 (我假设页面 ID 是您创建的自定义字段存储的内容)。

    如果是这样,你可以query your posts filtering by custom field values,像这样:

    <?php 
    
    // Fetches current page ID, assuming that this is page.php or similar
    $current_page_id = get_the_ID();
    
    query_posts(
        'post_type=Testimonial',
        'meta_key' => 'display_on_page',
        'meta_value' => $current_page_id // Only gets Testimonials that are set for this page
    ); 
    
    while ( have_posts() ) : the_post(); 
        the_field('the_testimonial');
    endwhile;
    
    // Resets default WP Post Data *AFTER* the loop is complete
    wp_reset_postdata(); 
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多