0
votes

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
@Sohrab how to do that?Tncmk

2 Answers

0
votes

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.

0
votes

Thanks guys I just solved it by changing

$term = get_the_category(); 

to

$term = get_the_terms($ID,array('blog-category','category'));