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

时间:2014-09-04 20:56:08

标签: php wordpress

我似乎遇到了问题。当我添加这一堆代码时,自定义字段显示得很好,但网站周围的一切都很混乱!我的所有页面标题都采用相同的标题,并且the_content消失了。我确定我在这里遗漏了一些东西:

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

&GT;

我想......也许我不得不在之前添加通常的循环,然后在之后关闭它:

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

......但那并没有奏效。我错过了什么?

1 个答案:

答案 0 :(得分:0)

包含您的代码。这项工作并不会搞砸你的$ 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();
         ?>