- This topic has 7 replies, 2 voices, and was last updated 7 months, 1 week ago by
David.
-
AuthorPosts
-
August 13, 2022 at 4:59 am #2311662
Tesco
I have two parent categories in my site. Each one has several child categories.
How can I replace/filter
the_content
if the post is inside a parent OR it’s child categories in thesingle.php
?August 13, 2022 at 9:54 am #2311963David
StaffCustomer SupportHi there,
you could create a helper function eg.
function db_category_has_parent() { $categories = get_the_category(); if ($categories[0]->category_parent > 0) { return true; } return false; }
This will check if the post category HAS a parent ( ie. its a child ).
Then you can use that in your
the_content
filter eg.function my_content_filter_function( $content ) { // If single post and category has parent if ( is_single() && 'post' == get_post_type() && db_category_has_parent() = ) { // do something for child category post } // if single post in parent category if ( is_single() && 'post' == get_post_type() && !db_category_has_parent() = ) { // do something for parent category post } return $content; } add_filter( 'the_content', 'my_content_filter_function');
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 15, 2022 at 11:38 am #2313694Tesco
Can I replace
the_content()
completely by any other thing when I need?I need to do something like this:
function my_content_filter_function( $content ) {
// If single post and category has parent
if ( is_single() && ‘post’ == get_post_type() && db_category_has_parent() = ) {
// here I will do something without calling the_content()
echo ‘blá, blá, blá…’;
}
// if single post in parent category
if ( is_single() && ‘post’ == get_post_type() && !db_category_has_parent() = ) {
// here I will do something BUT I will also call the_content()
echo ‘blá, blá, blá…’;
echo the_content();
}
return $content;
}
add_filter( ‘the_content’, ‘my_content_filter_function’);August 16, 2022 at 1:03 am #2314135David
StaffCustomer SupportDon’t use
echo
in your conditions. Just load the$content
variable with what you need and thenreturn
it.
eg.function my_content_filter_function( $content ) { // If single post and category has parent if ( is_single() && 'post' == get_post_type() && db_category_has_parent() = ) { // do something for child category post $content = 'this replaces the content'; } // if single post in parent category if ( is_single() && 'post' == get_post_type() && !db_category_has_parent() = ) { // do something for parent category post $content = 'this will be added before the content' . $content; } return $content; } add_filter( 'the_content', 'my_content_filter_function');
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 16, 2022 at 4:50 am #2314331Tesco
Not quite what I expected…
What I need is to manipulate the order where
the_content
renderseg.
// let's suppose that I have 3 variables: $string-one ="this is the first text"; $string-two ="this is the second text"; $string-three = $content; if ( is_single() && 'post' == get_post_type() && db_category_has_parent() = ) { // do something for child category post $content = $string-one; $content = $content; $content = $string-two; }
OR
$content = $string-two; $content = $string-one; $content = $content;
August 16, 2022 at 5:01 am #2314336David
StaffCustomer SupportAs an aside this is NOT theme related, and there is only so far i can go in teaching how PHP and WordPress functions work.
the_content
function can only be rendered once on a post.the_content
filter hook akaadd_filter( 'the_content', 'my_content_filter_function');
expects there to be one value returned to it. In your function callback that value is stored in$content
If you want to combine multiple strings then you can use the concatenate operator
.
:https://www.php.net/manual/en/language.operators.string.php
For example:
$content = $string-one . $content . $string-two;
or
$content = $string-one . $string-two . $content;
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 16, 2022 at 5:43 am #2314376Tesco
$content = $string-one . $content . $string-two; return $content;
I already tried that but it’s not respecting the order… It always show’s
$content
for lastAugust 16, 2022 at 6:40 am #2314441David
StaffCustomer SupportThen thats something up with your code.
This is an example of how it works:function my_content_filter_function( $content ) { $string_one = 'this is string one'; $string_two = 'this is string two'; if ( is_single() ) { $content = $string_one . $content . $string_two; } return $content; } add_filter( 'the_content', 'my_content_filter_function');
And that works.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.