Support » Fixing WordPress » Custom Fields in Reverse Order

  • Resolved eminentstyle


    Hi, I have searched through the forums so hopefully this hasn’t been covered.

    I am using custom fields in a portfolio page listing 12 previous websites I have designed as a thumbnail that links to a detailed case study page. I wish to have my most recent work at the top of the page and the rest in chronological order. This works fine when setting up the page as when adding the custom field I simply list the most recent first and so on.

    However when I come to list a new piece of work, I am gonna have to cut and past all of the older work down the list as when adding new fields it appears a the bottom of the list not the top.

    The simplest solution would be to list them oldest first and keep adding to the list at the bottom. Then when I display them on the page they should be in reverse order. How do I do this? Here is the code I use at the moment in my page template that lists them in the order they appear in the custom fields:

    <?php $cs_href = get_post_custom_values("cs_href"); ?>
    <?php $cs_image = get_post_custom_values("cs_image"); ?>
    <?php $hover_text = get_post_custom_values("hover_text"); ?>  
    <?php for($i = 0; $i < count($cs_image); $i++) {?>
    <li><a href="<?php echo $cs_href[$i] ?>"><p><?php echo $hover_text[$i] ?></p><img src="<?php echo $cs_image[$i] ?>" alt="<?php echo $hover_text[$i] ?>" width="179" height="105" /></a></li>
    <?php } ?>

    I freely admit to not being a php guru, I am a designer not a coder. How do I change the ‘for’ statement or is there a better solution?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Justin Tadlock


    WordPress God

    While I don’t have the code for something like this on hand, you might try the array_reverse() function:

    Basically, it’ll just reverse everything in your array. Since custom field values are in an array, this will probably be what you’re looking for.

    @greenshady, thanks for the links, I had actually already looked at the first link but unfortunately my PHP is not great. I am not sure how to tie this in with WordPress.

    Can anyone help?

    Ok scrap that, with a bit of trial and error I have resolved this in the following way. I am sure there must be a ‘short hand’ way of achieving this but it appears to do exactly what I am after.

    <?php $cs_href = get_post_custom_values("cs_href"); ?>
    <?php $cs_href = array_reverse($cs_href); ?>
    <?php $cs_image = get_post_custom_values("cs_image"); ?>
    <?php $cs_image = array_reverse($cs_image); ?>
    <?php $hover_text = get_post_custom_values("hover_text"); ?>
    <?php $hover_text = array_reverse($hover_text); ?>

    Thanks again greenshady

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Fields in Reverse Order’ is closed to new replies.