WordPress.org

Ready to get started?Download WordPress

Forums

SubStr function (7 posts)

  1. Dezio
    Member
    Posted 1 year ago #

    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?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/get_post_meta
    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?

  3. Dezio
    Member
    Posted 1 year ago #

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

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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>';
  5. Dezio
    Member
    Posted 1 year ago #

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

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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>';
  7. Dezio
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags