[Resolved] display only main cateory (meta terms)

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support display only main cateory (meta terms)

This topic contains 9 replies, has 2 voices, and was last updated by  Tom 6 months, 1 week ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #7535

    Alessio
    Participant

    hi tom,

    when I enable meta > include terms, multiple categories are displayed (when a post has multiple categories)
    would it be possible to limit the display only to the main category? if yes… please help 🙂

    thank you for your great plugin
    alex

    #7552

    Tom
    Keymaster

    Hey Alex,

    How is the main category determined? The first one to show up?

    Let me know 🙂

    #7555

    Alessio
    Participant

    hi tom,

    thank you for your reply. as most of our posts belong to more than one category (or sub-category), I would like to only show one in order to keep the meta always only on one line and not on several lines as it happens now. which one is the main category is not really important as long as it is only one. the easiest to achieve, will be enough for us 🙂

    thank you again
    alex

    #7584

    Tom
    Keymaster

    Hey Alex,

    Give this a shot:

    add_filter( 'wpsp_terms_output', function() {
        $all_terms = get_the_terms( get_the_ID(), 'category' );
    	
        $first_term = $all_terms[0];
    	$url = get_term_link( $first_term->term_id );
    
        return sprintf( '<span class="wp-show-posts-terms wp-show-posts-meta"><a href="%1$s">%2$s</a></span>',
    		$url,
            $first_term->name
        );
    } );

    Let me know 🙂

    #7585

    Alessio
    Participant

    hey tom,

    thank you for your help
    unluckily, when I add the snippet, i receive the following error displayed where the category should appear

    Notice: Undefined variable: settings in /home/xxx/public_html/xxx/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()’d code on line 2

    alex

    #7588

    Tom
    Keymaster
    #7592

    Alessio
    Participant

    tried, but unluckily different error message

    Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /home/xxx/public_html/xxx/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()’d code:3 Stack trace: #0 /home/xxx/public_html/xxx/wp-includes/class-wp-hook.php(286): {closure}(‘<span class=”wp…’) #1 /home/xxx/public_html/xxx/wp-includes/plugin.php(203): WP_Hook->apply_filters(‘<span class=”wp…’, Array) #2 /home/xxx/public_html/xxx/wp-content/plugins/wp-show-posts/inc/functions.php(104): apply_filters(‘wpsp_terms_outp…’, ‘<span class=”wp…’) #3 /home/xxx/public_html/xxx/wp-content/plugins/wp-show-posts/inc/functions.php(133): wpsp_meta(‘below-title’, Array) #4 /home/xxx/public_html/xxx/wp-includes/class-wp-hook.php(286): wpsp_add_post_meta_after_title(Array) #5 /home/xxx/public_html/xxx/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #6 /home/xxx/public_html/xxx/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #7 /home/ in /home/xxx/public_html/xxx/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()’d code on line 3

    #7615

    Tom
    Keymaster
    #7619

    Alessio
    Participant

    YES 🙂
    thank you very much Tom, working perfecty
    Alessio

    #7653

    Tom
    Keymaster

    Glad I could help 🙂

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.