Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Render the_content for the my post-type
- */
- add_filter('the_content', 'my_the_content');
- function my_the_content($content) {
- //if the_content was triggered by relevanssi, we append the content of all relevant custom fields so that relevanssi includes their content (inlcuding the real my content if specified)
- if($post = is_the_content_called_by_relevanssi()){
- // Fields to load content from
- $fields = array('myfield1', 'myfield1');
- foreach($fields as $field){
- //pay attention to $single (http://codex.wordpress.org/Function_Reference/get_post_meta)
- //die(print_r(get_post_meta($post->ID, $field, false), TRUE));
- $content .= ' '. implode(' ', get_post_meta($post->ID, $field, TRUE));
- }
- }
- else global $post; //in general you want to use this $post object
- return $content;
- }
- /*
- * Check if the_content was triggered by relevanssi
- *
- * http://wordpress.org/support/topic/snippets-not-created-from-custom-fields?replies=11#post-2168264
- */
- function is_the_content_called_by_relevanssi(){
- //if relevanssi_init() does not exists, there is no relevanssi and relevanssi cannot have triggered the_contet
- if(!function_exists('relevanssi_init')) return false;
- /*
- * If we find a function call 'relevanssi_do_excerpt' in backtrace, the_content is triggered by relevanssis to create the excerpt from the_content
- *
- * expect not more than 10 the_content hooks
- * 21 calculates as follows:
- * apply_filters() + 10 * (call_user_func_array() + callbackfunction()) = 21
- * you might want to increase this. Remember it slows down you script!
- */
- //limit trace depth if php version is newer than 5.4.0
- if (strnatcmp(phpversion(),'5.4.0') >= 0)
- $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 21);
- else
- $trace = debug_backtrace();
- //search for relevanssi_do_excerpt() in backtrace
- for($i = 0; $i<min(array(21,count($trace))); $i++){
- //if we find relevanssi_do_excerpt in the stack return the $post object, which we need
- if('relevanssi_do_excerpt' == $trace[$i]['function']) return $trace[$i]['args'][0];
- }
- return FALSE; //nothing found
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement