• Resolved HelgaTheViking

    (@helgatheviking)


    i’m trying to display archive loops that display the full content of the post up until the more tag (if it exists) and if a more tag does not exist, it displays the excerpt.

    i found this conditional :
    if ($pos=strpos($post->post_content, ‘<!–more–>’)):

    at :

    http://wordpress.org/support/topic/excerpt-if-there-is-no-more-tag?replies=5

    but when i do a print_r to dump the values of the $post object in the loop i can see that “<!–more–>” is never in the post_content even though i’ve added the more tag in the backend. in fact, i don’t see anything in the post_content pertaining to the more tag that i could test for. hence, the above conditional always comes back false.

    is the more tag behaving differently in 3.1? at what point and where can i test for its presence?

Viewing 5 replies - 1 through 5 (of 5 total)
  • you won’t see the <!--more--> on the screen as it is a html comment.

    do the var_dump or echo $post->post_content and look into the html in the browser – and it should be there.

    (it is at least in my local setup)

    simple test line for index.php within the post div:
    <?php if( strpos($post->post_content, '<!--more-->') >= 1 ) echo 'we have a more tag!'; ?>

    Thread Starter HelgaTheViking

    (@helgatheviking)

    awesome… thank you!

    i found this works as well:

    if ( preg_match('/<!--more(.*?)?-->/', $post->post_content) ) {	echo 'we have a more tag!'; }

    Just because this took me hours to sort out – I just posted my solution to this to another thread about custom excerpt lengths because I was having trouble getting those to show (resolved, http://wordpress.org/support/topic/custom-excerpt-length-doesnt-work-anymore ).

    My fix in the end was Instead of using the_excerpt if there isn’t a more tag, I’m doing a custom function to get a content at an arbitrary length.

    I’m using a child theme of twenty eleven, hence the $content .= twentyeleven_continue_reading_link(); at the end.

    in my showcase.php

    // Set $more to 0 in order to only get the first part of the post.
    						global $more;
    						$more = 0;
    
    						if ( has_post_format()) {
                                get_template_part( 'content', get_post_format() );
                            } else {
                                get_template_part( 'content', 'index' );
    						}

    In my content-index.php

    <?php if (has_more()) {
    		        the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) );
    		    } else {
                    $my_content = limit_content();
                    //$my_content = apply_filters('the_content', $my_content);
                    //$my_content = str_replace(']]>', ']]>', $my_content);
                    echo $my_content;
                }?>

    in the functions.php

    //more tag detection
    //http://wordpress.org/support/topic/check-if-post-has-more
    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(']]>', ']]>', $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;
    }

    other relevant pages:
    http://codex.wordpress.org/Customizing_the_Read_More (“Displaying a “more…” link without a <–more–> tag”)

    Oh, a note – once I moved the code from the test server to live server it broke until I changed

    if ( has_post_format()) {

    to

    if ( has_post_format('')) {

    I have no idea why it worked on my local dev and not on my live site. They are supposed to be identical.

    Actually… even better…

    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() );
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Excerpt if there is no more tag? REVISTED’ is closed to new replies.