【问题标题】:ACF CPT two columns with each categoryACF CPT 每个类别两列
【发布时间】:2015-04-09 15:11:41
【问题描述】:

我创建了自定义帖子类型并通过高级自定义字段添加了一些内容。 通常当我不需要从每个类别中进行选择时,我会做这样的事情。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
<?php the_field('name_of_field'); ?>
endwhile; else: ?>

<?php endif; ?>

但是现在我必须选择它是否是两个特定类别之一,然后在我的引导网格中将类别“tjanster”放在左栏中,将“produkter”放在右栏中。 所以它将是 50% 的“tjanster”,50% 的“produkter”。 我一直在疯狂寻找解决方案,但只找到了一些不起作用的 functions.php 代码和对我不起作用的 5 年旧帖子。

我应该做什么,所以我可以有一些类似的输出?

<div class="container">
  <div class="row">

    <div class="col-sm-6">
       <!-- Output of category tjanster. -->
    </div>

    <div class="col-sm-6">
       <!-- Output of category produkter. -->
    </div>

  </div>
</div>

【问题讨论】:

    标签: php wordpress categories custom-post-type advanced-custom-fields


    【解决方案1】:

    对于每一列,您需要一个 WP_Query()。请参阅 codex 以供参考:http://codex.wordpress.org/Class_Reference/WP_Query

    <div class="container">
      <div class="row">
        <div class="col-sm-6">
    
          <?php // tjanster
          $args1 = array(
            'category_name' => 'tjanster'
            );
    
          $query1 = new WP_Query( $args1 );    
          while ( $query1->have_posts() ) : $query1->the_post(); ?>
          <h2><?php the_title(); ?></h2>
          <?php the_content(); ?>
        <?php endwhile; 
        wp_reset_postdata(); ?>
    
      </div>
    
      <div class="col-sm-6">
    
          <?php  // produkter
          $args2 = array(
            'category_name' => 'produkter'
            );
    
          $query2 = new WP_Query( $args2 );    
          while ( $query2->have_posts() ) : $query2->the_post(); ?>
          <h2><?php the_title(); ?></h2>
          <?php the_content(); ?>
        <?php endwhile; 
        wp_reset_postdata(); ?>
    
      </div>
    </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2017-04-20
      相关资源
      最近更新 更多