• 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)
  • https://codex.www.ads-software.com/Function_Reference/get_post_meta
    https://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

    (@dezio)

    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 :/

    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

    (@dezio)

    $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 :/

    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

    (@dezio)

    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.