douche1936 2018-10-11 02:01
浏览 27

获取多个帖子类型和分类法

I want to display latest news/alerts post on my homepage and I need some help.

I am currently only displaying news with post post-type using below code. This is working perfectly. The problem occurs when I want to use more than one post-type. once I modify as 'post_type' => array('post','blog'), nothing appears. The reason why it does not show is $term = get_the_category();. Once I remove this whole section for color with <a></a>. It works perfectly.

So I believe I have to specify the taxonomies. Because in Post I have tax as category and in Blog I have tax as blog-category. I could not get these two in same time.

So how can I call two taxonomies in $term = get_the_category();

Thank you from now

<div class="news-area">
<div class="items clear">

<?php
$query = new WP_Query(array(
      'post_type' => 'post',
      'posts_per_page' => 4,
));
while ($query->have_posts()) : $query->the_post();
  $info = get_post_meta(get_the_ID(), '_post_info', true); if (!$info) $info = array();
  $post_elem=get_post();
  ?>
<div class="item <?php if (has_post_thumbnail()) echo 'has-image' ?>">
  <div class="inner">
      <div class="content">
          <?php if (has_post_thumbnail()): ?>
              <?php the_post_thumbnail('full') ?>
          <?php endif; ?>

          <?php
          $term = get_the_category();
          $term = $term[0];
          $color = get_term_meta($term->term_id, 'color', true);
          ?>
          <a href="<?php echo get_term_link($term) ?>" class="category" style="background-color: <?php echo $color ?>">
              <?php echo $term->name ?>
          </a>

          <h2 class="title">
              <a href="<?php the_permalink() ?>" class="underline"><?php the_title() ?></a>
          </h2>

          <div class="description">
              <?php //$except_meta=get_post_meta(get_the_ID(),"_excerpt") ?>
              <?php //echo "Aaaaaaa".$query->the_post()->post_excerpt ?>
              <?php if($post_elem->post_excerpt!=""): ?>
                  <?php  the_custom_excerpt($post_elem->post_excerpt, $length =80); ?>
              <?php else: ?>
              <?php the_excerpt(); ?>
              <?php endif; ?>
          </div>

          <div class="date"><?php echo get_the_date(et_get_option('_date_format')) ?></div>
      </div>
  </div>
</div>
<?php endwhile; wp_reset_postdata(); ?>

</div>
</div>
  • 写回答

2条回答 默认 最新

  • doumu1873 2018-10-12 00:59
    关注

    Cannot add a comment, so I'll post it as an answer. Try:

    <?php
          $term = get_the_category();
          if ( ! empty( $term ) ) {
              $term = $term[0];
              $color = get_term_meta($term->term_id, 'color', true);
              ?>
              <a href="<?php echo get_term_link($term) ?>" class="category" style="background-color: <?php echo $color ?>">
              <?php echo $term->name ?>
              </a>
              <?php
           }
           ?>
           <h2 class="title">
                  <a href="<?php the_permalink() ?>" class="underline"><?php the_title() ?></a>
           </h2>
    

    This will at least prevent from showing / executing when $color is not set.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)