1

I'm using the jquery cycle to create a slideshow in my Wordpress template. The "pager" is creating buttons that overlay the slideshow and rollover advances the slides. The trouble I'm running into is that I need the rollover to stay the same, but I also need to set an href so that clicking on the buttons takes the user to a specific page.

  1. I need to retrieve the variable $target from my slideshow for the href.
  2. I need the onclick to make the button clickable.

As I stated above, I'm new at this and it's quite possible that this is terrible code/can't be done/I'm a moron.

My jquery:

        <script src="<?php bloginfo('stylesheet_directory'); ?>/scripts/jquery.cycle.min.js" type="text/javascript"></script>
    <script type="text/javascript">
            jQuery(document).ready(function($) {
            if ( $('.slides > .slide').size() > 1 ) {
                $('.slides')
                    .cycle({
                        timeout: 6000,
                        speed: 1000,
                        pager: '#slides #mainNav',
                        pauseOnPagerHover: 200,
                        pagerEvent: 'mouseover',
                        pause: true,
                        pagerAnchorBuilder: function(idx, slide) {
                            var slideImage = $(slide).find('img');
                            var slideTitle = slideImage.attr('title');
                            var slideURL = "<?php echo $target; ?>";
                            return '<li><a href="/' + slideTitle + '">' + slideTitle + /* '<br /><span class="description">' + slideDescr + '</span>*/'</a></li>';
                        }
                    });

            }
        });
        </script>

and my Wordpress slideshow:

        <?php if ( is_front_page() && $slides = get_posts(array('numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_type' => 'slide')) ) : ?>
<div id="mainImg">
        <div id="slides">
            <div class="slides">
                <?php foreach ($slides as $slide) : ?>
                    <?php $title = $slide->post_title; ?>
                    <?php $content = wpautop($slide->post_content); ?>
                    <?php $description = get_post_meta($slide->ID, 'description', true); ?>
                    <?php $thumb = get_the_post_thumbnail($slide->ID, 'slide', array('title' => $title, 'alt' => $description)); ?>
                    <?php $url = get_post_meta($slide->ID, '_slide_url', true); ?>
                    <?php $target = (get_post_meta($slide->ID, '_slide_url_blank', true)) ? 'target="_blank"' : ''; ?>
                    <div class="slide">
                        <?php if ($url) : ?>
                            <a href="<?php echo $url; ?>" <?php echo $target; ?>><?php echo $thumb; ?></a>
                        <?php else: ?>
                            <?php echo $thumb; ?>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
<div id="mainNav"></div>        
        </div>
</div><!--end mainImg-->
    <?php endif; ?>

Thank you in advance for your help.

4

1 回答 1

0

你在哪里:

var slideURL = "<?php echo $target; ?>"

将其替换为:

var slideURL = $(slide).find('a').attr('target');

您在上面所做的是获取幻灯片对象,在幻灯片中找到“a”标签,然后获取“a”标签的“目标”属性。然后将此值分配给slideURL变量(将是'_blank'undefined)。有关如何使用 jQuery 获取 HTML 元素和/或其属性的更多信息,您应该更加熟悉 jQuery 的TraversingAttributes方法。

无论如何,您都不应该将 Javascript 代码与 PHP 甚至 HTML 代码混合使用。阅读更多关于“Unobtrusive JavaScript”的内容。

于 2012-12-17T00:46:05.700 回答