1

I have a coupon site and my main content is under category (store) pages.

I want to add default content to all stores so that i can get rid from adding content to each new store page i create.

Also i want to add default text after content (coupons) in store pages.

Anybody help me? Like where to add content or pass value to function?

Thanks in advance!

1 Answer 1

0

If I understand you correctly, you want some default content if you create a new post in the category "store".

Try to use the_content() filter here:

add_filter( 'the_content', 'add_default_content' ); 

function add_default_content( $content ) { 

    //run only when we are in the category "store"
    //could also be "coupons"
    if ( in_category('store') ) {

        //define your default text here
        $default = 'My default content is here.';

        // this will display the new default content first, 
        // after that the normal post-content you entered in the post-editor will be shown
        $content = $default . $content;

    }//END if is category

    return $content;
}

This will check if the current post is in that category.

Look at the line $content = $default . $content;

This line will first show our new default content ($default) and after this, it will display the post-editor content ($content).

If you have not entered any content in the post-editor, only the default text will show.

You can also rearrange this line too: $content = $content . $default;

In this case, the post-editor content will show first, and after this our default text.

So you could create 2 function, one for content before the post-editor and one after.


Update:

@sammy , OK, I just had a look at your site. It seems you are using a theme from http://www.premiumpress.com.

The code I posted above works for normal post categories, if you are using a custom type, taxonomies an terms, please try something like this:

if ( has_term('term-slug', 'taxonomy-slug') ) { ... }

Update 2:

You said 'store' is a taxonomy, so I just tested this:

I created a new taxonomy called 'store' and added some terms to that taxonomy. I edited some posts, and added some of the new store-taxonomy terms to these posts.

I added this code to my functions.php:

add_filter( 'the_content', 'add_default_content' );

function add_default_content( $content ) {

        // only show the default content if the post is in taxonomy 'store'
        // true if the current post has any of the given terms (or any term, if no term is specified)
        // we leave 'term' empty, we just specify the taxonomy
        if ( has_term( '', 'store' ) ) {

            //define your default text here
            $default = 'This is the default content.';

            // this will display the new default content first, 
            // after that the normal post-content you entered in the post-editor will be shown
            $content = $default . $content;

        }//END if has_term

        return $content;

    }

After this, I see the new content "This is the default content.", only on posts which are using the 'store' taxonomy. Regardless of which term is added.

As you can read in the codex, if we dont specify any term, the has_term() function will check if the post has any term of the taxonomy. So has_term() returns true.

8
  • Thanks for this answer but when i posted this code in functions . php the site becomes unavailable. Where can i post this code to work? @lws-mo
    – Sammy
    Commented Feb 20, 2017 at 14:23
  • I tested the code from my answer before I posted it, as a plugin file. And I just tested it now again in my functions.php. It is working without a problem. I just copied the above function, changed is_category to in_category and changed the slug. See if you entered the correct slug in if ( in_category('store') ) { ... } I just inserted "store" here cause I thought that is what you are using. Also what default content are you using now?
    – LWS-Mo
    Commented Feb 20, 2017 at 14:33
  • Yes am using a wordpress coupon theme from premiumpress, i think its a customized category page with name store. have edited functions.php as <?php add_filter( 'the_content', 'add_default_content' ); function add_default_content( $content ) { if ( has_term('store') ) { $default = 'My default content is here.Get upto 50% off on you order with our verified coupons and promo codes'; $content = $default . $content; } return $content; } ?>
    – Sammy
    Commented Feb 20, 2017 at 15:33
  • @sammy I think you need to clarify how your theme works! And if you are using custom content types or not. Read about taxonomies here and about post-types here. I would think "store" is the taxonomy and "coupon" is a term of that taxonomy. But I cant say and also dont use the themes from premiumpress. I updated my answer because I also tested has_term now.
    – LWS-Mo
    Commented Feb 20, 2017 at 15:51
  • But why i can't use themes from themes from premiumpress?
    – Sammy
    Commented Feb 20, 2017 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.