0

Hi i have a slight problem when making a custom post type widget.

The problem is that when i want to get the field i want to use in a inline style it doesn't work.

This is my code:

class Agenda_widget extends WP_Widget{
function __construct() {
    parent::__construct(
        'agenda_widget', // Base ID
        'Agenda Widget', // Name
        array('description' => __( 'Details'))
       );
}
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 .= get_field('time');  
                $listItem .= '</div>';
                $listItem .= '<div id="agenda-info-title">';
                $listItem .= get_field('title'); 
                $listItem .= '</div>';
                $listItem .= '<div id="agenda-info-name">';
                $listItem .= get_field('speaker'); 
                $listItem .= '</div>';
                $listItem .= '<div id="agenda-block-image-responsive" style="get_field('speaker-img', 'option' );">';
                $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');

and this part:

$listItem .= '<div id="agenda-block-image-responsive" style="get_field('speaker-img', 'option' );">';

is not working.

What i'm trying to do is that i want a image to be automaticlly put in the listitem with a acf get_field function.

Please help

3
  • Could you be more specific about what your code should do, what it actually does and what you already have done to get it working?
    – cjbj
    Jun 24, 2016 at 12:24
  • i have updated my request
    – Dionoh
    Jun 24, 2016 at 12:29
  • This is my output: <div id="agenda-block-image-responsive" style=""></div> and there is no image in it.
    – Dionoh
    Jun 24, 2016 at 12:36

1 Answer 1

2

When you are concatenating string you shouldn't use ;after the method. Also you should close que quotes, call the method, and open again. Try the code below:

$listItem .= '<div id="agenda-block-image-responsive" style="' .get_field('speaker-img' ) . '">';
3
  • The output that i'm getting is this: <div id="agenda-block-image-responsive" style=""></div> and there is noting in it. i placed an image in the post and it is not in the output.
    – Dionoh
    Jun 24, 2016 at 12:34
  • The second parameter for the get_field method is the post_id. Try echo directly get_field('speaker-img' ) and work from there, if you need to change something. Just edited answer.
    – cbonomini
    Jun 24, 2016 at 12:38
  • the second parameter could also be 'option' if the field lives in an ACF Options page
    – mrben522
    Jul 3, 2017 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.