• Josh Ford

    (@joshford1maccom)


    I am using the code below to display a list of custom field meta data, excluding the fields ‘CFimage’ and ‘description’.
    It works as required except the order of the fields and values does not display consistently. I would like to display the field/value pairs in a specific order.
    Can anyone please help me achieve this?
    Thanks

    <?php
    if ( $keys = get_post_custom_keys() ) {
    	echo "<ul class='post-meta'>\n";
    	foreach ( (array) $keys as $key ) {
    		$keyt = trim($key);
    		if ( '_' == $keyt{0} || 'CFimage' == $keyt || 'description' == $keyt )
    			$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>", $key, $value);
    	}
    	echo "</ul>\n";
    }
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • You said you want to display in a specific order, but did not say what the order really is. Can you provide an explanation with examples?

    Thread Starter Josh Ford

    (@joshford1maccom)

    I’m using the custom fields to display a list of criteria for individual posts.
    There are 5 key/value pairs for each post. Key names are constant, values differ.
    Displayed as such….

    key1: value1
    key2: value2
    key3: value3
    key4: value4
    key5: value5

    Some posts might not include key4: value 4
    Or it might be that that field is added at a later date.
    I need the list to display in the same order for every post regardless of the order the user inputs them.

    OK. I think you want the list in order by key. Try sorting the $keys array by changing this:

    <?php
    if ( $keys = get_post_custom_keys() ) {
    	echo "<ul class='post-meta'>\n";

    to this:

    <?php
    if ( $keys = get_post_custom_keys() ) {
    	sort($keys);
    	echo "<ul class='post-meta'>\n";
    Thread Starter Josh Ford

    (@joshford1maccom)

    Thanks I’ll give it a go

    Thread Starter Josh Ford

    (@joshford1maccom)

    Are there any arguments I can add to that?

    Some. See https://php.net/manual/en/function.sort.php

    But what do you need it to do differently?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Order display of custom field meta’ is closed to new replies.