我正在为 Wordpress 和高级自定义字段苦苦挣扎
I'm struggling with Wordpress and Advanced Custom Fields
我有一些 PHP 代码正在 WordPress 网站中使用。这是代码:
<h3>Case Studies</h3>
<?php
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<h3>Other Clients</h3>
<?php if( have_rows('clients') ):
while ( have_rows('clients') ) : the_row(); ?>
<a>
<h1><?php the_sub_field('client'); ?></h1><p><?php the_sub_field('sector'); ?></p>
<span></span>
</a>
<?php endwhile; endif; ?>
所以 - 在顶部,我们有 "Case Studies",这只是从网站上的帖子(客户名称和行业)中提取一些细节。
接下来,我得到了 "Other Clients" - 它被设置为出现此代码的页面上的高级自定义字段。也很简单。
现在,有趣的是:
如果我颠倒这两个部分(首先是 "Other Clients")但不是这样 - 知道出了什么问题吗?我假设是 "Case Studies" 部分中的某些内容弄乱了下一个部分,但我不知所措。如果我能提供更多信息,请告诉我!
非常感谢x
完成自定义查询后,您必须调用 wp_reset_postdata()
;
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
因为您的自定义查询 $the_query->the_post();
覆盖全局 $post
对象,所以在完成查询后必须始终执行此操作。
我有一些 PHP 代码正在 WordPress 网站中使用。这是代码:
<h3>Case Studies</h3>
<?php
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<h3>Other Clients</h3>
<?php if( have_rows('clients') ):
while ( have_rows('clients') ) : the_row(); ?>
<a>
<h1><?php the_sub_field('client'); ?></h1><p><?php the_sub_field('sector'); ?></p>
<span></span>
</a>
<?php endwhile; endif; ?>
所以 - 在顶部,我们有 "Case Studies",这只是从网站上的帖子(客户名称和行业)中提取一些细节。
接下来,我得到了 "Other Clients" - 它被设置为出现此代码的页面上的高级自定义字段。也很简单。
现在,有趣的是:
如果我颠倒这两个部分(首先是 "Other Clients")但不是这样 - 知道出了什么问题吗?我假设是 "Case Studies" 部分中的某些内容弄乱了下一个部分,但我不知所措。如果我能提供更多信息,请告诉我!
非常感谢x
完成自定义查询后,您必须调用 wp_reset_postdata()
;
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
因为您的自定义查询 $the_query->the_post();
覆盖全局 $post
对象,所以在完成查询后必须始终执行此操作。