自定义小部件中的自定义帖子类型-$listItem

时间:2016-06-24 作者:Dionoh

嗨,我在制作自定义post类型小部件时遇到了一个小问题。

问题是,当我在代码中添加自定义字段时,显示自定义字段信息的部分位于输出代码的顶部。

以下是我使用的代码:

class Agenda_widget extends WP_Widget{
function __construct() {
    parent::__construct(
        \'agenda_widget\', // Base ID
        \'Agenda Widget\', // Name
        array(\'description\' => __( \'Displays all agenda points.\'))
       );
}
function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance[\'title\'] = strip_tags($new_instance[\'title\']);
    $instance[\'numberOfListings\'] = strip_tags($new_instance[\'numberOfListings\']);
    return $instance;
}

function form($instance) {
    if( $instance) {
        $title = esc_attr($instance[\'title\']);
        $numberOfListings = esc_attr($instance[\'numberOfListings\']);
    } else {
        $title = \'\';
        $numberOfListings = \'\';
    }
    ?>
        <p>
        <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title\', \'agenda_widget\'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <p>
        <label for="<?php echo $this->get_field_id(\'numberOfListings\'); ?>"><?php _e(\'Number of Listings:\', \'agenda_widget\'); ?></label>
        <select id="<?php echo $this->get_field_id(\'numberOfListings\'); ?>"  name="<?php echo $this->get_field_name(\'numberOfListings\'); ?>">
            <?php for($x=1;$x<=30;$x++): ?>
            <option <?php echo $x == $numberOfListings ? \'selected="selected"\' : \'\';?> value="<?php echo $x;?>"><?php echo $x; ?></option>
            <?php endfor;?>
        </select>
        </p>
    <?php
    }

function widget($args, $instance) {
    extract( $args );
    $title = apply_filters(\'widget_title\', $instance[\'title\']);
    $numberOfListings = $instance[\'numberOfListings\'];
    echo $before_widget;
    if ( $title ) {
        echo $before_title . $title . $after_title;
    }
    $this->getRealtyListings($numberOfListings);
    echo $after_widget;
}

function getRealtyListings($numberOfListings) { //html
    global $post;
    add_image_size( \'agenda_widget_size\', 85, 45, false );
    $listings = new WP_Query();
    $listings->query(\'post_type=agenda&posts_per_page=\' . $numberOfListings );
    if($listings->found_posts > 0) {
        echo \'<ul class="agenda">\';
            while ($listings->have_posts()) {
                $listings->the_post();
                $listItem = \'<li class="agenda-listing">\';
                $listItem .= \'<header id="agenda-block">\';
                $listItem .= \'<div id="agenda-info">\';
                $listItem .= \'<div id="agenda-info-time">\'; 
                $listItem .= \'</div>\';
                $listItem .= \'<div id="agenda-info-title">\';
                $listItem .=  the_field(\'time\'); 
                $listItem .= \'</div>\';
                $listItem .= \'<div id="agenda-info-name">\';
                $listItem .= \'</div>\';
                $listItem .= \'</div></header>\';
                $listItem .= \'</li>\';
                echo $listItem;
            }

        echo \'</ul>\';
        wp_reset_postdata();
    }else{
        echo \'<p style="padding:25px;">No listing found</p>\';
    }
}


} //end class Trm_Agenda_widget
register_widget(\'Agenda_widget\');
这是输出:

  This is the the_field(\'time\'); ---> 13.15
  <li class="agenda-listing">
    <header id="agenda-block">
      <div id="agenda-info">
        <div id="agenda-info-time">It should be here</div>
        <div id="agenda-info-title"></div>
        <div id="agenda-info-name"></div>
      </div>
    </header>
  </li>
请帮忙

1 个回复
最合适的回答,由SO网友:Łukasz Florczak 整理而成

改变the_field()get_field()

the_field() = echo get_field()