Skip to content

Instantly share code, notes, and snippets.

@wpmark
Created March 17, 2023 10:08
Show Gist options
  • Save wpmark/d7348a593b60f2365c460a58e81d80ae to your computer and use it in GitHub Desktop.
Save wpmark/d7348a593b60f2365c460a58e81d80ae to your computer and use it in GitHub Desktop.
Modify the post terms block output in WordPress
<?php
/**
* Changes the links on post terms block for job listing category terms.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*
* @return string $block_content The block content.
*/
function hd_edit_job_category_links( $block_content, $block, $instance ) {
// if this is the post terms for job listing category.
if ( empty( $block['attrs']['term'] ) ) {
return $block_content;
}
if ( 'job_listing_category' !== $block['attrs']['term'] ) {
return $block_content;
}
// remove the tags from the content string.
$terms = wp_strip_all_tags( $block_content );
// split the terms at the comma.
$terms = explode( ',', $terms );
// if we have terms.
if ( empty( $terms ) ) {
return $block_content;
}
// store the new content as an array of term outputs.
$content = [];
// loop through each term.
foreach ( $terms as $term ) {
// if this terms is empty.
if ( empty( $term ) ) {
continue;
}
// get the term object.
$term = get_term_by( 'name', $term, 'job_listing_category' );
$content[] = '<a href="' . get_post_type_archive_link( 'job_listing' ) . '?_job_industry=' . esc_attr( $term->slug ) . '" rel="tag">' . esc_html( $term->name ) . '</a>';
}
// if we have no content.
if ( empty( $content ) ) {
return $block_content;
}
return '<div class="taxonomy-job_listing_category wp-block-post-terms">' . implode( '<span class="wp-block-post-terms__separator">, </span>', $content ) . '</div>';
}
add_filter( 'render_block_core/post-terms', 'hd_edit_job_category_links', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment