• Not sure if this is the right forum, sorry in advance.

    I’m trying to do a mouseover change of photos. After help from wp forums I managed to put together a shortcode which actually works. Almost. The problem is when I try to use same shortcode on same page with different parameters it doesn’t work anymore. Shortcode in functions.php is

    <?php
    function roll_me_over_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'normal' => 'url/to/normal-not-set-photo.jpg',
            'hover' => 'url/to/hover-not-set-photo.jpg',
    	'link' => 'url'
        ), $atts, 'roll-me-over' );
    
        ob_Start();
        ?>
    
    <a href="'<?php echo esc_attr( $atts['link'] ); ?>'" target="_top"
       onmouseover="document.sub_but.src='<?php echo esc_attr( $atts['hover'] ); ?>'"
       onmouseout="document.sub_but.src='<?php echo esc_attr( $atts['normal'] ); ?>'">
        <img src='<?php echo esc_attr( $atts['normal'] ); ?>'
             style="width:250px; height:250px; border:0px solid #cc3300;"
             alt="Move your mouse over me" name="sub_but" />
    </a>
    
        <?php
         return ob_get_clean();
    }
    add_shortcode( 'roll-me-over', 'roll_me_over_shortcode' );

    and when I put only one shortoced per page it works like it should. But if I put two or more it only displays photo, without mouseover change. So when I put
    [roll-me-over link="url to page" normal="photo1" hover="photo2"] it works, but when I put more, so

    [roll-me-over link="url to page" normal="photo1" hover="photo2"]
    [roll-me-over link="url to page 2" normal="photo3" hover="photo4"]

    it doesn’t work anymore. Any suggestions?

Viewing 1 replies (of 1 total)
  • document.sub_but will retrieve element with name sub_but

    when you are only using one short code, document.sub_but will return:

    <img src="photo1" style="width:250px; height:250px; border:0px solid #cc3300;" alt="Move your mouse over me" name="sub_but">

    whereas when there are two short codes, document.sub_but will return an array:

    [
        <img src=?"photo1" style=?"width:?250px;? height:?250px;? border:?0px solid #cc3300;?" alt=?"Move your mouse over me" name=?"sub_but">?
    ,
        <img src=?"photo2" style=?"width:?250px;? height:?250px;? border:?0px solid #cc3300;?" alt=?"Move your mouse over me" name=?"sub_but">?
    ]

    which makes document.sub_but.src invalid

    my suggestion is to change that into:

    this.getElementsByTagName('img')[0].src

    this -> means <a> element
    getElementsByTagName('img')[0] -> retrieve the first <img> element relative from <a>

Viewing 1 replies (of 1 total)
  • The topic ‘Mulltiple shortcodes on same page’ is closed to new replies.