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 7 months ago by Domeniko89.
    • This topic was modified 7 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)

    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.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator

    @thaikolja I’ve deleted your offer to login to your user’s site. I’m am 100% sure you mean well but please never ask for credentials on these forums.

    https://wordpress.org/support/guidelines/#the-bad-stuff

    Now for the why: The internet is a wonderful place full of very nice people and a few very bad ones. I’m sure everyone here is very nice however, by giving some ones keys to your house you are trusting they wont steal anything. Likewise the person who takes the keys is now responsible for the house FOREVER.

    If something was to go wrong, then you the author may well legally become liable for damages, which they would not normally have been as their software is provided without warranty.

    Please be aware that repeatedly asking for credentials will result in us escalating this to the plugins team.

    It’s never necessary to do that. Here’s why.

    There are many ways to get information you need and accessing the user’s site is not one of them. That’s going too far.

    You get the idea.

    Volunteer support is not easy. But these forums need to a safe place for all users, experienced or new. Accessing their system that way is a short cut that will get you into real trouble in these forums.

    You’ve been placed on “modwatch” until we’re convinced such postings have stopped. Your account has *not* been banned, we just want to check things for a while before they’re public. If you wish to take issue with this, contact moderators via the #forums channel on slack (https://make.wordpress.org/slack).  Please do not DM me or other moderators; use the channel.

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