WordPress.org

Forums

[resolved] explode custom field not working (3 posts)

  1. wassem mansour
    Member
    Posted 2 years ago #

    hi
    i am trying to use this code to explode data from custom field by name (schools) which includes data like : one,two,three
    but it keeps returning this error

    Warning: trim() expects parameter 1 to be string, array given in C:\xampp\htdocs\***\wp-content\themes\***\schools.php on line 20

    anyhelp ?

    <div class="schools_names">
    		<?php			
    
    		$school = get_post_meta($post->ID, 'schools', false);
    		$school = explode(',' , $school);
    
    		?>
    		<h3>colleges:</h3>
    		<p>
    		<?php
    		foreach($school as $src)
    		{
    		echo '<a href="#" target="_blank">'.trim($src).'</a> - ';
    		}
    		?>
    		</p>
    	</div>
  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/get_post_meta

    using 'false' as the third parameter ($single) would return an array:

    If false, or not set, then the function returns an array of the custom fields.

    if you have just a string with comma-separated words in your custom field, try to set that parameter to 'true'.

  3. wassem mansour
    Member
    Posted 2 years ago #

    yes thank you , Worked perfect.

Topic Closed

This topic has been closed to new replies.

About this Topic