Insert PHP within function

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

Read More

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.

Related posts

Leave a Reply

1 comment

  1. You’re attempting to concatenate the output of the_tags onto $content, but the_tags does not return anything. When you call the_tags it sends output to the browser itself.

    the_tags is pretty much just a wrapper around get_the_tag_list which does return content as a string rather than outputting it. Try:

    $content .= get_the_tag_list();
    

    Also, just to clarify exactly what is wrong with some of your attempts:

    $content.= <?php the_tags(); ?>;
    

    <?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.

    $content.= ' the_tags();';
    

    This will append the literal string the_tags(); onto $content. You cannot embed function calls into strings like this.

    $content.= '<?php the_tags(); ?>';
    

    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.