我正在努力使用Wordpress和Advanced Custom Fields


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

所以 - 在顶部,我们有"案例研究",这只是从网站上的帖子中提取一些细节(客户名称和部门)。

接下来,我有"其他客户端" - 这在显示此代码的页面上设置为高级自定义字段。也很简单。

现在,乐趣来了:

如果我反转两个部分(首先是"其他客户"),但它有效,但不是这样 - 任何想法出了什么问题?我假设是"案例研究"部分中的内容搞砸了下面的内容,但我不知所措。如果我能提供更多信息,请告诉我!

提前非常感谢 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对象,因此在完成查询后,必须始终执行此操作。