我正在使用自定义文章类型不同的菜单部分餐厅的菜单。我用出色的自定义文章类型UI插件和神韵元框插件。然后我流出来成单独的回路与下面码的若干段:

    <?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> 

但后来上面,我试图得到一个if询问是否有一个自定义类型后,我已经定义内的任何职位。

我尝试之类的

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

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

   $hotfoodpresent = get_posts($args);

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

那些,但均没有似乎工作。我想我已经通过WordPress的法典拖网并花了相当多的时间试图破译的问题。

任何帮助,将不胜感激。

<强>更新 对不起,完全排序。我是过于复杂的事情。一个简单的

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

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

完美。

有帮助吗?

解决方案

我被过于复杂的事情。一个简单的

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

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

完美。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top