0

I created very simple php loop which generates image links for given number. This is the code

<?php $im = stripslashes(get_option('tm_alim'));
for ($i=1; $i<=im; $i++)
  {
  echo "<img alt='' src='http://sitename.com/"  . $i . "/00"  . $i . ".jpg' /> 
<!--nextpage-->";
  }
?>

As you can see i am trying to print code right after every image which will make pagination. Each image will be shown in a page. This code is in a page template and the page is attached to that template.

But the problem is code is assumed as comment line and it is not showing even in html page source. Is there anyway to fix this or any alternative would be great

Or is there any way to tell wordpress total number of pages should be the $im or count the images and create pagination for each. i am sure there would be various ways to do it.

thanks

1

Firstly, in the for there is missing a '$'. It should read: for ( $i = 1; $i <= $im; $i++ ).

Secondly, in order to use the page link(s), you need to call the function wp_link_pages().

$im = stripslashes( get_option( 'tm_alim' ) );
for ( $i = 1; $i <= $im; $i++ )
    echo "<img alt='' src='http://sitename.com/" . $i . "/00"  . $i . ".jpg' />"
        . "<!--nextpage-->";
wp_link_pages();

See Page-Links for further information.

// Edit

You cannot use <!--nextpage--> inside template files, so you have to do it by any other means.

You could for instance provide the image with an id and alter the source when clicking on the link.

A quick-and-dirty set up could look like the following code:

echo "
    <script>
        var i = 1;
        function nextImage() {
            if ( ++i <= " . stripslashes( get_option( 'tm_alim' ) ) . " )
                document.getElementById('my-slideshow').src = 'http://sitename.com/' + i + '/00' + i + '.jpg';
        };
    </script>";
echo '<img id="my-slideshow" src="http://sitename.com/' . $i . '/00'  . $i . '.jpg" />';
echo '<a onclick="javascript:nextImage()" style="cursor:pointer;">next image</a>';

Of course you can also use jQuery if you already use it (or want to use it for that purpose).

  • first i actually had that "$" on my code but forgot here somehow second you also forgot "{" after for () third still same not giving pagination .. – Emre Caglar Mar 7 '13 at 12:23
  • Well, since I only want to do one single line (i.e., the echo), I don't need any curly brackets for the for. Where do you put the code? Inside a page (on the admin side) or inside a template file (such as custom page)? – tfrommen Mar 7 '13 at 12:27
  • without a curly bracket it was giving an error. this code goes to a template file and the page calls this template – Emre Caglar Mar 7 '13 at 12:34
  • This works definitley wihout curly bracktes as it is only a single line for the for - but that's not the problem anyway. It seems that you cannot use the pseudo-shortcode <!--nextpage--> inside template files. You have to do it yourself. I will edit my answer shortly. – tfrommen Mar 7 '13 at 12:45
  • See updated answer. I hope it's working. I tried out some different things but hopefully there are no leftovers which aren't used anymore. ;) – tfrommen Mar 7 '13 at 13:54

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.