2014-10-30 53 views
0

如果我可以過濾搜索結果基於post_type過濾搜索結果while循環自定義字段名稱

在:if ($post->post_type == "mobile-experience") { ?>

如何添加兩個過濾器,將篩選結果基於自定義字段名稱

我曾嘗試:

if ($post->post_meta == "mobile_app") { ?> 

但不起作用。

自定義字段名稱爲'Mobile App'。

我想在3個自定義字段名稱下顯示搜索結果;沒有用。

<?php 
// Start the Loop. 
while (have_posts()) : the_post(); 

if ($post->post_type == "mobile-experience") { ?> 

     <?php get_template_part('search-mobile-experience'); // works 
    } 

if ($post->post_meta == "mobile_app") { ?> // Doesn't work 

     <?php get_template_part('new-mobile-app_template'); 
} 
else { ?> 

更新:週四下午1:15($#& !!)

非常感謝您的意見傢伙;這讓我瘋狂。以下是我目前擁有的。但仍然出現我的自定義字段過濾器被忽略。

 <?php 
      // Start the Loop. 
      while (have_posts()) : the_post(); 

       if ($post->post_type == "mobile-experience") { ?> 

         <?php get_template_part('search-mobile-experience'); 
        } 

      if (get_post_meta($post->ID, 'mobile_app', true) == "mobile_app") { ?> 

       <?php get_template_part('new-mobile-app-template'); 
         } 
        else { ?> 

         <p><?php the_date(); ?></p> 

         <?php get_template_part('content', get_post_format()); 
        } 
      endwhile; 

     else : 
      // If no content, include the "No posts found" template. 
      get_template_part('content', 'none'); 

     endif; 
     ?> 

然後,我有2個模板:

new-mobile-app-template.php 

search-mobile-experience.php 

(最終我將有一個第三,或自定義字段過濾器2)

這兩個看起來像這樣:

<?php 
/** 
* The default template for displaying content. Used for both single and index/archive/search. 
* 
* @package WordPress 
* @subpackage Twenty_Twelve 
* @since Twenty Twelve 1.0 
*/ 
?> 

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
     <?php if (is_sticky() && is_home() && ! is_paged()) : ?> 
     <div class="featured-post"> 
      <?php _e('Featured post', 'twentytwelve'); ?> 
     </div> 
     <?php endif; ?> 
     <header class="entry-header"> 
      <?php the_post_thumbnail(); ?> 
      <p>Mobile Website | March 28, 2014 </p> 
      <h1 class="entry-title"> 
       <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a> 
      </h1> 

     </header><!-- .entry-header --> 

     <div class="entry-summary"> 
      <?php the_excerpt(); ?> 

         <?php if ((get_custom_field('Objective')) !== ''): ?> 
          <?php print_custom_field('Objective'); ?> 
         <?php endif ?> 

     </div> 
    </article> 
    <!-- #post --><hr /> 

我的目標是有一個搜索結果頁面有3類:


文章(所有文章和頁面除了2個下文提到的自定義字段)

{Search Results}


自定義字段#1

{搜索結果}


自定義字段#2

{搜索結果}

回答

0

如何get_post_meta()?例如:

<?php 
// Start the Loop. 
while (have_posts()) : the_post(); 

if ($post->post_type == "mobile-experience") { ?> 

     <?php get_template_part('search-mobile-experience'); // works 
    } 

if (get_post_meta($post->ID, 'mobile_app', true) == "mobile_app") { ?> 

     <?php get_template_part('new-mobile-app_template'); 
} 
else { ?> 
+0

感謝這看起來很棒,但仍然沒有工作;我已更新我的問題,有什麼建議? – 2014-10-30 20:30:14

0

梅維斯的回答是正確的。另外,不管值如何檢查meta:

if('' != get_post_meta(get_the_ID(), 'mobile_app', true)) { 
    // do stuff 
} 
+0

當一篇文章保存在管理員中時,'update_post_meta()'如何處理「空白」值?我沒有檢查過。它會是一個空字符串嗎?假?空值?空白?實際上,我不知道。這肯定會影響'''!='是否會一直工作。 – rnevius 2014-10-30 19:28:51

+0

可以更好地檢查'!empty()' – rnevius 2014-10-30 19:30:39

+1

從'get_post_meta()'的codex條目:如果沒有任何東西要返回,那麼函數將返回一個空數組,除非'$ single'已經被設置爲'true' ,在這種情況下返回一個空字符串。「 – diggy 2014-10-30 19:32:37