I am currently using this code, which shows the code beginning of every post. I want to show code in the Beginning and Ending of every post. Can anyone help with this?
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return esc_html__("So Friends, How is our Article of ".get_the_title().". Do You Like it? Don't Forget to Comment below if Any Queries. For More Article regarding ".get_the_title()." Subscribe Us.").$content;
}
return $content;
}
You already know the answer. In "the_content" filter you are prepending some text to the $content variable. Now what you want to do is append. Ex:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return "Beginning text" .$content . "Ending Text";
}
return $content;
}
"Beginning text" is the text that you have already added.
[2020 Jan 07]: Per OP request adding code snippet to change based on the pos category.
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
//get categories
$categories = get_the_category();
foreach($categories as $category){
//you can check by term_id, name, slug
if($category->$term_id == $target_term_id){
return "Beginning text" .$content . "Ending Text";
}
}
}
return $content;
}