Wordpress - Query se un tipo di messaggio personalizzato ha scritto post - al di fuori del ciclo

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

Domanda

Sto facendo un menu di un ristorante utilizzando i tipi di messaggi personalizzati per le diverse sezioni del menu. Ho usato le eccellenti Tipi personalizzati post UI plug-in e il plugin Verve Meta Boxes. Sono poi scorre fuori in cicli separati con diversi segmenti del codice qui sotto:

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

Ma poi sopra che, sto cercando di ottenere un if chiedendo se ci sono posti all'interno di un tipo messaggio personalizzato che ho definito.

Ho provato cose come

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

e

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

   $hotfoodpresent = get_posts($args);

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

, ma nessuno di questi sembra funzionare. Penso di aver spulciato attraverso il Wordpress Codex e hanno trascorso un sacco di tempo a cercare di decifrare il problema.

Qualsiasi aiuto sarebbe molto apprezzato.

UPDATE: Siamo spiacenti, totalmente ordinato. Stavo overcomplicating cose. Un semplice

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

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

ha funzionato perfettamente.

È stato utile?

Soluzione

Sono stato overcomplicating cose. Un semplice

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

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

ha funzionato perfettamente.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top