0

I have a homepage that contains a hero div, in which I'm trying to create a preview which retrieves as much content from the site's About Us page (currently at http://localhost/wordpress/about/) to fill the container div, and then truncates it with a ... and a Read More link that takes them to the page itself to continue reading it.

How exactly would such a thing be accomplished in Wordpress and PHP? I've looked at several similar questions, but all seem to be concerned with retrieving either the content of a post or the current page, rather than displaying the content of another page.

I'm creating this site pro-bono for a non-profit charity to an important deadline, but unfortunately don't have anywhere near enough understanding of PHP to code this feature myself, so I'd really appreciate any help here.

Here's the most minimal version of the markup I can create:

<?php

get_header();

?>

<div class="hero-image-container">
        <div id="welcome-about-container">

            <div id="welcome-about-text">   
            <!-- About Us preview/PHP script goes here -->
            </div>

        </div>

</div>
Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68

1 Answers1

1

You can refer to WordPress with here: get post by post name instead of id to get your post by name or here via ID, if you know it with get_post().

$myAboutPage = get_page_by_title("About");
//echo '<pre>';
//var_dump($myAboutPage);
//echo '</pre>';
$myAboutPageExcerpt = substr($myAboutPage->post_content, 0, strpos($myAboutPage->post_content, ' ', 260)) . "...";
//echo '<pre>';
//var_dump($myAboutPageExcerpt);
//echo '</pre>';
$myAboutPageLink = $myAboutPageExcerpt . ' <a href="' . 
the_permalink($myAboutPage->ID) . '">' . __("Read more") . '</a>';
echo $myAboutPageLink;

With PHP you would then start with substr() the content of 0 and specify the end number to even create an excerpt. Here you would best end with a space. Making sure PHP substr finishes on a word not a character

You should also add the link with the_permalink() and an a tag. https://developer.wordpress.org/reference/functions/the_permalink/

If I understood you, that should be your solution. But if you create a template for all pages then use the_excerpt() or the_content()

I hope this helps.

prod3v3loper
  • 124
  • 10
  • I appreciate the links, but like I said, I don't know how to code in PHP, so I can't even begin to figure out how to put these together, and I don't currently have the time to learn. Could you give me something closer to a working snippet of code that actually combines these functions together? It will only be for one page, so doesn't need `the_excerpt()` or `the_content()`, and the permalink to my About page is `http://localhost/wordpress/about/`. Also, how would a solution coded with these functions handle the parent div being resized - will it automatically truncate the excerpt to fit? – Hashim Aziz Jun 25 '19 at 21:58
  • I have revised my comment and you made the solution pure. And you can do that with every post or article, just enter the title. – prod3v3loper Jun 25 '19 at 22:49