Possibly, pass the post id into the function and test if the custom field exists for that post id.
Hi,
What I am actually trying to achieve is to have a separate page where my data could be displayed. At first I wanted the plugin to create a page automatically (by entering page name in options menu) but it seems not possible. So a user will have to create a page himself and add a custom field with function from plugin. Then this function will display content in that page.
I have found working example in another plugin, however I’m struggling to adapt it to my needs.
function load_runninglog($content) {
if (is_page()) {
$cust_values = get_post_custom_values($this->runninglog_show);
if ($cust_values != NULL) {
$log_target = $cust_values[0];
$content = $this->display_runninglog($log_target);
}
}
return $content;
}
function display_runninglog($log_target) {
echo "It's working!"
}
I get the following error. Fatal error: Using $this when not in object context in /var/www/wp-content/plugins/running-log/running-log.php on line 264.
I understand I need to get classes involved here. However I’m trying to avoid them as classes go beyond my meager programming knowledge. Is there a way to pass the post id into the function and test if the custom field exists for that post id without using classes?
Use global $post
function load_runninglog() {
global $post;
$content = "It's working! for post id ". $post->ID;
$custom_field_key = 'cf1'
$one_custom_field_value = get_post_meta($post->ID, $custom_field_key, true);
return $content;
}
add_filter('the_content', 'load_runninglog');
Hi Michael,
Thanks for helping me. It is working! I have added a cutom field value and key check and my content appears only on correct page now. That’s exactly what I wanted. However original content disappeared on all other pages and posts.
function load_runninglog() {
global $post;
$custom_field_key = 'load_runninglog';
$one_custom_field_value = get_post_meta($post->ID, $custom_field_key, true);
if ($one_custom_field_value == 'races') {
$content = 'It works! for post id '. $post->ID;
}
return $content;
}
How can I avoid messing up with other content?
You’ll still want to use the Conditional Tags such as is_page().
Thank you, Michael.
It’s working now. 🙂