在循环到自定义字段名称时筛选搜索结果


filter search results while loop to custom field names

如果我可以根据post_type 过滤搜索结果

在: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;
        ?>

然后我有两个模板:

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个自定义字段外的所有帖子和页面)

{搜索结果}


自定义字段#1

{搜索结果}


自定义字段#2

{搜索结果}

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

Mevius的回答是正确的。此外,无论值是多少,都要检查meta:

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