Add a captions to integrated FlexSlider on Wordpre

2019-09-02 17:26发布

问题:

Would like to add a caption to FlexSlider, which is integrated into a purchased Wordpress Organictheme. I believe the slideshow page code must be modified and though I have found some similar issues here, I have not figured out (after many hours and failed attempts) where and what exactly to add to the existing code. Seems like captions should be a no-brainer! Seems like they should somehow be included within the

  • tags??? Would REALLY APPRECIATE some help. html, but NOT php literate. THANK YOU!

    The code needs to go somewhere in here:

                <div class="flexslider">
    
                    <ul class="slides">
    
                        <?php $data = array(
                            'post_parent'       => $post->ID,
                            'post_type'         => 'attachment',
                            'post_mime_type'    => 'image',
                            'order'             => 'ASC',
                            'orderby'           => 'menu_order',
                            'numberposts'       => -1
                        ); ?>
    
                        <?php 
                        $images = get_posts($data); foreach( $images as $image ) { 
                            $imageurl = wp_get_attachment_url($image->ID);              echo '<li><img src="'.$imageurl.'"  /></li>' . "\n"; 
                        } ?>
    
                    </ul>
    
                </div>
    

    回答1:

    You were very close, indeed.

    I'll answer this in case someone else needs it. The two missing bits of code are:

    <p class="flex-caption"><?php echo $caption; ?></p>
    

    ... required by flexslider to display the captions, and

    $caption = $image->post_excerpt;
    

    ... to actually obtain the captions. New code would be:

            <div class="flexslider">
    
                <ul class="slides">
    
                    <?php $data = array(
                        'post_parent'       => $post->ID,
                        'post_type'         => 'attachment',
                        'post_mime_type'    => 'image',
                        'order'             => 'ASC',
                        'orderby'           => 'menu_order',
                        'numberposts'       => -1
                    ); ?>
    
                    <?php 
                    $images = get_posts($data);
                    foreach( $images as $image ) { 
                        $imageurl = wp_get_attachment_url($image->ID);
                        $caption = $image->post_excerpt;
                        echo '<li><img src="'.$imageurl.'"  /><p class="flex-caption">'.$caption.'</p></li>' . "\n";
                    } ?>
    
                </ul>
    
            </div>