• Resolved hgassner

    (@hgassner)


    Hello!

    First thanx for this great PlugIn!

    I’m using user defined fields to change the post title for specific posts, e.g. adding “guest post” to the title, if the user defined variable ‘Gastbeitrag’ is set. See the code below.

    // add text for guest post to title
    add_filter( ‘the_title’, ‘ai_guest_post’ );
    function ai_guest_post( $title )
    {
    global $post;

    $guest_post = get_post_meta($post->ID, ‘Gastbeitrag’, true);
    if ($guest_post == ‘true’ && in_the_loop() )
    {
    $guest_text = “<br><span style=’font-size: 50%; font-style: italic’>Gastbeitrag</span>”;
    return $title.$guest_text;
    }

    return $title;
    }

    But now when displaying a guest post I see the added “guest post” (‘Gastbeitrag’ in the code above) text for ALL posts in the TOP 10 list and not only for guest posts.

    So it seems that TOP 10 uses the postId of the displayed post for all posts in the TOP 10 list when setting the title.

    Any help would be appreciated!

    Thank you very much
    Horst

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ajay

    (@ajay)

    Can you try using the function to filter tptn_post_title

    Thread Starter hgassner

    (@hgassner)

    Thank you very much for your answer!
    Unfortunately this does not work but the output has changed.

    The header of the post does not show “Gastbeitrag” anymore and the format of the top list changed a bit.

    Since it is not possible to uploaf pictures here directly I’ve uploaded them in a public area. Please take a look – it might help you finding another answer.

    the_title
    tptn_post_title

    Have fun
    Horst

    Plugin Author Ajay

    (@ajay)

    add_filter( ‘ tptn_post_title’, ‘tptn_ai_guest_post’, 10, 2 );
    function tptn_ai_guest_post( $title, $result )
    {
    $guest_post = get_post_meta($result->ID, ‘Gastbeitrag’, true);
    if ($guest_post == ‘true’ && in_the_loop() )
    {
    $guest_text = “<br><span style=’font-size: 50%; font-style: italic’>Gastbeitrag</span>”;
    return $title.$guest_text;
    }
    
    return $title;
    }
    

    I’ve changed the code around, but can you let me know if this works.

    Another option if you don’t want to display Gastbeitrag, you could filter the tptn_post_title to have a filter priority 99 and strip out the $guest_text you have above that was added

    Thread Starter hgassner

    (@hgassner)

    Thank you Ajay! Played around with your suggested solution and it principally worked.

    But I then had no more “Gastbeitrag” text in the header of my singles post and archives. But your suggestion helped a lot!

    Here is my complete and working solution (‘Gastbeitrag’) in post title and only in these posts in the top list which are indeed guest posts.

    // =================================================================================
    // guest posts
    // =================================================================================
    
    // add text for guest post to title (non top list areas like post header and archives)
    add_filter( 'the_title', 'ai_guest_post' );
    function ai_guest_post( $title )
    {  
       global $post;
       $guest_text = "Gastbeitrag";
    
       $guest_post = get_post_meta($post->ID, 'ai_guest_post', true); 
       if ($guest_post == 'true' && in_the_loop() )
       {
    	  return $title."<br><span style='font-size: 50%; font-style: italic'>".$guest_text."</span>";
       }  
    
       return $title;
    }
    
    // handle text for top list
    add_filter( 'tptn_post_title', 'tptn_ai_guest_post', 99, 2 );
    function tptn_ai_guest_post( $title, $result )
    {  
       // first always remove text added by 'the_title' filter
       $guest_text = "Gastbeitrag";
       $title = str_replace($guest_text, '', $title);
    
       $guest_post = get_post_meta($result->ID, 'ai_guest_post', true);
       if ( $guest_post== 'true' && in_the_loop())
       {
          // now add text for top list
    	  return $title."<br><span style='font-size: 75%; font-style: italic'>".$guest_text."</span>";
       }  
    
       return $title;
    }
    • This reply was modified 7 years, 7 months ago by hgassner.
    Plugin Author Ajay

    (@ajay)

    Glad to know this work. I’ll do a bit of testing at my end as well to see how I can selectively add this. The plugin uses get_the_title which I suspect why this is triggering. Bit surprised that in_the_loop is validating true for my posts list as well… that’s for another day!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Invalid title when using user defined fields’ is closed to new replies.