0

I'm having trouble fixing this for working in ie8, i find out thats last comma is problem, but dont know how to fix it.

this is current code.

<?php $args = array( 'post_type' => 'katalog', 'posts_per_page' => 999 );
$loop = new WP_Query( $args ); 
if ($loop->have_posts()) : 
while ($loop->have_posts()) : $loop->the_post(); ?>

<?php global $post; $thumb=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full'); $thumb2=wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium'); $url = rwmb_meta( 'smb_katalozi' ); ?>
{image : "<?php echo $thumb[0] ?> ", title : "<?php echo get_the_title() ?>", url : "<?php echo post_permalink( $url ); ?>"},

<?php endwhile; else: endif; ?>

and the output is this

{image : "img url", title : "title", url : "url"},
{image : "img url", title : "title", url : "url"},

and i want it to be

{image : "img url", title : "title", url : "url"},
{image : "img url", title : "title", url : "url"}

so, its different just by last comma.


Got it to work with this code

<?php
    $args = array( 'post_type' => 'katalog', 'posts_per_page' => 999 );
    $loop = new WP_Query( $args ); 
    $post_count = 0;
    $te = $loop->post_count;
    $total = $te-1;
    if ($loop->have_posts()) : 
    while ($loop->have_posts()) : $loop->the_post(); 
    global $post;  $thumb=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full'); $thumb2=wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium'); $url = rwmb_meta( 'smb_katalozi' );
    if ($post_count == 0 || $post_count !== $total) { ?>
        {image : "<?php echo $thumb[0] ?> ", title : "<?php echo get_the_title() ?>", url : "<?php echo post_permalink( $url ); ?>"},
    <?php } if ($post_count == $total) { ?>
        {image : "<?php echo $thumb[0] ?> ", title : "<?php echo get_the_title() ?>", url : "<?php echo post_permalink( $url ); ?>"}
    <?php }
    $post_count++;
    endwhile; else: endif;
    ?>
1
1

Looks like you're trying to build up some JSON. Why not use what PHP already provides, which has the advantage of escaping characters too?

global $post;

$data = array();

if ($loop->have_posts()) {
    while ($loop->have_posts()) {
        $loop->the_post();
        $thumb=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full');
        $thumb2=wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium');
        $url = rwmb_meta( 'smb_katalozi' );

        $data[] = array (
            'image' => $thumb[0],
            'title' => get_the_title(),
            'url' => post_permalink( $url )
        );
    }
}

echo json_encode($data);
0

Try this in place of comma inside while loop:

if ( $loop->have_posts() )
    echo ',';

This will output comma as long as there is still next post in loop.

5
  • you sure? This should be in the while loop to iterate.
    – JMau
    Jun 14 '13 at 20:35
  • I meant this to go inside loop, where plain text comma currently is.
    – Rarst
    Jun 14 '13 at 20:37
  • Please add an explanation to your answer: why could that solve the problem? Greetings from the Low Quality queue. :)
    – fuxia
    Jun 14 '13 at 21:30
  • @toscho hey, I spent a lot of effort twisting this into WP-specific answer already :)
    – Rarst
    Jun 14 '13 at 23:14
  • tips and tricks... is it such a big deal? ^^ I prefer using CSS for separators, indeed it's poor.
    – JMau
    Jun 15 '13 at 1:12
-1

While this can be done in PHP, I prefer using some markup and css peudo-class such as last-child

So this can be :

<span class="last-child">{image : "<?php echo $thumb[0] ?> ", title : "<?php echo get_the_title() ?>", url : "<?php echo post_permalink( $url ); ?>"}</span>

and in your stylesheet :

.last-child:after { content:", "; }
.last-child:last-child:after { content:"";}

You can use selectivzr to make it compatible with older versions on IE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.