Wordpress & jQuery Cycle Plugin - How to get capti

2019-09-04 16:58发布

问题:

I'm trying to use jQuery Cycle Plugin within a theme that I'm currently developing. I've got the jQuery Cycle to display slideshow of post images on the home page. However, my problem is that I can't get it to display the content of a div that contains the_title() and the_excerpt() as a caption correctly. I'm using the following code:

<script>
    $(function() {
        $('#slideshow').cycle({
            fx:       'fadeZoom',
            timeout:   2000,
            after: onAfter
        });
    });
    function onAfter(curr,next,opts) {
        var caption = $('.featured-entry').text();
        $('#caption').html(caption);
    }
</script>

<div id="container">
    <div class="pics" id="slideshow">
        <?php
            $slideshow_posts = new WP_Query('cat=3&posts_per_page=5');
            while($slideshow_posts->have_posts())
                $slideshow_posts->the_post();
                $picture = get_post_meta($post->ID, 'picture', true);
                if(!empty($picture))
                {
                ?>
                    <div class="slideshow-inner">
                        <a class="featured-article" href="<?php the_permalink(); ?>"><img src="<?php echo $picture; ?>" /></a>
                        <div class="featured-entry">
                            <a class="entry-title" href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                            <div class="entry-summary"><?php the_excerpt() ?></div>
                        </div>
                    </div>
                <?php
                }
            }
        ?>
    </div>
    <div id="caption"></div>
</div>

The problem with this code is that it only displays the_excerpt of one post for all slides. Meaning that the_excerpt does not change when the image changes.

So what's I'm missing? This is my first work with jQuery, and I hope someone could help me with this.

P.S. I know how to get caption from img's attributes such as 'alt', but I don't want to do it that way and I want to get the caption from the content of a div.

Regards

-XO

回答1:

Try this:

function onAfter(curr, next, opts) {
    var caption = $(next).find('.featured-entry').text();
    $('#caption').html(caption);
}

$('featured-entry') should btw. never work — it's not a valid selector.