• Phillip

    (@phillustration)


    Hi there,

    I am trying to work out how to limit the amount of words being pulled through when I use $content.

    My code looks like this:

    <?php
    $page_id = 89;
    $page_data = get_page( $page_id );
    $title = $page_data->post_title;
    $content = $page_data->post_content;
    echo '<h5>' . $title . '</h5>';
    echo $content;
    ?>

    I am trying to make it only display approx. 100 words without using excerpt etc. so as to not rely on my client to understand the <more> function.

    Any help appreciated, Thanks.

    Phillustration

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Phillip

    (@phillustration)

    Hi alchymyth,

    I looked at the page above. Thank you for your help.

    Although this strips out all of the html tags. Is there a way to keep the html styling and still limit the content? I just need the <h3> and <h4> tags to still work on the static page.

    <?php
    $page_id = 89;
    $page_data = get_page( $page_id );
    $title = $page_data->post_title;
    $content = $page_data->post_content;
    echo '<h5>' . $title . '</h5>';
    $trimmed_content = wp_trim_words( $content, 40, '<a href="'. get_permalink() .'"> ...Read More</a>' );
    echo $trimmed_content;
    ?>

    Thank you,

    Phil Vale

    Thread Starter Phillip

    (@phillustration)

    Hi Alchymyth,

    I managed to use the trim-words function and add html around it to achieve what i needed. Thank you very much.

    <?php
    $page_id = 89;
    $page_data = get_page( $page_id );
    $thumbnail = get_the_post_thumbnail( $page_id, $size, $attr );
    $title = $page_data->post_title;
    $content = $page_data->post_content;
    $trimmed_content = wp_trim_words( $content, 20 );
    echo '<div class="thumbnail">' . $thumbnail . '</div>';
    echo '<div id="slidecontent">';
    echo '<h5>' . $title . '</h5>';
    echo '<p>' . $trimmed_content . '</p>';
    echo '<a class="slidemore" href="'. get_permalink($page_id) . '">MORE</a>';
    echo '</div>';
    ?>

    Only problem now is that this pulls through the headings written on each page. An example of this is at: https://upg.phillipvale.com.au/

    In the mining slide that you can see in the top left corner. I don’t want to display the text “THIS IS THE MINING HEADING.”
    I was previously hiding this with css and display=none; Is there a way to start the trim_words from the fullstop in the heading?

    Confusing, but I thought I would just ask. ??

    Thank you,

    Phil

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Limit words displayed in $content’ is closed to new replies.