Wordpress & jQuery Cycle Plugin - How to get caption from div that contins the_title() and the_excerpt()

血红的双手。 提交于 2019-12-11 17:48:44

问题


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.



来源:https://stackoverflow.com/questions/5101890/wordpress-jquery-cycle-plugin-how-to-get-caption-from-div-that-contins-the-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!