Title: SubStr function
Last modified: August 20, 2016

---

# SubStr function

 *  [Dezio](https://wordpress.org/support/users/dezio/)
 * (@dezio)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/)
 * Hello,
    I’ve a problem, I need to add the SubStr function to this part of code:
 *     ```
       $viewed_posts = get_user_meta($current_user->ID, 'nd_viewed_posts', true);
       		if (is_array($viewed_posts) && sizeof($viewed_posts)>0) :
       			echo '<ul class="linksin">';
       			$viewed_posts = array_reverse($viewed_posts);
       			foreach ($viewed_posts as $viewed) :
       				$viewed_post = get_post($viewed);
       				if ($viewed_post) echo '<li><a href="'.get_permalink($viewed).'">'.$viewed_post->post_title.'</a></li>';
       			endforeach;
       			echo '</ul>';
       ```
   
 * I try to add it like this:
    `substr($viewed_posts, 0, 5)."...";` But It doesn’t
   work.. why?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743677)
 * [http://codex.wordpress.org/Function_Reference/get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)
   
   [http://php.net/manual/en/function.substr.php](http://php.net/manual/en/function.substr.php)
 * what are you trying to achieve, and how is it not working?
 * where was the `substr()` code inserted?
 *  Thread Starter [Dezio](https://wordpress.org/support/users/dezio/)
 * (@dezio)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743736)
 * I’ve to shorten the post_title with “…” after 20 char
 *     ```
       $viewcorto = $viewed_post->post_title;
   
       		if (is_array($viewed_posts) && sizeof($viewed_posts)>0) :
       			echo '<ul class="linksin">';
       			$viewed_posts = array_reverse($viewed_posts);
       			foreach ($viewed_posts as $viewed) :
       				$viewed_post = get_post($viewed);
       				if ($viewed_post) echo '<li><a href="'.get_permalink($viewed).'">'substr($viewcorto, 0, 26);...'</a></li>';
       			endforeach;
       			echo '</ul>';
       ```
   
 * Im not a coder and something go wrong :/
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743752)
 * there seems to a problem with the concatenation of the string output;
 * try:
 *     ```
       if ($viewed_post) echo '<li><a href="'.get_permalink($viewed).'">'.substr($viewcorto, 0, 26).'...</a></li>';
       ```
   
 *  Thread Starter [Dezio](https://wordpress.org/support/users/dezio/)
 * (@dezio)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743763)
 *     ```
       $viewcorto = $viewed_post->post_title;
       		if (is_array($viewed_posts) && sizeof($viewed_posts)>0) :
       			echo '<ul class="linksin">';
       			$viewed_posts = array_reverse($viewed_posts);
       			foreach ($viewed_posts as $viewed) :
       				$viewed_post = get_post($viewed);
       				if ($viewed_post) echo '<li><a href="'.get_permalink($viewed).'">'.substr($viewcorto, 0, 10).'...</a></li>';
       			endforeach;
       			echo '</ul>';
       ```
   
 * It print just the “…” at the end fo line. Im not a coder but it seems to be correct:/
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743764)
 * are you trying to use the $viewcorto which is defined in the first line of your
   code, or the post title of the posts in the foreach loop?
 *     ```
       if ($viewed_post) echo '<li><a href="'.get_permalink($viewed).'">'.substr($viewed_post->post_title, 0, 10).'...</a></li>';
       ```
   
 *  Thread Starter [Dezio](https://wordpress.org/support/users/dezio/)
 * (@dezio)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743766)
 * `if ($viewed_post) echo '<li><a href="'.get_permalink($viewed).'">'.substr($viewed_post-
   >post_title, 0, 10).'...</a></li>';`
    This one works correctly! Thank you a lot
   🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘SubStr function’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [Dezio](https://wordpress.org/support/users/dezio/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/substr-function-1/#post-2743766)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
