Unyson框架中的WP_Query循环中不存在$ post变量。

时间:2016-06-04 07:12:01

标签: php wordpress

我需要从自定义查询中检索most_meta,我需要函数参数(postID),但在我的循环中全局变量$ post没有任何内容。

我使用Unyson框架(Page Builder Extension)来创建短代码View。 在我看来,可能是这个变量用于从短代码创建页面的情况,这是用这个扩展创建的。

我在Unyson框架之外使用了这个循环并且它有效。我花了一天时间试着在写这里之前意识到这个问题,我没有想法,我不想再用另一个框架制作网站。
     

            $args = array(
                'post_type' => 'elements',

            );
            $query = new WP_Query( $args );


            if($query->have_posts()):
                while( $query->have_posts()):
                    $query->the_post(); ?>

            <!--GRAPHIC DESIGN PART-->
            <div class="services__element--wrapper animate ">

                <div class="col-md-3 col-sm-6">
                    <i class="services__what-can-i-do--icon">
                        <?php echo $post->ID; ?> // nothing has shown

                    </i>
                    <h5 class="services__what-can-i-do--under-header"><?php the_title(); // work correcty?></h5>
                    <p class="services__what-can-i-do-text"><?php echo the_excerpt(); //work correcty?></p>
                </div>
            </div>
            <!--GRAPHIC DESIGN PART-->

1 个答案:

答案 0 :(得分:0)

在循环中使用get_the_ID,例如:$post_id = get_the_ID();

$args = array(
    'post_type' => 'elements',

);
$query = new WP_Query( $args );


if($query->have_posts()):
    while( $query->have_posts()):
        $query->the_post(); ?>

        $post_id = get_the_ID();

<!--GRAPHIC DESIGN PART-->
<div class="services__element--wrapper animate ">

    <div class="col-md-3 col-sm-6">
        <i class="services__what-can-i-do--icon">
            <?php echo $post->ID; ?> // nothing has shown

        </i>
        <h5 class="services__what-can-i-do--under-header"><?php the_title(); // work correcty?></h5>
        <p class="services__what-can-i-do-text"><?php echo the_excerpt(); //work correcty?></p>
    </div>
</div>
<!--GRAPHIC DESIGN PART-->