• Resolved charlie67p

    (@charlie67p)


    Hi,

    Do you know how to replace the Div of the Post-terms block by another Html Tag ?

    <div class=”taxonomy-category wp-block-post-terms”><a href=”CategoryUrl” rel=”tag”>Category name</a></div>

    Thank you

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator threadi

    (@threadi)

    The HTML element is unfortunately fixed in the source code like this, see: https://github.com/WordPress/WordPress/blob/master/wp-includes/blocks/post-terms.php#L42

    I also don’t see a hook to change this.

    You still have 3 possibilities:

    • Develop a Gutenberg block yourself that creates such a list. Build their output the way you want it.
    • Send a request to the Gutenberg team: https://github.com/WordPress/gutenberg/issues
    • Leave it as it is. If you are concerned with design, the HTML element does not matter, CSS does. For semantic adjustments this list might have too little meaning I think.
    Thread Starter charlie67p

    (@charlie67p)

    Thank you for answering @threadi

    You’re right about the semantic

    Finally I wrote a simple str_replace and it works, but I am not sure that it is a ‘clean’ solution (?)

    function change_div_to_h2( $block_content ) {
    if ( is_home() ) { // here H2
    return str_replace('div','h2', $block_content);
    }
    else { // here DIV
    return $block_content;
    }
    }
    add_filter( 'render_block_core/post-terms', 'change_div_to_h2', 10, 1 );

    Moderator threadi

    (@threadi)

    Yes, that’s a good 4th option I hadn’t thought of. The code is basically fine, however I would make a short-exit in case it doesn’t apply. So like this:

    function change_div_to_h2( $block_content ) {
    	if ( !is_home() ) {
    		return $block_content;
    	}
    
    	// show post-terms as h2 instead of div on home.
    	return str_replace( 'div', 'h2', $block_content );
    }
    add_filter( 'render_block_core/post-terms', 'change_div_to_h2', 10, 1 );
    Thread Starter charlie67p

    (@charlie67p)

    OK
    Thanks !

    Moderator threadi

    (@threadi)

    Great if I could help. You are welcome to set the topic to solved.

    Thread Starter charlie67p

    (@charlie67p)

    🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Post-terms block : replace Div with a H2’ is closed to new replies.