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' /> 

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.


1 Answer 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-->";

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 "
        var i = 1;
        function nextImage() {
            if ( ++i <= " . stripslashes( get_option( 'tm_alim' ) ) . " )
                document.getElementById('my-slideshow').src = 'http://sitename.com/' + i + '/00' + i + '.jpg';
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 .. Commented Mar 7, 2013 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
    Commented Mar 7, 2013 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 Commented Mar 7, 2013 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
    Commented Mar 7, 2013 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
    Commented Mar 7, 2013 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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