• Resolved brad8985

    (@brad8985)


    I have seen a few similar questions but still can’t find an answer. The code below is outputting the custom field values in random order. The order doesn’t change, but is different from page to page.

    The fields are ordered in sequential order (HA01.png, HA02.png, HA03.png) etc. They are also input in the correct order.

    <?php
    
    $image = get_post_meta($post->ID, 'Image', true);
    $imagelarge = get_post_meta($post->ID, 'ImageLarge', false);
    $client = get_post_meta($post->ID, 'Client', true); ?>
    
    <ul>
    <?php foreach($imagelarge as $images){ ?>
    <li>
    <a rel="<?php echo $client; ?>" href="http://www.bradlangdon.co.uk/wp-content/uploads/<?php echo $images; ?>" >
    </a>
    </li>
    <?php } ?>
    </ul>
Viewing 7 replies - 1 through 7 (of 7 total)
  • What does the $imagelarge array look like before it is looped over in the foreach loop above?

    If you’re not sure how to find that out, before this line..

    <ul>

    Add.

    <?php print '<!--<pre>'."\n"; print_r( $imagelarge ); print "\n".'</pre>-->'; ?>

    Then load up the page and look in the source code, you should see an array of data ..

    Copy and paste that here, so i can see how the array looks (just the array of data please, not the whole source), and i’ll write you some code to sort the fields into the correct order.

    Thread Starter brad8985

    (@brad8985)

    Thanks,

    This is the output…

    Array
    (
        [0] => HA02.png
        [1] => HA01.png
        [2] => HA03.png
    )

    The order I input the fields is HA01, HA02, HA03

    After.

    $imagelarge = get_post_meta($post->ID, 'ImageLarge', false);

    Add.

    sort( $imagelarge );

    That should do it… 🙂

    Thread Starter brad8985

    (@brad8985)

    And it did 😉

    So how does “sort” work? Can I use that on any array, what is the default order that it uses?

    sort() orders by the array values.
    ksort() orders by the array key.

    There’s several other array sorting functions to, you see various functions listed down the left when viewing one of many of the PHP pages.
    http://php.net/manual/en/function.sort.php

    Some of them, sorting functions. 🙂

    Thread Starter brad8985

    (@brad8985)

    Cheers.

    You’re welcome.. 😉

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Custom Field Output is Random’ is closed to new replies.