Use filter on the content when it has shortcodes and excerpts
-
0 down vote favorite
I guess I’m doing wrong, but I’m writting a script which appends some content to my pages contents. The problem is that on my homepage for example, I have some text, some shortcodes, and in those shortcodes, I have posts and so, posts excerpts.
When I apply the the_content filter on my homepage and return the content, all my shortcodes excerpts are gone and their full content is returned instead aswell.
Is there anyway to retrun exactly the content the way it looks like before applying the filter ? Here is my piece of code.
function echo_post_type( $content ) { global $post; // target contents $content_targets = array( 'event', 'post', 'page' ); if ( ( is_single() && in_array( $post->post_type, $content_targets ) ) || is_page() ) { $itemscope = array(); // will receive the code to append if ( $post->post_excerpt == '' || $post->post_excerpt == null ) $description = wp_trim_words(strip_tags($post->post_content), 55); $author = get_userdata( $post->post_author ); $image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); $date = date_i18n( get_option( 'date_format' ), strtotime($post->post_date) ); // if event $eo_meta_full = get_post_meta( $post->ID ); $eo_meta = get_post_meta( $post->ID, '_eventorganiser_event_schedule', true ); $startDate = $post->StartDate; $endDate = $post->EndDate; if( eo_get_venue() ) $place = eo_get_venue_name(); switch ($post->post_type) { case 'post': $itemscope[] = '<div class="microdata" itemscope itemtype="https://schema.org/BlogPosting">'; $itemscope[] = '<span itemprop="name">' . $post->post_title . '</span>'; $itemscope[] = '<span itemprop="thumbnailUrl">' . $image . '</span>'; $itemscope[] = '<span itemprop="author">' . $author->data->display_name . '</span>'; $itemscope[] = '<span itemprop="description">' . $description . '</span>'; $itemscope[] = '</div>'; break; case 'event': $itemscope[] = '<div class="microdata" itemscope itemtype="https://schema.org/Event">'; $itemscope[] = '<span itemprop="name">' . $post->post_title . '</span>'; $itemscope[] = '<span itemprop="startDate">' . $startDate . '</span>'; $itemscope[] = '<span itemprop="endDate">' . $endDate . '</span>'; $itemscope[] = '<span itemprop="location">' . $place . '</span>'; $itemscope[] = '<span itemprop="description">' . $description . '</span>'; $itemscope[] = '</div>'; break; default: // other pages including homepage $itemscope[] = '<div class="microdata" itemscope itemtype="https://schema.org/WebPage" style="border: 1px solid red">'; $itemscope[] = '<span itemprop="name">' . $post->post_title . '</span>'; $itemscope[] = '<span itemprop="lastReviewed">' . $date . '</span>'; $itemscope[] = '<span itemprop="thumbnailUrl">' . $image . '</span>'; $itemscope[] = '<span itemprop="author">' . $author->data->display_name . '</span>'; $itemscope[] = '<span itemprop="description">' . $description . '</span>'; $itemscope[] = '</div>'; break; } foreach ($itemscope as $scope) { $content .= $scope; } return $content; } } add_filter( 'the_content', 'echo_post_type' );
Shematically :
Page content -> shortcodes, Shortcodes -> post excerpts
I need : append custom content to page content
I do : filter page content, append my custom content, return the content
Result : Custom content appended, Shortcodes’ excerpts disappear, Full contents displayed instead
Thank you
- The topic ‘Use filter on the content when it has shortcodes and excerpts’ is closed to new replies.