I have a child theme in WordPress and I want to insert this code at the end of each single post:
<?php the_tags(); ?>
I don’t want to edit single.php. I would like to insert this code via functions.php
I found this thread and used the code in this answer: https://wordpress.org/support/topic/insert-info-into-bottom-of-posts#post-3990037
It worked well for me, but I cannot figure out how to insert my PHP code.
These are the things I tried but didn’t reflect what I wanted in the front end:
$content.= '<?php the_tags(); ?>';
$content.= ' the_tags();';
$content.= <?php the_tags(); ?>;
$content.= the_tags();
How can I alter the code in that WordPress thread to include php?
Thank you.
You’re attempting to concatenate the output of
the_tags
onto$content
, butthe_tags
does not return anything. When you callthe_tags
it sends output to the browser itself.the_tags
is pretty much just a wrapper aroundget_the_tag_list
which does return content as a string rather than outputting it. Try:Also, just to clarify exactly what is wrong with some of your attempts:
<?php
essentially means begin interpreting PHP, at this point I’m assuming you have already opened your PHP tags so there is no need to do it again and attempting to do so will result in an error.This will append the literal string
the_tags();
onto$content
. You cannot embed function calls into strings like this.This last line is just a combination of the two issues I just mentioned. It will result in the literal string
<?php the_tags(); ?>
being appended to$content
.