Remove : (colon) from custom fields post meta key
-
Hi,
I use TwentyFourteen and checked my template-tags.php to remove the colon in custom fields post meta key. Unfortunately I can’t find it!
Anyone knows where it is?
<ul class='post-meta'> <li><span class='post-meta-key'>your_key:</span> your_value</li> </ul>
-
Where does the code above appear? Are you possibly using a customized TwentyFourteen? Check for a plugin and for a filter function in functions.php.
Not sure, it is not found in none of my functions.php. I only use
<?php the_meta(); ?>which output that code! (as find here http://codex.wordpress.org/Function_Reference/the_meta)I use TwentyFourteen Childtheme, but I kind of sorted this problem with
display:nonefor the span class post-meta-key. So the value displays what the post-meta-key should output without the colon 🙂OK. the_meta() is a function in wp-includes/post-template.php. You can override its output with a filter on the_meta_key, something like this:
function mam_filter_the_meta_key( $list_item, $meta_key='', $meta_value='' ) { $new_list_item = str_replace( ':</span>', '</span>', $list_item ); return $new_list_item; } add_filter( 'the_meta_key', 'mam_filter_the_meta_key' );This should go in your functions.php file in your child theme. Be careful when you are editing functions.php because any error will cause you site to become unusable, and you will need to use ftp to correct the error.
Thanks vtxyzzy, I’ll try the function soon!
Oh and also, it possible to add the same value to a specific key? It’s rather time-consuming to insert the same value for every needed post.
Not quite sure what you need. Are you talking about new posts? If so, you have to add the key anyway, so not much more trouble to add the value.
Yes, I actually want the same key and value for new upcoming posts.
Well the value is a textlink and I thought it would be easier just to insert some kind of shortcode there which echoed same text for different posts.Haha sorry i’m not the winner of ‘best in explanation’!
As long as you use some unique value, you could replace that value with the link. Just add another str_replace like this:
$new_list_item = str_replace( 'unique_value', 'put link here', $new_list_item );Thanks again, I’ll try!
Hi @vtxyzzy,
I just tried to replace the unique value with a link, but didn’t work.
Tested the str_replace in both functions.php and content.php, not sure I’m doing it right though but didn’t get any error.
My unique value for this specific field are here “utskick”.
$new_list_item = str_replace( 'utskick', 'http://www.makeupedia.se/pr-policy#pressutskick', $new_list_item );You have replaced ‘utskick’ with the URL, but not a link. Try this:
$new_list_item = str_replace( 'utskick', '<a href="http://www.makeupedia.se/pr-policy#pressutskick">put link text here</a>', $new_list_item );Hi,
So I tried a few methods but can’t make it work.
This is in content.php now:
<span class="prsample-link"><?php echo get_post_meta($post->ID, 'Pressutskick', true); ?></span>And functions.php
function mam_filter_the_meta_key( $list_item, $meta_key='Pressutskick', $meta_value='utskick' ) { $new_list_item = str_replace( 'utskick', '<a href="http://www.makeupedia.se/pr-policy#pressutskick">http://www.makeupedia.se/pr-policy#pressutskick</a>', $new_list_item ); return $new_list_item; } add_filter( 'the_meta_key', 'mam_filter_the_meta_key' );Can you give the HTML that you want to show after the replaces?
Let’s start over. You seem to have two separate problems.
- In existing posts, you want to remove the colon from the post_meta_key resulting from a call to the_meta().
- In new posts, you want to insert a link as if it came from a call to the_meta().
The first problem is addressed with the code I first gave.
I mistakenly tried to solve the second one as an extension to the first. I now realize that there would be no results from a call to the_meta() in a new post, so that approach will not work.
See this article for a method to solve the second problem: http://www.wpbeginner.com/wp-tutorials/how-to-add-custom-fields-automatically-on-post-publish-in-wordpress/
About the colon, I sorted it with
display:nonefor now, so will leave it until later 🙂In new posts I would like to add custom fields. But the value would preferably only be a one-word value, like “utskick” so I don’t have to insert loads of HTML-code in those custom fields where it is needed.
It shouldn’t be applicable for all posts, since the value would differs from category to another. Also, some of the value has more HTML-code and shows up in a popup-window, while others only display a textlink which directs to a new page.
I tried the functions here as well, http://www.wpbeginner.com/wp-tutorials/how-to-add-custom-fields-automatically-on-post-publish-in-wordpress/, but didn’t work.
I guess there something I must change here?:
add_action(‘publish_page’, ‘add_custom_field_automatically’);
add_action(‘publish_post’, ‘add_custom_field_automatically’);
The topic ‘Remove : (colon) from custom fields post meta key’ is closed to new replies.