0

我从博客(https://tessakriesel.com/learn/how-to-filter-custom-post-types-by-acf-date-field/)获得了这个功能,但它在页面顶部显示了输出和不在我放置简码的元素中。我看到很多它与echo. 但我不知道如何将我的代码更改为正确的工作代码:

function evenementen() {
$today = current_time('Ymd');
$args = array(
    'post_type' => 'evenementen',
    'posts_per_page' => '4',
    'meta_key'  => 'datum_evenement',
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_query' => array(
      array(
            'key'   => 'datum_evenement',
            'compare' => '>=',
            'value'   => $today,
        ),
    ),
);

$children = new WP_Query($args);
?>

<?php if ($children->have_posts()) : ?>
  <?php while ($children->have_posts()) : $children->the_post(); $fields = (object) get_fields(); ?>
    <div class="event row">
      <div class="event-logo col-sm-4">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' ) ); ?></a>
      </div>
      <div class="event-details col-sm-8">
        <h3 class="underline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <h5><?php the_field('datum_evenement'); ?></h5>
        <p class="call-to-action"><a href="<?php the_permalink(); ?>">Read More</a></p>
      </div>
    </div>
  <?php endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
<?php

} add_shortcode('nm2d_evenementen', 'evenementen');

4

1 回答 1

1

你需要使用'ob_start();' 在你的函数开始和返回函数'return ob_get_clean();' 在 wordpress 中,此功能用于维护包含中的功能序列。

function evenementen() {
ob_start();
$today = current_time('Ymd');
$args = array(
    'post_type' => 'evenementen',
    'posts_per_page' => '4',
    'meta_key'  => 'datum_evenement',
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_query' => array(
      array(
            'key'   => 'datum_evenement',
            'compare' => '>=',
            'value'   => $today,
        ),
    ),
);

$children = new WP_Query($args);
?>

<?php if ($children->have_posts()) : ?>
  <?php while ($children->have_posts()) : $children->the_post(); $fields = (object) get_fields(); ?>
    <div class="event row">
      <div class="event-logo col-sm-4">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' ) ); ?></a>
      </div>
      <div class="event-details col-sm-8">
        <h3 class="underline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <h5><?php the_field('datum_evenement'); ?></h5>
        <p class="call-to-action"><a href="<?php the_permalink(); ?>">Read More</a></p>
      </div>
    </div>
  <?php endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
<?php
return ob_get_clean();
}add_shortcode('nm2d_evenementen', 'evenementen');
于 2019-09-16T11:48:20.980 回答