From: Anthony Talarico Date: Fri, 1 Sep 2017 14:54:00 +0000 (-0400) Subject: adding excerpt function to get city page content without cutting off words X-Git-Tag: v1.0.0^2~29 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=fc633efbc01122b1a39f395d3ac00e24831f281f;p=WP-Themes%2Fcoldwater.git adding excerpt function to get city page content without cutting off words using custom function to get page content excerpt without cutting off content mid word. --- diff --git a/functions.php b/functions.php index 5f40e04..7845699 100644 --- a/functions.php +++ b/functions.php @@ -144,78 +144,21 @@ function is_post_type($type){ add_action('thematic_searchloop', 'mytheme_search_loop'); // End of the Contextual/Highlight Search functions -// This series of functions is used for forcing a template if one or any is picked. -// START FORCE TEMPLATE FUNCTIONS -add_filter( 'template_include', 'glm_force_template', 99 ); -function glm_force_template( $template ) { - global $post; - /* - * Uncomment this IF statement surrounding the locate_template line - * if you want to use the given template (index.php) EVERY time any page - * template is selected & included. - */ - //if (get_page_template_slug( $post->ID ) == "test-template") { - $template = locate_template( "index.php" ); - //} - return $template; +function get_excerpt_by_id($post_id, $length = 35){ + $the_post = get_post($post_id); //Gets post ID + $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt + $excerpt_length = $length; //Sets excerpt length by word count + $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images + $words = explode(' ', $the_excerpt, $excerpt_length + 1); + + if(count($words) > $excerpt_length) : + array_pop($words); + array_push($words, '…'); + $the_excerpt = implode(' ', $words); + endif; + +// $the_excerpt = '

' . $the_excerpt . '

'; + + return $the_excerpt; } -function get_custom_page_templates() { - $templates = array(); - // maybe by options? --> $templates = get_option( 'custom_page_templates' ); - // maybe by conf file? --> $templates = include 'custom_page_templates.php'; - return apply_filters( 'custom_page_templates', $templates ); -} -add_action( 'edit_form_after_editor', 'custom_page_templates_init' ); -add_action( 'load-post.php', 'custom_page_templates_init_post' ); -add_action( 'load-post-new.php', 'custom_page_templates_init_post' ); - -function custom_page_templates_init() { - remove_action( current_filter(), __FUNCTION__ ); - if ( is_admin() && get_current_screen()->post_type === 'page' ) { - $templates = get_custom_page_templates(); // the function above - if ( ! empty( $templates ) ) { - set_custom_page_templates( $templates ); - } - } -} -function custom_page_templates_init_post() { - remove_action( current_filter(), __FUNCTION__ ); - $method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING ); - if ( empty( $method ) || strtoupper( $method ) !== 'POST' ) return; - if ( get_current_screen()->post_type === 'page' ) { - custom_page_templates_init(); - } -} -function set_custom_page_templates( $templates = array() ) { - if ( ! is_array( $templates ) || empty( $templates ) ) return; - $core = array_flip( (array) get_page_templates() ); // templates defined by file - $data = array_filter( array_merge( $core, $templates ) ); - ksort( $data ); - $stylesheet = get_stylesheet(); - $hash = md5( get_theme_root( $stylesheet ) . '/' . $stylesheet ); - $persistently = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' ); - $exp = is_int( $persistently ) ? $persistently : 1800; - wp_cache_set( 'page_templates-' . $hash, $data, 'themes', $exp ); -} -add_filter( 'custom_page_templates', function( $now_templates ) { - // Any slug->name pairs down here will be selectable in the admin side of - // a page. You do not need to create a new template file. You can refer to - // this template by its slug in our index.php using get_page_template_slug - // Below are some commented examples of custom quasi-templates. - $templates = array( - // 'some-custom-page-template' => 'Some Custom Page Template', - // 'any-template-slug' => 'Sample Template' , - ); - - return array_merge( $now_templates, $templates ); -} ); -// END FORCE TEMPLATE FUNCTIONS - - -// THIS IS HOW IT CAN BE USED IN INDEX.PHP -// if (get_page_template_slug( $post->ID ) == "some-custom-page-template") { -// echo "
[I am totally not a block ._.]
"; -// } else { -// echo "

The current template used is:".get_page_template_slug( $post->ID )."

"; -// } ?> diff --git a/parts/map-section.php b/parts/map-section.php index 6c95cfa..344c043 100644 --- a/parts/map-section.php +++ b/parts/map-section.php @@ -10,8 +10,10 @@ ); $city_page = get_posts($args); - $content = substr(strip_tags($city_page[0]->post_content),0, 300); - $content = strip_shortcodes($content); +// $content = substr(strip_tags($city_page[0]->post_content),0, 300); +// $content = strip_shortcodes($content); + $content = get_excerpt_by_id($city_page[0]->ID, 50); + echo $content; $city_image_id = get_post_thumbnail_id($city_page[0]->ID); $city_image = wp_get_attachment_image_src( $city_image_id, 250,250, true); $city_url = get_permalink($city_page[0]->ID);