查询高级自定义字段不显示
问题描述:
我们有WP的ACF Pro,我们创建了一个ACF,显示一个选择的位置。查询高级自定义字段不显示
当试图输出我们得到这样的:
注意:试图让非对象的财产 /家庭/ cwplantactiveint /的public_html /可湿性粉剂内容/主题/ cwplant /循环作业。上线66
PHP 这是本
<?php $location = get_field('job_location'); echo $location->post_title; ?>
现在奇怪的是,它输出另一自定义字段,其是createdto输出日期:
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
整个代码块看起来是这样的:
<?php while (have_posts()) : the_post(); ?>
<?php /* Check closing date is not past. */
$today = strtotime("now");
$closedate = strtotime(get_field('closing_date'));
if ($today < $closedate || !get_field('closing_date')) {
?>
<div class="singlepost infobox info-job content cfix">
<h2><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'twentyten'), the_title_attribute('echo=0')); ?>"><?php the_title(); ?></a></h2>
<p><span class="red first">Location:</span> <?php $location = get_field('job_location'); echo $location->post_title; ?>
<span class="red">Closing Date:</span>
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
</p>
<?php if (is_archive() || is_search() || is_home()) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">View Details</a>
<?php else : ?>
<?php the_content(__('Continue reading →', 'twentyten')); ?>
<?php endif; ?>
</div>
<?php $jobstrue = 'true'; ?>
<?php } else { ?>
<?php $jobsfalse = 'true'; ?>
<?php } ?>
<?php endwhile; // End the loop. Whew. ?>
答
我觉得你的问题是,你有没有重置$post
对象与wp_reset_postdata()
所以你的查询返回最后一件东西在$post
(您的情况可能是createdto
)。
每当我处理在WP的任何对象,我总是将它设置,然后重新设置是这样的:
<?php
// YOUR CUSTOM FIELD
// 0- Reset post object from last query (this should really be part of your last query)
wp_reset_postdata();
// 1- Put custom field into post object and check for content
$post_object = get_field('location');
// 2- Check to make sure object exists and setup $post (must use $post variable name)
if ($post_object) {
$post = $post_object;
setup_postdata($post);
// 3- Do some magic
echo get_field('closing_date');
// 4- reset postdata so other parts of the page can use it
wp_reset_postdata();
}
?>
注:'$ closedate =的strtotime(get_field( 'closing_date'));' - 'get_field ''会返回一个'true'或'false',它应该是'the_field('closing_date')' - 类似于'$ location',你应该把它作为'if($ location){do_something}' –
这是一个档案模板还是常规模板? – staypuftman
@staypuftman这是一个标准的新页面模板。 – PhpDude