过滤搜索结果while循环自定义字段名称
如果我可以过滤搜索结果基于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
{搜索结果}
如何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 { ?>
梅维斯的回答是正确的。另外,不管值如何检查meta:
if('' != get_post_meta(get_the_ID(), 'mobile_app', true)) {
// do stuff
}
当一篇文章保存在管理员中时,'update_post_meta()'如何处理“空白”值?我没有检查过。它会是一个空字符串吗?假?空值?空白?实际上,我不知道。这肯定会影响'''!='是否会一直工作。 – rnevius 2014-10-30 19:28:51
可以更好地检查'!empty()' – rnevius 2014-10-30 19:30:39
从'get_post_meta()'的codex条目:如果没有任何东西要返回,那么函数将返回一个空数组,除非'$ single'已经被设置为'true' ,在这种情况下返回一个空字符串。“ – diggy 2014-10-30 19:32:37
感谢这看起来很棒,但仍然没有工作;我已更新我的问题,有什么建议? – 2014-10-30 20:30:14