Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I need to include default text to posts in a specific category. The co WordPress

  • SOLVED

I need to include default text to posts in a specific category. The code below is close but I need it to only apply to 1 category, in my case the category has an ID of 5 or is called Wanted Persons.

Is it possible?

<?php
add_filter('default_content', 'my_editor_content');

function my_editor_content( $content ) {
$content = "If you enjoyed this post, make sure to subscribe to my rss feed.";
return $content;
}
?>

Answers (5)

2019-08-13

Reigel Gallarde answers:

Please try something like this:

<?php
add_filter('default_content', 'my_editor_content', 10, 2);

function my_editor_content( $content, $post ) {
if ( has_category(5, $post ) ) {
$content = "If you enjoyed this post, make sure to subscribe to my rss feed.";
}
return $content;
}
?>


twdesigns comments:

Didn't work. :( Ideas?


Reigel Gallarde comments:

can you please describe what's happening now that you've used my code?


Reigel Gallarde comments:

is the category a post category or custom taxonomy?


Reigel Gallarde comments:

if that's a custom taxonomy, you can try something like this.
<?php
add_filter('default_content', 'my_editor_content');

function my_editor_content( $content) {
if ( has_term(5) ) {
$content = "If you enjoyed this post, make sure to subscribe to my rss feed.";
}
return $content;
}
?>


Reigel Gallarde comments:

Any update on this mate?


twdesigns comments:

It is a standard category and nothing happened at all. If I use the example code I posted it works everywhere. So far no ones code singles it to category 5 only.


Reigel Gallarde comments:

Seems odd. I have tried it and it works as expected on my end.

You may also try instead of the id 5, you can use the category slug.
That is on my first suggested code above.
Assuming your slug for "Wanted Persons" is "wanted-persons", has_category('wanted-persons', $post )


twdesigns comments:

I'm not sure if what I want is possible? If I click new post, the code I posted automatically fills in the content box with my text. Is it even possible for it to fill in the content box of a new post once you select the category?


Reigel Gallarde comments:

I see now. No it's not possible.
Because selecting category happens only on the client side, this case is the browser.

When writing, there's no category yet unless you submit the post.
`default_content` is called before saving the post. `default_content` is already called before the post will have a category.

2019-08-13

Darlene Grace Arcenal answers:

Please try this code
<?php
add_filter('the_content', 'my_editor_content');
function my_editor_content( $content ) {
global $post;
if ( is_single() ) {
if ( in_category(5, $post->ID ) ) {
$content .= 'If you enjoyed this post, make sure to subscribe to my rss feed.';
}
return $content;
}
}
?>


twdesigns comments:

This gets me closer. Is there any way to make it the "default content" rather than adding to "the content" field?


Darlene Grace Arcenal comments:

Yes. Please change this part

$content .= 'If you enjoyed this post, make sure to subscribe to my rss feed.';

to this

$content = 'If you enjoyed this post, make sure to subscribe to my rss feed.';

2019-08-13

Arnav Joy answers:

Try this


<?php
add_filter('default_content', 'my_editor_content');

function my_editor_content( $content) {
global $post;
if ( has_category(5, $post ) ) {
$content = "If you enjoyed this post, make sure to subscribe to my rss feed.";
}
return $content;
}
?>


twdesigns comments:

Same as above didn't appear in the create new post or on existing category 5 posts.

2019-08-13

Krishna Tiwari answers:

Hi,
Please add bellow code into functions.php
add_filter('default_content', 'my_editor_content', 1, 1);
function my_editor_content( $content) {
global $post;
$term_id = get_the_category( $post->ID )[0]->term_id;

if($term_id == 18){ //18 is category id please change according your condition
$content ='content to add to a post';
}
return $content;
}

and add bellow code where you display the_content (single.php)
echo $post->post_content = (string) apply_filters( 'default_content', $post_content, $post );

Thanks,
Krishna

2019-08-13

Rowela Alzona answers:

Hi try this steps:

Step 1:
Make a duplicate on your category.php and make the new one to category-wanted-persons.php

Step 2:
Insert the code function below


<?php echo “Message here”; ?>