【问题标题】:ACF Wordpress & sort category archive pageACF Wordpress 和排序类别存档页面
【发布时间】:2015-05-14 13:38:56
【问题描述】:

我正在使用 acf wordpress 插件将布尔字段 (featured_project) 添加到自定义帖子(项目)。

我正在尝试对分类存档页面上的帖子进行排序,以显示顶部有特色的帖子和底部没有特色的帖子。

同一类型的帖子有多个类别。

我已经阅读了一些其他类似的问题,其中的解决方案是使用 'wp_query' 或 pre_get_posts,但我似乎无法让它工作。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php if ( get_field('featured_project') ) { ?>
        <article class='project featured' id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article">
        <a href='<?php the_permalink(); ?>'>
           <h3 class="h2"><?php the_title(); ?></h3>

           <?php    
                $url =  wp_get_attachment_url( get_post_thumbnail_id() );      
                $width = 300;                                                                  
                $height = 200;                                                                 
                $crop = true;                                                                 
                $retina = false;                                                             

                // Call the resizing function (returns an array)
                $image = matthewruddy_image_resize( $url, $width, $height, $crop, $retina ); 

           ?> 
           <img src='<?php echo $image['url']; ?>'/ alt='<?php the_title(); ?>'>
        </a>    
        <?php // the_post_thumbnail( 'projects-full', false ); ?> 

        <div class='excerpt'><?php the_excerpt(); ?></div>
        </article>
        <?php } else { ?>

是分类文件中代码的一部分。

谢谢

更新:还没有找到解决方案,其他人可以加入吗?

【问题讨论】:

  • 你试过查看 get_field('featured_project') 的值吗?
  • 是的,但我看到的唯一方法是在循环内,或者在循环外传递帖子 ID。
  • 是的,但你确定你的if 语句正在工作就是我所说的,你想让 get_field... 返回 true 或 1,它会给你吗?
  • 啊,抱歉,是的,我得到每个帖子的预期 1 或 0。
  • 好的,你有打印的东西,只是顺序不对?

标签: php wordpress advanced-custom-fields


【解决方案1】:

好吧,问题和解决方案不在这段代码上……问题出在查询中, 要解决此问题,您还应该查询元键

<?php
$args = array_merge( $query, 
    array( 
        'meta_query' => array(
        'relation' => 'AND',
            array(
                'key' => 'featured_project',
                'value' => 1, // test if your value is a number or string
                //'type' => 'BINARY', //Maybe you should cast the meta value
                'compare' => '='
                ),
            ),
        ) 
    );
    $custom_query = new WP_Query( $args ); ?>
?

<?php if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
    <?php if ( get_field('featured_project') ) { ?>

也许您还应该在查询中添加 orderby(类似于 'orderby' =&gt; ' meta_value_num date'

【讨论】:

  • 谢谢,但我收到“警告:array_merge():参数 #1 不是数组”,这与 $args = array_merge($query,当我删除 $query 时消失但帖子消失了。
  • 转储 $query 时会得到什么? echo "&lt;pre&gt;"; var_dump($query,"Hello"); echo "&lt;/pre&gt;";die;
  • 它只是输出为NULL
  • $query = 应该做什么?
  • -.- 你能提供你的代码吗?另请阅读:codex.wordpress.org/Class_Reference/WP_Query
猜你喜欢
  • 1970-01-01
  • 2019-01-26
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 2018-07-11
  • 1970-01-01
相关资源
最近更新 更多