Support » Plugin: Secondary Title » default secondary title for specific categories

  • Resolved Domeniko89

    (@domeniko89)


    Is it possible to insert the secondary title by default only for certain specific categories of articles? i tried to create a code snippet but it doesn’t work… thank you for your reply

    function adddd( $secodary_title, $post_id ) 
    {
        if( has_category( 103, $post_id ) ) { /* 103 my category */
            $secodary_title = '<span class="secondtitle">' . '101 motivi' .' <br></span>' . $secodary_title . ' ';
    		}
    
        return $secodary_title;
    }
    • This topic was modified 2 months ago by Domeniko89.
    • This topic was modified 2 months ago by Domeniko89.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author thaikolja

    (@thaikolja)

    Hi @domeniko89,

    Thanks for posting your question.

    Secondary Title has a couple of filter hooks that allow you to filter where and how the secondary title will show up. In this case, it’s the filter get_secondary_title.

    In that function, you’d have to check if the current post contains the category ID you’d like to have filtered out. You may also want to check if you’re on the Dashboard, otherwise, you won’t see the secondary title in the post overview.

    Here’s a snippet that should work. I added a few comments, I hope this helps.

    add_filter( "get_secondary_title", function ( string $secondary_title, int $post_id ) {
        /** @var \WP_Post $post The original post */
        $post = get_post( $post_id );
    
        /** @var int $category_id The category ID (!) that should be filtered out */
        $category_id = 1;
    
        /** @var string $title Our new title */
        $title = '';
    
        /** If we're in the Dashboard or the filtered category ID isn't part of the post, show secondary title */
        if ( is_admin() || ! in_array( $category_id, (array) $post->post_category ) ) {
            $title = $secondary_title;
        }
    
        /** Otherwise, use the default title */
        return (string) $title;
    }, 10, 2 );

    If it doesn’t help, please let me know. Also please note that this snippet just detects one category ID. However, checking for numerous category IDs is just a small adjustment.

    Thread Starter Domeniko89

    (@domeniko89)

    Hi thank you very much for the answer I tried but unfortunately it doesn’t work for me, I’m not very familiar with php. Could you help me?

    Plugin Author thaikolja

    (@thaikolja)

    Did you change the $category_id to the ID of the category you wanted to filter out?

    Thread Starter Domeniko89

    (@domeniko89)

    Yes of course, I have inserted category instead of 1, with 103 the one that interests me. If you want I can pass you the accesses of the site so you can check it yourself thanks.

    Plugin Author thaikolja

    (@thaikolja)

    If I’ve earned your trust (as many other before you have, see the reviews section), I can take a manual look for you. Please send me the (S)TFP details to my email address which is kolja.nolte @ gmail.com.

    Plugin Author thaikolja

    (@thaikolja)

    This issue was resolved via email.

    For anyone who might have the same problem, I can only give a temporary fix until the new version comes out.

    Thread Starter Domeniko89

    (@domeniko89)

    Dear, I really appreciate the help provided. You solved a big problem that I’ve been looking for secondary solutions for the intent for quite a while. I sincerely thank you for your help. I am glad that with my question you have also improved your plugin and will release a new version.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.