• I’m having a problem with the custom field template. I have space in my template for four small thumbnailed screen shots. I use custom fields for this. My custom field options look like this:

    [Screenshot]
    type = textarea
    rows = 3
    cols=40
    mediaButton = true
    label = Screenshots
    sort = desc
    
    [Screenshot]
    type = textarea
    rows = 3
    cols=40
    mediaButton = true
    hideKey = true
    sort = desc
    
    [Screenshot]
    type = textarea
    rows = 3
    cols=40
    mediaButton = true
    hideKey = true
    sort = desc
    
    [Screenshot]
    type = textarea
    rows = 3
    cols=40
    mediaButton = true
    hideKey = true
    sort = desc

    The problem is the screenshots are not staying in order. Everytime the page is updated the values entered into these four fields are flipped. I’ve tried using sort = desc, sort = asc and sort = order but none of these seem to work. Can any point me to a way to keep these values in order?

    I guess I could change it to [screenshot1] [screenshot2] etc. but I’d rather stick with a simple loop in my template to output them.

    Thanks in advance

Viewing 1 replies (of 1 total)
  • Hi

    A while back I modified a WP function to sort keys returned when retrieving a post’s custom fields.

    If you named your fields Screenshot1, Screenshot2, etc. this routine would put them in the correct order and process them as you like. It loops through the array of keys and ignores those not containing “Screenshot” in their key name.

    The code is set up to output them as a list, using <ul> and <li>. But you can change the echo statements to output the results as you need. (This could be made into a function and placed in the theme’s functions.php file so the full code is not in your template file.)

    <?php if ( $keys = get_post_custom_keys() ) {
        echo "<ul class='post-meta'>\n";
        asort($keys);
        foreach ( (array) $keys as $key ) {
            $keyt = trim($key);
            if ( !substr_count($keyt, 'Screenshot' ))
               continue;
            $values = array_map('trim', get_post_custom_values($key));
            $value = implode($values,', ');
            echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
        }
        echo "</ul>\n";
    } ?>

    I modified my original code herem for your purposes here. I didn’t test it, but if it doesn’t work it should be very close, as in missing a parentheses, etc.

    Hope you find it useful.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Field Template and Ordering’ is closed to new replies.