I am making an attempt to create a shortcode to show the put up classes with customized format (ex.: I have to show emoji acf discipline earlier than every class).
I am utilizing model 6.0 of WordPress and the Full Website Enhancing to create the templates.
I’ve added the shortcode within the single put up template and contained in the question loop block within the index template. It really works superb for every single put up web page, however within the index web page with all posts listed is displaying the identical class (the class of the primary put up) for each put up.
I am new to php and WordPress improvement. Any assistance on methods to remedy this concern will likely be a lot appreciated.
Code for the shortcode under:
operate my_theme_post_categories_shortcode() {
$post_id = get_the_ID();
$post_categories = get_the_category( $post_id );
$output="";
if ( $post_categories ) {
foreach( $post_categories as $class ) {
$category_emoji = get_term_meta( $category->term_id, 'emoji', true);
$category_color = get_term_meta( $category->term_id, 'category_color', true);
$output .= '<a href="' . esc_url(get_category_link( $category->term_id )) . '" model="coloration: var(--wp--preset--color--' . $category_color . ');">' . '<span class="emoji" aria-hidden="true">' . $category_emoji . '</span>' . $category->cat_name . '</a>';
}
}
return '<div class="post-categories">' . $output . '</div>';
}
add_shortcode( 'post_categories', 'my_theme_post_categories_shortcode' );
I’ve managed so as to add the emoji to the core classes record block that lists all classes by way of filters, with the next code. However I am unable to handle to do the identical for the core put up phrases classes that lists the classes of every put up.
operate my_theme_custom_list_categories( $record ) {
$classes = get_categories();
if ( $classes ) {
foreach( $classes as $cat ) {
$cat_emoji = get_term_meta( $cat->term_id, 'emoji', true);
$discover = '<a href="' . esc_url(get_term_link( $cat )) . '">';
$substitute="<a href="" . esc_url(get_term_link( $cat )) . '">' . '<span class="emoji" aria-hidden="true">' . $cat_emoji . '</span><span class="cat-name">';
$record = str_replace( $discover, $substitute, $record );
$discover = '</a>';
$substitute="</span></a>";
$record = str_replace( $discover, $substitute, $record );
}
}
return $record;
}
add_filter( 'wp_list_categories', 'my_theme_custom_list_categories');