문제

I'm making a restaurant menu using custom post types for the different menu sections. I used the excellent Custom Post Types UI plugin and the Verve Meta Boxes plugin. I'm then flowing them out into separate loops with several segments of the below code:

    <?php 
   $hotfood = get_posts( array('post_type' => 'hotfood', 'posts_per_page' => -1) );
    foreach ($hotfood as $post) : 
      setup_postdata($post);
   ?>
   <dl>
    <dt>
     <strong><?php the_title_attribute(); ?></strong>
     <span>&pound;<?php echo get_post_meta($post->ID, 'price', true) ?></span>
    </dt>
   <?php
   $key = 'description';
   $themeta = get_post_meta($post->ID, $key, TRUE);
   if($themeta != '') {
   echo '<dd>'.$themeta.'</dd>';
   }
   ?>
   </dl> 

But then above that, I'm trying to get an if asking whether there are any posts within a custom post type that I've defined.

I've tried things like

     <?php if(get_posts ('post_type' => 'hotfood')
  echo '<h2>Hot Food</h2>';
     ?>

and

  <?php $args = array
   (
   'post_type' => 'hotfood',
   'numberposts' => -1'
   );

   $hotfoodpresent = get_posts($args);

    if($hotfoodpresent) echo '<h2>Hot Food</h2>';
  ?>

but neither of those seem to work. I think I've trawled through the Wordpress Codex and have spent quite a lot of time trying to decipher the issue.

Any help would be greatly appreciated.

UPDATE: Sorry, totally sorted. I was overcomplicating things. A simple

<?php
$hotfoodpresent = get_posts( 
    [
        'post_type' => 'hotfood', 
        'posts_per_page' => 1,
        'fields' => 'ids'
    ] 
);

if ($hotfoodpresent) {
    echo '<h2>Hot Food</h2>'
};

worked perfectly.

도움이 되었습니까?

해결책

I was overcomplicating things. A simple

<?php
$hotfoodpresent = get_posts( 
    [
        'post_type' => 'hotfood', 
        'posts_per_page' => 1,
        'fields' => 'ids'
    ] 
);

if ($hotfoodpresent) {
    echo '<h2>Hot Food</h2>';
};

worked perfectly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top