• Resolved msmgking

    (@msmgking)


    I’m using the below on home page & archive pages to limit the post content character count beside a thumbnail image.

    This works great and as expected. However now I’m participating in a weekly post series which is wordless and only contains images. So as to keep the layout the same I would like to use a custom field on a per post basis to create my own teaser text. I can not get the syntax correct to make the custom field show up where I want it.

    This is all in the theme’s functions.php by the way.

    Can someone please point me in the correct direction I’ve read until my eyes cross and still must be looking right past it.

    <?php function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
        $content = get_the_content($more_link_text, $stripteaser, $more_file);
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
        $content = strip_tags($content);
    
       if (strlen($_GET['p']) > 0) {
          echo "<p>";
          echo $content;
          echo " <a href='";
          the_permalink();
          echo "'>"."[Read More]</a>";
          echo "</p>";
       }
       else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
            $content = substr($content, 0, $espacio);
            $content = $content;
            echo "";
            echo $content;
            echo " [...]";
            echo "<p class='readmore'><a href='";
            the_permalink();
            echo "'>".$more_link_text."</a>";
            echo "</p>";
       }
       else {
         echo $content;
            echo "Let's replace this text with a custom field";
            echo "<p class='readmore'><a href='";
            the_permalink();
          echo "'>"."[Read More]</a>";
          echo "</p>";
       }
    }
    ?>

    This is the text I would like to replace with my custom field key:

    echo “Let’s replace this text with a custom field”;

Viewing 1 replies (of 1 total)
  • Thread Starter msmgking

    (@msmgking)

    Solved it, just had to keep working on it!

    In case someone else may want to do something similar I’ll post the solution below.

    else {
       		global $wp_query;
    		$postid = $wp_query->post->ID;
            echo get_post_meta($postid, 'custom-field-name', true);
    		echo " [...]";
            echo "<p class='readmore'><a href='";
            the_permalink();
          echo "'>"."[Read More]</a>";
          echo "</p>";
       }
    }
    ?>

    The parts added/modified from what I was working with and previously posted:

    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, ‘custom-field-name’, true);

Viewing 1 replies (of 1 total)
  • The topic ‘Content_limit Functions.php Include Custom Field Key’ is closed to new replies.