• Hi basically i need to override the code below in funcitons php

    /**
     * Display list of post custom fields.
     *
     * @internal This will probably change at some point...
     * @since 1.2.0
     * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
     */
    function the_meta() {
    	if ( $keys = get_post_custom_keys() ) {
    		echo "<ul class='post-meta'>\n";
    		foreach ( (array) $keys as $key ) {
    			$keyt = trim($key);
    			if ( is_protected_meta( $keyt, 'post' ) )
    				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 know it involves add_filter or something but when i do this:

    add_filter('the_meta_key', 'modified_meta_matt', $priority = 1, $accepted_args = 1 ); 
    
    function modified_meta_matt() {
    	if ( $keys = get_post_custom_keys() ) {
    		echo "<ul class='post-meta'>\n";
    		foreach ( (array) $keys as $key ) {
    			$keyt = trim($key);
    			if ( is_protected_meta( $keyt, 'post' ) )
    				continue;
    			$values = array_map('trim', get_post_custom_values($key));
    			$value = implode($values,', ');
    			echo apply_filters('modified_meta_matt', "<li><span class='post-meta-key-3'>$key:</span> $value</li>\n", $key, $value);
    		}
    		echo "</ul>\n";
    	}
    }
    
    ?>

    It does work, but it displays the custom fields repeated like 6 times!
    thanks for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot use filters to actually override functions. Filters are used to alter what the function returns or outputs, which is usually just as good.

    In this case, you filter callback could be passed 3 parameters (from apply_filters()) for your use. Whatever your callback returns is immediately echoed out. Your callback is called once for every meta key, so there is no need to run your own loop.

    To receive all 3 parameters, add your filter callback with something more like this:
    add_filter('the_meta_key', 'modified_meta_matt', 10, 3);

    Thread Starter Mart89

    (@mart89)

    The part of the code I really need to change is "<li><span class='post-meta-key-3'>$key:</span> $value</li>\n"

    I just want to remove the list items and add in some divs

    Can you tell me what I need to put into my functions.php to allow this to happen?

    I may be misunderstanding what you want, but wouldn’t that just mean deleting
    <li> and </li> and adding <div> and </div> in its place? And also changing <ul class='post-meta'> and </ul> to <div class='post-meta'> and </div>.

    Moderator bcworkz

    (@bcworkz)

    And also changing <ul class='post-meta'> and </ul> to <div class='post-meta'> and </div>.

    Ah, there’s the rub. While it’s easy to filter the <li> items to something else, the <ul> portions are hard coded into the function. The only way to alter these would be to define an alternate function and call that instead. Depending on where the_meta() is called, this could be simple or impossible.

    AFAIK, there should be no problem with a <ul> block containing no <li> elements. There could be a validation warning, but browsers should not complain. To alter the <li> portion and leave the <ul> part as is, in addition to the add_filter() line I previously mentioned, you just need something like this:

    function modified_meta_matt($list, $key, $value) {
       return "<div class='my_meta_class'><span class='post-meta-key-3'>$key:</span> $value</div>\n";
    }

    Thread Starter Mart89

    (@mart89)

    Thanks bcworkz, that was really helpful! yeah w3 checker isnt happy about having a div as a child of a ul. I’m going to use it anyway unless anyone has a solution for this?

    thanks

    Thread Starter Mart89

    (@mart89)

    Actually I was able to achieve what I wanted by just putting the key items and values into separate list items and styling them as i would divs so cheers all!

    function modified_meta_matt($list, $key, $value) {
       return "<li class='my_meta_class_key'>$key:</li> <li class='my_meta_class_value'>$value</li>\n";
    }
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘customising custom field format’ is closed to new replies.