3

i have an edited Supersized...i've added a code for loop about photos on wp....

so i've this on original:

slides : [
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', title : 'Image Credit: Brooke Shaden', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg', url : 'http://www.nonsensesociety.com/2011/06/brooke-shaden/'},
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-3.jpg', title : 'Image Credit: Brooke Shaden', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-3.jpg', url : 'http://www.nonsensesociety.com/2011/06/brooke-shaden/'}
                                        ],

and i have edited for loop gallery from wp:

slides : [

                    <?php query_posts('cat=46'); ?> 
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>     
                        <?php
                            $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
                            $thumb = wp_get_attachment_image_src($thumb[0], false);
                            $thumb = $thumb[0];
                        if ( has_post_thumbnail() ) { ?>
                                {image : '<?php echo $thumb; ?>'},
                    <?php } ?>
                    <?php endwhile; else: ?>
                    <?php endif; ?>
                    <?php wp_reset_query(); ?>
                    ],

and it work on Chrome, Firefox etc...

but i've a problem with this alert on IE 8 - 7 and Firefox old:

Message: ‘slides[...].url’ is null or not an object

Line: 23

Char: 3

Code: 0

URI: supersized.3.0.js

i've heard that the true problem is a last comma (you can see at the first code on this post, there isn't...and it work perfectly.

so i want to resolve for remove the last comma at the edited slides...'cause it repeat comme at one by one images and it cause this problem on ie... how can i remove this last comma?

Niko
  • 26,516
  • 9
  • 93
  • 110
user12932
  • 61
  • 6

5 Answers5

1

Why are you constantly dropping in and out of PHP? That's terribly inefficient...

slides : [ 
    <?php
    query_posts('cat=46');
    if ( have_posts() ) {
        $post_array = Array();
        while ( have_posts() ) {
            the_post();
            $thumb = get_post_meta($post->ID,'_thumbnail_id',false); 
            $thumb = wp_get_attachment_image_src($thumb[0], false); 
            $thumb = $thumb[0]; 
            if ( has_post_thumbnail() ) {
                $post_array[] = "{image : '".$thumb."'}";
            } 
        }
        echo implode(",",$post_array);
    }
    wp_reset_query();
    ?>
], 
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Out of curiosity, could you explain why that type of coding is inefficient please? – RRikesh Oct 03 '12 at 04:59
  • It's throwing in unnecessary tokens. I don't know how PHP works, but it can't be good for it. – Niet the Dark Absol Oct 03 '12 at 11:43
  • Every time you close off the php on one line and open it on the next line, it's the same as adding a print statement to print a newline and a bunch of whitespace. It also makes your code very hard to follow. Kolink's version is so much easier to read! And his version results in a lot less unintended whitespace in the final output. – Tom McClure Oct 03 '12 at 17:01
0

Are you sure the problem is about the last coma? If yes you need to save all the images in a variable, get rid of the last coma at the end of the loop and the print the result:

<?php $string = ''; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>     
                    <?php
                        $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
                        $thumb = wp_get_attachment_image_src($thumb[0], false);
                        $thumb = $thumb[0];
                    if ( has_post_thumbnail() ) { ?>
                <?php  $string.= '{image : '.$thumb.'},';
                <?php } ?>
                <?php endwhile; ?>
                <?php echo substr($string, -1); ?>
Michal Trojanowski
  • 10,641
  • 2
  • 22
  • 41
0

3 solutions:

one:

<?php if ( have_posts() ) : $i=0; while ( have_posts() ) : the_post(); ?>     
                        <?php
                            $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
                            $thumb = wp_get_attachment_image_src($thumb[0], false);
                            $thumb = $thumb[0];
                        if ( has_post_thumbnail() ) { ?>
                                <?=($i>0?',':'')?>{image : '<?php echo $thumb; ?>'}
                    <?php  } $i++;?>

two:

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>     
                            <?php
                                $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
                                $thumb = wp_get_attachment_image_src($thumb[0], false);
                                $thumb = $thumb[0];
                            if ( has_post_thumbnail() ) { ?>
                                    $string[]="{image : '<?php echo $thumb; ?>'}";
                        <?php } if (!empty($string)) implode(",",$string);

tree:

     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>     
                                <?php
                                    $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
                                    $thumb = wp_get_attachment_image_src($thumb[0], false);
                                    $thumb = $thumb[0];
                                if ( has_post_thumbnail() ) { ?>
                                        $string.="{image : '<?php echo $thumb; ?>'}";
                            <?php } $lenght=strlen($string)-2;
echo substr($string,0,$lenght);

For my point of view the second solution is the most clean.

David
  • 1,116
  • 3
  • 18
  • 32
0

Why not use json to form the slides array

<?php 
$slide_arr = array ();

query_posts('cat=46'); 
if ( have_posts() ) : 
  while ( have_posts() ) : the_post();     
    $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
    $thumb = wp_get_attachment_image_src($thumb[0], false);
    $thumb = $thumb[0];
    if ( has_post_thumbnail() ) { 
      $slideObj = new stdClass;
      $slideObj->image = $thumb; 
      $slide_arr[] = $slideObj;
    } 
  endwhile; 
else: 
endif; 
wp_reset_query(); 

$slide_json = json_encode($slide_arr);
?>

var slide_json = '<?php echo $slide_json; ?>';
var slide_arr  = eval('(' + slide_json + ')');
// js code ...

slides : slide_arr,
air4x
  • 5,618
  • 1
  • 23
  • 36
-2

it is valid to have a trailing comma in an array of JS objects, so the comma is not your issue. This error is thrown because you have not set the URL property for your slide objects. this is your JSON string that you currently output:

{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg'}

but you need the complete object for slides like this:

{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', title: 'YOUR TITLE HERE', thumb: 'SOME IMAGE PATH GOES HERE', url: 'SOME URL PATH GOES HERE'}

UPDATE:

People seem to think there is some sort of problem wit the comma. I do not think this is the main issue. It might cause problems (i dont think it will) but besides that, this guys code WILL NEVER WORK WITHOUT THROWING THE ERROR THAT HE NOTED IN HIS QUESTION. I went directly to the supersize source to show you people what the problem is.

var imageLink = (options.slides[options.slides.length - 1].url) ? "href='" + options.slides[options.slides.length - 1].url + "'" : "";

there it is. on line 23, just as HIS ERROR MESSAGE SAYS. The script is trying to access the URL property of each slide object. BUT HE HASN'T SET A URL PROPERTY. so it will ALWAYS throw the error that THE USER SAID HE IS GETTING.

solidau
  • 4,021
  • 3
  • 24
  • 45
  • While it *is* valid javascript, it is not accepted by MSIE. So, the trailing comma actually *is* the source of his issue. http://stackoverflow.com/questions/7246618/trailing-commas-in-javascript -- also, strict JSON does *not* allow the trailing comma! http://stackoverflow.com/questions/201782/can-you-use-a-trailing-comma-in-a-json-object – Tom McClure Oct 02 '12 at 23:30
  • I think you're wrong. here's the fiddle http://jsfiddle.net/AvbL3/3/. Notice how it works in IE7/8. Notice how it DOESN'T work to call a property that doesn't exist. – solidau Oct 03 '12 at 00:41
  • yes, sorry, there are issues with MSIE and trailing commas but the main issue is exactly as you say. apologies! – Tom McClure Oct 03 '12 at 05:31