Filter content with new content structure
-
Hi.. i tried to applying the custom fileds on post (post type) but need a snippet to filter content with new content structure like below :
custom fields 1
content
custom fields 2so if i added the social share it fits above (new) content and below title.
any suggestion ?
-
Well that was interesting. I’ve never used Custom Fields before.
For those that don’t know: In the screen options in a post, check the box to display Custom Fields. Add your Custom Fields at the bottom of the screen (make sure you don’t touch the Customizr ones, or you may get unpredictable results).
Say you added two separate fields called ‘temperature’ and ‘precipitation’. You can then display them in the post list and on single posts with the following in the functions.php of your child theme:
add_filter ( 'tc_post_metas' , 'my_temperature_post_metas' ); function my_temperature_post_metas( $html ) { // Get custom field data for temperature $custom_field_data = get_post_meta(get_the_ID(), 'temperature', true); return $html . '<span class="post-temperature-label">Degrees C: </span><span class="post-temperature-reading">' . $custom_field_data . '</span>'; } add_action ( '__after_loop' , 'my_precipitation_post_metas' ); function my_precipitation_post_metas( $html ) { // Get custom field data for precipitation $custom_field_data = get_post_meta(get_the_ID(), 'precipitation', true); echo '<span class="post-precipitation-label">Rainfall: </span><span class="post-precipitation-reading">' . $custom_field_data . '</span>'; }This will display them after the post metas (“This entry was posted…” etc) and after the post content (before the separator).
You can then style the labels/information with the classes included above:
- post-temperature-label
- post-temperature-reading
- post-precipitation-label
- post-precipitation-reading
Let us know how you get on.
or instead of tc_post_metas use the action hook
__after_content_titleand don’t think__after_loopis a good choice since is outside the<article>tag. Would be better__after_content, in my opinionThanks Rocco. I had tried the
__after_content_titlehook, but it places the text before the metas and doesn’t look too good. afjsystem can take his/her choice 🙂__after_content: Good idea. I also spotted an extraneous parameter that I had left in there, so the code becomes:add_filter ( 'tc_post_metas' , 'my_temperature_post_metas' ); function my_temperature_post_metas( $html ) { // Get custom field data for temperature $custom_field_data = get_post_meta(get_the_ID(), 'temperature', true); return $html . '<span class="post-temperature-label">Degrees C: </span><span class="post-temperature-reading">' . $custom_field_data . '</span>'; } add_action ( '__after_content' , 'my_precipitation_post_metas' ); function my_precipitation_post_metas() { // Get custom field data for precipitation $custom_field_data = get_post_meta(get_the_ID(), 'precipitation', true); echo '<span class="post-precipitation-label">Rainfall: </span><span class="post-precipitation-reading">' . $custom_field_data . '</span>'; }afjsystem: if you want to use Rocco’s suggestion for the first hook, then the first 2 lines of the above code become:
add_action ( '__after_content_title' , 'my_temperature_post_metas' ); function my_temperature_post_metas() {and the
return $html . '<span class=...line becomesecho '<span class=...Well, after content title with an higher value for the priority than the one used by tc_post_metas, which is 10 by default, so
add_action ( '__after_content_title' , 'my_temperature_post_metas', 15 )Anyway mine and your solution will work only if there’s a title. So..
__before_contenthook looks like a better, and however more consistent, solution. 🙂 So:add_action ( '__before_content' , 'my_temperature_cf' ); function my_temperature_cf() { // Get custom field data for temperature $custom_field_data = get_post_meta(get_the_ID(), 'temperature', true); echo '<span class="post-temperature-label">Degrees C: </span><span class="post-temperature-reading">' . $custom_field_data . '</span>'; } add_action ( '__after_content' , 'my_precipitation_cf' ); function my_precipitation_cf() { // Get custom field data for precipitation $custom_field_data = get_post_meta(get_the_ID(), 'precipitation', true); echo '<span class="post-precipitation-label">Rainfall: </span><span class="post-precipitation-reading">' . $custom_field_data . '</span>'; }🙂
Hadn’t thought about priorities. Thanks!
The topic ‘Filter content with new content structure’ is closed to new replies.
