Wordpress - Consulta si un tipo de mensaje personalizado tiene ningún post - desde fuera del bucle

StackOverflow https://stackoverflow.com/questions/4023932

Pregunta

Estoy haciendo un menú de restaurante usando tipos personalizados de correos para las diferentes secciones del menú. He utilizado las excelentes Tipos personalizados Pon UI Plugin y el plugin Cajas Verve Meta. entonces yo estoy fluyendo hacia fuera en bucles independientes con varios segmentos de los siguientes códigos:

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

Pero, por encima de eso, yo estoy tratando de obtener una if preguntando si hay alguna puestos dentro de un tipo de mensaje personalizado que he definido.

He intentado cosas como

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

y

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

   $hotfoodpresent = get_posts($args);

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

pero ninguno de los que parecen funcionar. Creo que he rastreado a través del Codex de WordPress y he pasado mucho tiempo tratando de descifrar el problema.

Cualquier ayuda sería muy apreciada.

ACTUALIZACIÓN: Lo sentimos, totalmente ordenados. Estaba complicar las cosas. Un simple

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

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

funcionó a la perfección.

¿Fue útil?

Solución

Me complicar las cosas. Un simple

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

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

funcionó a la perfección.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top