mercredi 23 mars 2016

Widget retrieve same post_excerpt for different Post object

WP_Post Object
(
    [ID] => 73 
    [post_title] => Jane Doe
    [post_excerpt] => Junior Programmer
    [post_name] => jane-doe

)

WP_Post Object
(
    [ID] => 72
    [post_title] => Rob Gilbert
    [post_excerpt] => Trainee Programmer
    [post_name] => rob-gilbert
)

Those two objects are generating from a $posts = get_posts($args); Each of the post_title is showing as a select box option. But I also need to show post_excerpt. If I choose Jane Doe from select box it loads "Junior Programmer" as the value of post_excerpt.

But if I choose Julia Doe from options that is also showing "Junior Programmer" as the value of post_excerpt instead of showing "Programming Lead".

What did I do wrong? Why the same value is showing for different option? What is it that I am missing

enter image description here

Here is the backend code.

global $post;    
    $args = array(
            'type'                  => 'post',                              
            'orderby'               => 'name',
            'order'                 => 'ASC',                   
            'taxonomy'              => 'department',                    
            'posts_per_page'        => -1,
            'post_type'             => 'employee',
            'tax_query'             => array(
                                            array(
                                                'taxonomy' => 'department',
                                                'field' => 'slug',
                                                'terms' => 'programming',
                                                )
                                            ) 
                ); 
            $posts = get_posts( $args );
            ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'employee' ); ?>">Employee:</label>
                <select class="widefat" 
                    name="<?php echo $this->get_field_name( 'employee' );?>"
                    id="<?php echo $this->get_field_id( 'employee' ); ?>">
                        <option value="" selected></option>
                <?php 
                        foreach ( $posts as $post ) :
                            setup_postdata( $post );                        
                 ?>
                <option value="<?php echo esc_attr( $post->post_title ); ?>"
            <?php selected($instance['employee'], $post->post_title); ?>>
            <?php echo esc_html( $post->post_title );?>
            <?php $excerpt = esc_html( $post->post_excerpt ); ?>                    
                </option>           
                <?php   endforeach; wp_reset_postdata(); ?>
                    </select>               
                <p> 
                <?php echo "<pre>"; print_r($excerpt); echo "</pre>"; ?>        



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire