Joy
(@joyously)
It sounds like you understood it.
PHP is executed as it is encountered. So if you are in a theme page template file, and you use the_content(), that function will echo the result regardless of where you have it coded. It could be as a function parameter, but since it echoes and does not return a value, the content will be output right there, and the function you called will get nothing (no return value).
So if you need to manipulate the value before output, use the functions that start with get. If you don’t need to do anything except output, use the functions that start with the.
Basically, the the functions echo the get functions, often with a filter.
After looking at your code, I will confuse you further. 🙂
Many themes and plugins alter post content via the the_content filter. The get_the_content() function does not apply this filter, but the the_content() function does. More info:
https://developer.ww.wp.xz.cn/reference/functions/get_the_content/#user-contributed-notes
I bring this up because your code is not applying the the_content filter. That means it’s possible the excerpt you’re creating will not represent the same content that is displayed in the actual post.
Thread Starter
GusGF
(@gusgf)
Oh no don’t confuse me further Dion Designs!!!
Thanks Joy I get what you said.
But I’m still confused. Presumably we want to end up with a fully formed piece of HTML like this line in the php file…
<a href=”http://test.local/2019/01/03/miniature-uav/”
And this seems to do the job…
<a href="<?php the_permalink(); ?>
But I’m confused as to why it works as you’ve explained that when the php is interpreted the function the_permalink() doesn’t return anything to the HTML as the result is directly echoed to the web page. To me that means it’s echoes to the users screen as a text string!! So this line after the PHP is interpreted in my understanding becomes…
<a href=”” and in theory it does nothing. Can you see what I’m saying? This is very confusing 🙁
-
This reply was modified 7 years, 4 months ago by
GusGF.
-
This reply was modified 7 years, 4 months ago by
GusGF.
-
This reply was modified 7 years, 4 months ago by
GusGF.
-
This reply was modified 7 years, 4 months ago by
GusGF.
-
This reply was modified 7 years, 4 months ago by
GusGF.
You said “doesn’t return anything to the HTML”. That statement is erroneous and why I think you are confused. HTML cannot accept returned values, only PHP code can. Any HTML encountered on a PHP page could be considered as a series of echo statements where the echo command is hidden or implied. Consider this HTML in a PHP page:
<div>This is HTML content</div>
The following would appear exactly the same in the client browser:
<?php echo '<div>This is HTML content</div>'; ?>
So in a way, HTML is much like a PHP string. A string itself cannot receive return values. But you can concatenate a return value to a string:
<?php echo 'A PHP string ' . get_the_content(); ?>
Here the dot operator is receiving the return, not the string itself, which is then echoed out. This has the exact same result:
<?php echo 'A PHP string '; echo get_the_content(); ?>
So using your example:
<a href="<?php the_permalink(); ?>">
This has the exact same result:
<?php echo '<a href="'; echo get_the_permalink(); echo '">';?>
I am hoping that if you see all HTML on a PHP page as implied echo statements, the reason why <a href="<?php the_permalink(); ?>"> works makes sense.
You also reference two different destinations for output: the web page, and the user’s screen. Usually “user’s screen” refers to the local terminal screen with which the user is interacting with the computer running the software. This never happens with PHP running on a web server. All PHP output is sent out over the Internet to the client browser. There’s no “user’s screen” per se.
Basically, your mental model of what’s happening is flawed. I know that altering such models once established can be very difficult. Somehow, you need to change how you are picturing what’s going on. I hope this all helps you do that.
Thread Starter
GusGF
(@gusgf)
This was an excerpt from the WP course I’m doing:
Well there’s a rule of thumb that if a WordPress function begins with the word get that means it’s not going to echo anything for you it’s just going to return a value and it’s up to you to use that value however you see fit. On the other hand if a function begins with the word the that means WordPress will indeed handle echoing and outputting it onto the page for you. Now at this point in the course I say we just leave it at that.
On the face of it I found this confusing when I looked closer in retrospect, initially it made sense based on a false assumption I’d made. When he refers to
outputting it onto the page for you
I now know this means when the PHP interpreter is executing and the page is being built before being sent to the user. There is a subtle difference here which was lost on me, being a beginner it’s an easy mistake to make. I’m not sure I’m explaining myself or my confusion very well here but I believe I now understand and your explanation was very helpful.
Here we are echoing the link to the page to build the HTML.
<a href="<?php the_permalink(); ?>">
Whereas here we are returning a value first for further processing by the wp_trim_words function and then echoing to build the HTML.
<?php echo wp_trim_words(get_the_content(), 18) ?>
As I say for a beginner these nuances can easily be missed whereas to seasoned WP devs this is so obvious.
Thank you so much for taking the time to explain 🙂
-
This reply was modified 7 years, 4 months ago by
GusGF.
-
This reply was modified 7 years, 4 months ago by
GusGF.
Thread Starter
GusGF
(@gusgf)
This works and limits the output to 18 characters….
<?php echo wp_trim_words(get_the_content(), 18) ?>
But this doesn’t, in this case the full string is returned which puzzles me.
<?php echo wp_trim_words('This is just a test to see how many characters I can fit in', 18) ?>
The wp_trim_words() function trims to a certain number of words, not characters.
Thread Starter
GusGF
(@gusgf)
Ooops yes, thank you for pointing that out.
It seems like your understanding has improved greatly! Don’t worry, we’ve all undergone similar experiences when starting out. Initially there is so much to be confused about it’s mind numbing. At the risk of adding confusion, please recall that Dion mentioned applying “the_content” filter in order to be sure your trimmed output matches what we would see on a normal blog listing.
the_content() applies this filter and echoes the filtered content. get_the_content() does neither. You can add an apply_filters() call before trimming to capture any changes to content that may occur from the filter.
<?php echo wp_trim_words( apply_filters('the_content', get_the_content()), 18); ?>
Also be aware that wp_trim_words() strips HTML tags before doing the word count. Stripped tags are not restored. This can be quite annoying, but handling word count in a more sustainable way gets very complicated if we wish to maintain HTML that properly validates.
Thread Starter
GusGF
(@gusgf)
Thank you so much bcworkz and as you rightly pointed out my understanding which was initially flawed is now far better. I knew PHP was a server language but somehow I’d forgotten this. Haven’t tackled the the_content() function and filters just yet 🙂
Again thanks to you and everyone else for chipping in 🙂