使用自定义字段查询循环,不返回任何结果

时间:2015-07-08 04:22:18

标签: php wordpress loops

我试图通过产品具有“关系”的国家/地区过滤循环。至。我已经根据自定义字段编写了一个查询,但它返回空,但我知道有与该国家/地区相关的产品。

我的代码如下,有什么想法吗?

<?php


if (!is_category()) {
echo '<p>Showing results for:<br />';
echo 'Number of results: '.$wp_query->found_posts;
echo 'Page '. ( get_query_var('paged') ? get_query_var('paged') : 1 ) . ' of ' . $wp_query->max_num_pages . '</p>';
}


$the_query = new WP_Query( $args );
$args = array(
'meta_key' => get_field('group-tour-country'),
'meta_value' => 'china'
);

if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();

    if (in_category(18)){ // is a group tour

        if (isset($countryNames)) { unset ($countryNames); }
        if (isset($countryLinks)) { unset ($countryLinks); }

        $countryIDS = get_field('group-tour-country');
        if( $countryIDS ):
            foreach( $countryIDS as $p): // variable must be called $p (IMPORTANT)
                setup_postdata($p);
                $countryNames[] = get_the_title( $p->ID );
                $countryLinks[] = get_the_permalink( $p->ID );
            endforeach;
        endif;
    ?>

        <article style="float:left;margin: 10px 0 10px 0;">

            <h2 style="background: none; line-height: normal;">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                    <?php the_title(); ?>
                    <?php if (strlen(get_the_title()) > 30) {
                        echo '<br />';
                    } ?>
                    <span style="font-size: 14px; color: #666;"><?php the_field('group-tour-duration'); ?>
                        days from $<?php echo $leadIn = (get_field('group-tour-lead-in-price') == 0 ? "N/A" : number_format(get_field('group-tour-lead-in-price'), 0, ".", ",")); ?></span>
                </a>
                <?php
                $paceField = get_field_object('group-tour-pace');
                $paceValue = get_field('group-tour-pace');
                $paceLabel = $paceField['choices'][$paceValue];
                ?>
                <a href="<?php bloginfo('url'); ?>/more-details?field=<?php the_field('group-tour-pace'); ?>" style="float:right; margin:0 2px;" class="info-overlay help" title="<?php echo $paceLabel . ' tour - click icon for more details.'; ?> ">
                    <img src="<?php bloginfo('template_url'); ?>/images/icons/paced-<?php echo $paceValue; ?>-icon.png" alt="<?php echo $paceLabel; ?> icon" class="tour-pace"/>
                </a>
                <?php
                if ( get_field('group-tour-altitude') ) {
                    ?>
                    <a href="<?php bloginfo('url'); ?>/more-details?field=high-altitude" class="info-overlay help" style="float:right; margin:0 2px;" title="Tour at high altitude - click icon for more details.">
                        <img src="<?php bloginfo('template_url'); ?>/images/icons/paced-altitude-icon.png" alt="High Altitude" class="tour-pace" />
                    </a>
                <?php } ?>
                <?php
                if (in_category(44)) {
                ?>
                <a href="<?php bloginfo('url'); ?>/more-details?field=rhs" class="info-overlay help" style="float:right; margin:0 2px;" title="RHS Garden Tour - click icon for more details.">
                    <i class="fa fa-leaf" style="color:#ae241b;"></i>
                </a>
                <?php } ?>
            </h2>

            <div style="width:150px; padding-right:5px; float: left; text-align: center">
                <?php
                if ( has_post_thumbnail() ) {
                $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'mapmedium' );
                $alt = get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true);
                $url = $thumb['0'];
                   echo '<a title="View '.get_the_title().' " href="' .get_the_permalink(). '"><img
                                width="150"
                                alt="'. $alt .'"
                                src="'. $url .'" /></a>';
                }
                else {
                    $tourImages = get_field('group-tour-gallery');
                    if ($tourImages) {
                        ?>
                        <a title="<?php echo $tourImages[0]['alt']; ?>" href="<?php the_permalink(); ?>"><img
                                width='150'
                                alt='<?php echo $image['alt']; ?>'
                                src='<?php echo $tourImages[0]['sizes']['mapmedium']; ?>'/></a>
                    <?php
                    }
                }

                $mapImage = get_field('group-tour-map');
                if( !empty($mapImage) ):
                $imgLarge = $mapImage['sizes'][ 'maplarge' ];
                ?>
                <a href="<?php echo $imgLarge; ?>" class="map-overlay" title="<?php the_title(); ?> group tour map" style="display:block; padding: 10px 0"><i class="fa fa-map-marker" style="color:#ae241b;"></i> View map</a>
                <?php endif; ?>
            </div>
            <div style="width:373px; float: left;">
                <p>
                    <?php
                    $shortDesc = substr(get_field('group-tour-description'), 0, stripos(get_field('group-tour-description'), "."));
                    echo $shortDesc . '...';
                    ?>
                </p>

                <p>
                    <strong>Countries Visited:</strong>
                    <?php
                    $i = 0;
                    foreach ($countryNames as $country) {
                        if ($i == count($countryNames) - 1) {
                            echo '<a href="' . $countryLinks[$i] . '" title="' . $country . ' group tours" >' . $country . '</a>';
                        } else {
                            echo '<a href="' . $countryLinks[$i] . '" title="' . $country . ' group tours" >' . $country . '</a>, ';
                        }
                        $i++;
                    }
                    ?>
                </p>

                <p style="text-align: right;">
                    <a href="<?php the_permalink(); ?>" class="book-online">More details &raquo;</a> &nbsp;&nbsp;&nbsp;
                    <a href="<?php the_permalink(); ?>#price-guide" class="book-online">Dates &amp; Prices &raquo;</a>
                </p>
            </div>

        </article>
    <?php
    }
    ?>


<?php endwhile; ?>


<?php else: ?>

    <!-- article -->
    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'WWT' ); ?></h2>
    </article>
    <!-- /article -->


 <?php endif; ?>

0 个答案:

没有答案