There are a few issues that break your code:
$post
doesn’t exist, you’d need to import it usingglobal
, or simply useget_the_ID
as you’re already doing$check['']
won’t return anything since it will access an undefined index, outside of theempty
construct it would trigger a warning
I can’t test it right now, but this should work, at least syntax wise (and according to the API documentation theoretically also functionality wise):
function headway_title_button()
{
$post_id = get_the_ID();
if(metadata_exists('post', $post_id, 'titel_knop'))
{
printf(
'<div class="title-btn"><a href="https://wordpress.stackexchange.com/questions/67540/%s" alt="https://wordpress.stackexchange.com/questions/67540/%s"><img src="https://wordpress.stackexchange.com/questions/67540/%s" /></a></div>',
get_post_meta($post_id, 'titel_knop_url', true)
get_the_title(),
get_post_meta($post_id, 'titel_knop', true)
);
}
}
However it might be more efficient to fetch all metadata at once. But WordPress isn’t smart enough to unserialize it when not requesting a single value, so you would need to do that on your own (if neccessary at all, depends on the metadata):
function headway_title_button()
{
$meta = get_post_meta(get_the_ID());
if(!empty($meta['titel_knop'][0]))
{
printf(
'<div class="title-btn"><a href="https://wordpress.stackexchange.com/questions/67540/%s" alt="https://wordpress.stackexchange.com/questions/67540/%s"><img src="https://wordpress.stackexchange.com/questions/67540/%s" /></a></div>',
maybe_unserialize($meta['titel_knop_url'][0]),
get_the_title(),
maybe_unserialize($meta['titel_knop'][0])
);
}
}