skip to Main Content

I am looking to write a plugin that executes code on individual post pages but does not try to execute that code in search results or archives or anything that shows multiple posts in one place. I tried using a filter on "the_content", but that seems to execute everywhere. Seems this would be a simple thing to find out, but the dual meaning of the word "filter" has made searching for this pretty difficult.

As a related question, is there a good source for seeing what filters and actions are fired for different types of pages in a theme? I’m sure I’ll have a similar problem in the future, and learning to fish will be helpful here.

Many thanks.

2

Answers


  1. You need to create a condition where you specify that your code should only be executed on the single post or product

    if(is_single('post_type')){
    // add your code here
    // Replace post type with the type of post you want this code to run on
    }
    
    Login or Signup to reply.
  2. And maybe this code will be a good guide for you

    function yourprefix_add_to_content( $content ) {    
        if( is_single() ) {
            $content .= 'Your new content here';
        }
        return $content;
    }
    add_filter( 'the_content', 'yourprefix_add_to_content' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search