Site logo

[Support request] How to display selected custom taxonomy items with individual icons?

Home Forums Support [Support request] How to display selected custom taxonomy items with individual icons?

Home Forums Support How to display selected custom taxonomy items with individual icons?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2545987
    Steve

    I’ve managed to display custom taxonomy items in the front end using the dynamic headline options.

    Thanks to this post:

    https://generatepress.com/forums/topic/custom-taxonomy-not-displaying-in-dynamic-data/

    I’m pretty sure I can get them to display as a list using the the snippet Fernando posted in #2453884

    Using custom CSS targeting the content of li::before, I reckon I can probably get a checkmark to display before each taxonomy item like this:

    Screenshot-2023-02-24-at-20-34-15

    But…

    How would I go about displaying a unique icon next to each taxonomy item like this please?

    Screenshot-2023-02-24-at-20-46-24

    As well as GP/GB Pro I’m using CPT UI and ACF Pro.

    #2546329
    Steve

    Here’s how I’m thinking I could approach this, please let me know if I’m barking up the wrong tree…

    1. use ACF to add a custom text field (used to add a class name) to the custom taxonomy so that each term gets its own unique class. This field would be called ‘acf_term_class’. I know how to do that.

    2. Use custom css to add a unique icon using the ::before pseudo-selector for each of those unique term class names. I know how to do that too.

    3. Somehow inject the unique class names into Fernando’s code (I don’t know how to do that but ending up with something like the below)

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-tax-headline' ) !== false ) {
    		$terms = get_field('custom-tax');
    		$output = '<ul>'; 
    		foreach( $terms as $term ){
    			$output = $output . '<li class="inject the term's unique classname here">' . '<a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a></li>';
    		}
    		$output = $output . '</ul';
    		return $output;
    	}
    	return $content;
    }, 10, 3);

    I’d also like to strip out the term links – not sure exactly the right way to do that either (I don’t know php) but perhaps this?:

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-tax-headline' ) !== false ) {
    		$terms = get_field('custom-tax');
    		$output = '<ul>'; 
    		foreach( $terms as $term ){
    			$output = $output . '<li class="inject the term's unique classname here">' . esc_html( $term->name ) . '</li>';
    		}
    		$output = $output . '</ul';
    		return $output;
    	}
    	return $content;
    }, 10, 3);

    As ever, any help would be most appreciated. Thanks.

    #2546415
    David
    Staff
    Customer Support

    Hi there,

    by default GenerateBlocks prints the term-{slug} for as a CSS class for each term.

    Heres an example of the HTML taken from the Newsroom site in the GP Site Library::

    <span class="post-term-item term-lifestyle"><a href="https://gpsites.co/newsroom/category/lifestyle/">Lifestyle</a></span>

    Which George the design took advantage of to change each terms color using custom CSS.

    And you could do the same:

    /* add generic styling for all pseudo items */
    .post-term-item:before {
        margin-right: 10px;
    }
    /* add content to each term */
    .post-term-item.term-lifestyle:before {
        /* do your content property thing */
    }
    
    #2546489
    Steve

    Thanks David – so the class I was after is there already!

    Was my edit to the php to remove the link around the term correct?

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-tax-headline' ) !== false ) {
    		$terms = get_field('custom-tax');
    		$output = '<ul>'; 
    		foreach( $terms as $term ){
    			$output = $output . '<li>' . esc_html( $term->name ) . '</li>';
    		}
    		$output = $output . '</ul';
    		return $output;
    	}
    	return $content;
    }, 10, 3);
    #2547269
    David
    Staff
    Customer Support

    Yeah – it was top of the list when it can to building the list of terms feature, as otherwise you had to use href attributes in your CSS, which isn’t very pretty.

    If you select the block showing the list of terms, in its dynamic data options, you can simply NOT set a link source, if you just want them as labels.

    #2548017
    Steve

    Sorry, I realise I made an error when referencing the other post with the add_filter example as that was talking about ACF taxonomy fields, not custom taxonomies as I thought.

    What I am trying to achieve here is to format the list of terms in the front end as a proper unordered list, so the list is wrapped in <ul> tags and each term is wrapped in an <li> tag.

    On a similar note, is it possible to use the Headline block to output the data from an ACF checkboxes field? I’ve not had any luck with that so far. I saw a post from April 22 saying it was on the list of things to do but wondered if that had been implemented yet in more recent versions of GenerateBlocks.

    I can use the acf shortcode to display the list of active checkbox terms but it just comes through as a comma separated list, and again I’d like each to have its own class and ideally be formatted as a proper list.

    #2548464
    David
    Staff
    Customer Support

    If you need to loop through repeater fields or checkboxes then you would need to use an ACF Shortcode for that now.
    GB Pro will handle that in a future update

    #2548883
    Steve

    OK thanks David, I’ll look into a Shortcode for the checkboxes.

    Regarding the Taxonomy List of Terms that GB does currently support though, how do I display that as a proper unordered list in the front end (with ul and li tags), rather that just a comma separated list please?

    #2548937
    David
    Staff
    Customer Support

    There is no support for UL/LI tags for headlines or buttons at this time.
    If you have GB Pro then you can use the blocks Advanced > Attributes to add a role = list attribute to the container, and role = listitem to the headlines. Those will then be treated as if they were a list.

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