Last active
May 2, 2017 19:02
-
-
Save themeblvd/65646575be6f7c5fdc68d03c1d53b613 to your computer and use it in GitHub Desktop.
When using Theme Blvd WordPress Framework, replace the category output in a post showcase with a custom field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add custom field after current <span class="item-title"> | |
*/ | |
function my_item_info( $output ) { | |
$output .= sprintf( '<span class="tagline">%s.</span>', esc_html( get_post_meta( get_the_ID(), 'my_custom_field', true ) ) ); | |
return $output; | |
} | |
add_filter( 'themeblvd_item_info', 'my_item_info' ); | |
/** | |
* Replace taxo term with custom field in post showcase. | |
*/ | |
function my_item_info() { | |
$post_id = get_the_ID(); | |
$output = '<span class="item-title">'; | |
$output .= sprintf( '<span class="title">%s</span>', esc_html( get_the_title( $post_id ) ) ); | |
$output .= sprintf( '<span class="cat tagline">%s</span>', esc_html( get_post_meta( $post_id, 'my_custom_field', true ) ) ); // Using class "cat" to inherit theme styling. | |
$output .= '</span>'; | |
return $output; | |
} | |
add_filter( 'themeblvd_item_info', 'my_item_info' ); | |
/** | |
* Add custom field after taxo, within wrapping <span>. | |
*/ | |
function my_item_info( $output ) { | |
$field = sprintf( '<span class="cat tagline">%s</span>', esc_html( get_post_meta( get_the_ID(), 'my_custom_field', true ) ) ); // Using class "cat" to inherit theme styling. | |
$pos = strrpos( $output, '</span>' ); // Find last occurrence of </span> | |
if ( $pos !== false ) { | |
$output = substr_replace( $output, $field, $pos, strlen('</span>') ); | |
} | |
return $output; | |
} | |
add_filter( 'themeblvd_item_info', 'my_item_info' ); | |
/** | |
* Replace taxo term with custom field in post showcase, | |
* only if custom field exists. | |
*/ | |
function my_item_info() { | |
$post_id = get_the_ID(); | |
$post_type = get_post_type( $post_id ); | |
$output = '<span class="item-title">'; | |
$output .= sprintf( '<span class="title">%s</span>', esc_html( get_the_title( $post_id ) ) ); | |
if ( $value = get_post_meta( $post_id, 'my_custom_field', true ) ) { | |
$output .= sprintf( '<span class="cat tagline">%s</span>', esc_html( $value ) ); // Using class "cat" to inherit theme styling. | |
} else { | |
if ( ! $tax ) { | |
$tax = 'category'; | |
if ( $post_type == 'portfolio_item' ) { | |
$tax = 'portfolio'; | |
} | |
} | |
$tax = apply_filters( 'themeblvd_item_info_tax', $tax, $post_type, $post_id ); | |
$terms_obj = get_the_terms( $post_id, $tax ); | |
if ( $terms_obj ) { | |
$terms = array(); | |
foreach ( $terms_obj as $term ) { | |
$terms[] = $term->name; | |
} | |
$output .= sprintf( '<span class="cat">%s</span>', esc_html( implode($terms, ', ') ) ); | |
} | |
} | |
$output .= '</span>'; | |
return $output; | |
} | |
add_filter( 'themeblvd_item_info', 'my_item_info' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment