我的主页上有多个循环。我试图在每个循环中显示自定义字段。但其中一些没有出现。有什么想法吗,为什么?代码如下:
//check for Featured Events here
$featured = new WP_Query (array(
\'category_name\' => \'Featured Events\',
\'posts_per_page\' => 2)
);
if ($featured -> have_posts()) {
while ($featured-> have_posts()) : $featured -> the_post(); ?>
<h2 class="featured"> <?php the_title(); ?> </h2>
<h3 class="featured">
<?php echo get_post_meta($post->ID, \'Date\', true); ?> </h3>
<h4 class="featured">
<?php echo get_post_meta($post->ID, \'Start Time\', true); ?>
<?php if ( get_post_meta($post->ID, \'End Time\', true) != "") { ; ?>
-
<?php echo get_post_meta($post->ID, \'End Time\', true); }?> </h4>
<!--only print sponsor if value there -->
<?php if ( get_post_meta($post->ID, \'Sponsor\', true) != "") { ; ?>
<h5 class="featured">
<?php echo get_post_meta($post->ID, \'Sponsor\', true); ?></h5>
<?php }; ?>
<?php if ( get_post_meta($post->ID, \'Cost\', true) != "") { ; ?>
<p class="cost">
<?php echo \'Cost: \'. get_post_meta($post->ID, \'Cost\', true); ?> </p> <?php };
if ( get_post_meta($post->ID, \'Location\', true) != "") { ; ?>
<h6 class="featured">
<?php echo \'Location: \'. get_post_meta($post->ID, \'Location\', true); ?>
</h6>
<?php };
global $more;
$more = 0;
the_content(\'Read more please\');
if ( get_post_meta($post->ID, \'link\', true) != "") { ; ?>
<p> <a href="
<?php echo get_post_meta($post->ID, \'link\', true); ?> "
> Click here for more information </a> </p>
<?php } endwhile;
}
wp_reset_query(); ?>
</div> <!-- content_inner -->
</div> <!-- content -->
</div> <!-- end 2 column wrap div -->
<div id="lftcol">
<div id="lcol_inner">
<?php
$ann_query = new WP_Query (array(
\'category_name\' => \'announcement\',
\'orderby\'=> \'modified\',
\'posts_per_page\' => 5) );
if ($ann_query -> have_posts()) {
while ($ann_query-> have_posts()) : $ann_query -> the_post();
if( $post->ID == $do_not_duplicate ) continue; //This is the Magic Line
// echo \'NON urgent announcement should follow \'; ?>
<h2 class="featured"> <?php the_title(); ?> </h2>
<?php
//The code must be inserted ahead of the call the_content, but AFTER the_post()
global $more;
$more = 0; ?>
<h3 class="featured">
<?php echo get_post_meta($post->ID, \'Date\', true); ?>
</h3>
<h4 class="featured">
<?php echo get_post_meta($post->ID, \'Start Time\', true); ?>
<?php if ( get_post_meta($post->ID, \'End Time\', true) != "") { ; ?>
-
<?php echo get_post_meta($post->ID, \'End Time\', true); }?>
</h4>
<?php if ( get_post_meta($post->ID, \'Location\', true) != "") { ; ?>
<h6 class="featured">
<?php echo \'Location: \'. get_post_meta($post->ID, \'Location\', true); ?>
</h6>
<?php };
the_content(\'Read more please\');
if ( get_post_meta($post->ID, \'link\', true) != "") { ; ?>
<p> <a href="
<?php echo get_post_meta($post->ID, \'link\', true); ?>
" > Click here for more information </a> </p>
<?php }
endwhile; }
wp_reset_query(); ?>
</div>
</div>
最合适的回答,由SO网友:Mayeenul Islam 整理而成
是的,您的代码中存在问题。我想推荐你在WordPress上编写代码,打开相关的Codex页面以供参考,你不会错的。
首先,在WP_Query()
您使用\'category_name\' => \'Featured Events\'
. 但根据codex,你不能在这里使用分类名称,你必须使用Category Slug, 类别段塞中不能包含空格,它们必须用下划线连接。(Reference)
秒,英寸get_post_meta()
你的钥匙不对。因为键中不能包含空格。必须用下划线连接单独的字符串,如start_time
(not 开始时间)。我猜这些键在你用一个字符串编码的地方起作用Date
. (Reference)
最后,在每个查询结束时,您使用wp_reset_query();
重置查询。但根据Codex,对于WP\\u Query(),重置查询代码为not wp_reset_query();
, 但是wp_reset_postdata()
. (Reference)
仅此而已。
另外,在我的一个项目中,我使用了许多自定义字段。因此,我制作了两个可重用函数,以便在整个项目中使用。只需将以下内容粘贴到funcitons.php
在内部<?php
和?>
.
/**
* REUSABLE POST_META FUNCTION
*/
function has_custom_field( $fieldName = \'\' ) {
global $post;
$fetchedCustomField = get_post_meta($post->ID, $fieldName, true);
$CFchecker = ($fetchedCustomField == \'\') ? \'0\' : \'1\';
return (bool) $CFchecker;
}
function custom_field( $fieldName = \'\' ) {
global $post;
$fetchedCustomField = get_post_meta($post->ID, $fieldName, true);
return $fetchedCustomField;
}
并像这样使用它们:
<?php if(has_custom_field(\'date\')) { echo custom_field(\'date\'); } else { echo \'no date\'; } ?>
还有一个额外的提示:无论何时在WordPress上编码,请始终使用
WP_DEBUG
到
\'true\'
在里面
wp-config.php
. 别忘了将其设置为
\'false\'
开发完成后再次进行。(
Reference)
祝你好运
编辑以指定自定义字段,在post/page下添加如下自定义字段:
如果自定义字段的定义与图像类似,则get_post_meta()
将是:
<?php echo get_post_meta($post->ID, \'start_date\', true); ?>
但对于使用自定义字段,我喜欢使用
Advanced Custom Fields plugin.