WP:显示自定义字段,但会在整个站点中导致错误(get_posts)


WP: Custom fields showing up, but causing errors throughout the site (get_posts)

我似乎遇到了一个问题。当我添加这堆代码时,自定义字段显示得很好,但网站周围的一切都很混乱!我所有的页面标题都使用了同一个标题,内容就消失了。我肯定我错过了什么:

<?php
$posts = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'page',
    'meta_query' => array(
        array(
            'key' => 'project_categories',
            'value' => '"boyle_upcoming"',
            'compare' => 'LIKE'
        )
    ),
    'order' => 'ASC'
));
if($posts) {
    foreach($posts as $post) {
        echo '<li><a href="' . get_permalink($post->ID) . '">' . '<img src="' . get_field('project_header_photo') . '" alt="' . get_the_title($post->ID) . '">' . '<div><em><b>' . get_the_title($post->ID) . '</b><br>' .get_field('project_location') . '<br><br>' . get_field('project_blurb') . '</em></div></a></li>';
    };
}
wp_reset_query();   

?>

我想。。。也许我必须在之前添加通常的循环,然后在之后关闭它

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

但那没用。我错过了什么?

包括您的代码。这项工作,不会打乱你的$post。

 <?php
         $args = array(
            'post_type'       => 'page',
            'post_status'     => 'publish',
           'posts_per_page'  =>  -1,
           'order'           => 'ASC',
           'meta_query' => array(
                   array(
                       'key' => 'project_categories',
                       'value' => 'boyle_upcoming',
                       'compare' => 'LIKE'
                   )
               ),
         );
         // The Query
         $the_query = new WP_Query( $args );
         // The Loop
         if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                 echo '<li><a href="' . get_permalink($the_query->post->ID) . '">' . '<img src="' . get_field('project_header_photo') . '" alt="' . get_the_title($the_query->post->ID) . '">' . '<div><em><b>' . get_the_title($the_query->post->ID) . '</b><br>' .get_field('project_location') . '<br><br>' . get_field('project_blurb') . '</em></div></a></li>';

         } else {
            echo "No DATA";
         }
         /* Restore original Post Data */
         wp_reset_postdata();
         ?>