I am currently hooking on the_content() but that goes through the WordPress loop too. How can I only hook on the Single.php page?
Also, is there a way to only look on the first X posts in the WordPress loop?
By the way, I am creating a plugin
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This will handle appending the content to single posts:
function yourprefix_add_to_content( $content ) {
if( is_single() ) {
$content .= 'Your new content here';
}
return $content;
}
add_filter( 'the_content', 'yourprefix_add_to_content' );
Method 2
Just to add to Pippin’s answer, in my case some content were also being shown in other parts of the single page, e.g. sidebar. Checking just is_single() also triggered the content modification in the other areas. Here’s another check so that only the main content will have appended stuff:
function yourprefix_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
}
return $content;
}
add_filter('the_content', 'yourprefix_add_to_content');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0