Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Created November 17, 2011 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlynorama/1374521 to your computer and use it in GitHub Desktop.
Save carlynorama/1374521 to your computer and use it in GitHub Desktop.
Create a more tag when it doesn't exist in a Wordpress Twenty Eleven Theme
<?PHP
//functions that need to be in the functions.php
//for this to work
//more tag detection
function has_more()
{
global $post;
if ( empty( $post ) ) return;
if ($pos=strpos($post->post_content, '<!--more-->')) {
return true;
} else {
return false;
}
}
function more_posistion()
{
if ($pos=strpos($post->post_content, '<!--more-->')) {
return $pos;
}
}
///limit content length
//http://www.fusedthought.com/archives/wordpress-custom-content-length-code-snippet
function limit_content($content_length = 150, $allowtags = true, $allowedtags = '') {
global $post;
$content = $post->post_content;
$content = apply_filters('the_content', $content);
if (!$allowtags){
$allowedtags .= '<style>';
$content = strip_tags($content, $allowedtags);
}
$content = str_replace(']]>', ']]&gt;', $content);
$wordarray = explode(' ', $content, $content_length + 1);
if(count($wordarray) > $content_length) :
array_pop($wordarray);
array_push($wordarray, '...');
$content = implode(' ', $wordarray);
$content .= "</p>";
$content .= twentyeleven_continue_reading_link();
endif;
echo $content;
}
?>
//content index is a copy of content-images.php from the twenty-eleven theme
//I changed the section referencing the content
<div class="entry-content">
<?php if (has_more()) {
the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) );
} else {
$my_content = limit_content();
echo $my_content;
}?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?PHP
//what is now
global $more;
$more = 0;
$format = get_post_format( $post_id );
if ( ! $format ) {
get_template_part( 'content', 'index' );
} else {
get_template_part( 'content', get_post_format() );
}
?>
<?PHP
//what it used to be
global $more;
$more = 0;
get_template_part( 'content', get_post_format() );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment