WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Can WP ignore <!--more--> tag on most recent post? (12 posts)

  1. York
    Member
    Posted 6 years ago #

    I use the <!--more--> tag on most posts on my website. I would like WordPress to ignore the tag on my most recent post so that the entire post is displayed. When that post is no longer the most recent, it would then be truncated at the <!--more--> tag.

    Is there a solution for this, perhaps a line of PHP code to insert in a template file?

    Thanks in advance for your help!

  2. BPartch
    Member
    Posted 6 years ago #

    Hello

    There may be a way to do exactly what you want using the more tag and stuff though I am unsure, but this might also accomplish generally what you want.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $postcount++; ?>
    
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <p class="date"><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p>
    
    <?php if ($postcount == 1) { ?>
    <?php the_content("Continue reading: " . the_title('', ' &raquo;', false)); ?>
    <?php } else { ?>
    <?php the_excerpt() ?>
    <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read more of <?php the_title(); ?></a></p>
    <? } ?>
    
    <p class="postmetadata">Posted in: <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit',' | ',' '); ?></p>
    
    <!--
    <?php trackback_rdf(); ?>
    -->
    
    <?php endwhile; ?>

    This will display the most recent post in it's entirety, than the older posts will just have the excerpt.

    The excerpt by default will use the first 175 letters of the post (or something like that) or if you enter text into the optional excerpt field of a post (in the dashboard) that will be used instead.

    So, it is not exactly what you we're asking for but does almost the same thing.

    This would go in index or home.php depending on which file you are using.

    Hope it is helpful. :)

  3. York
    Member
    Posted 6 years ago #

    Thank you for your help, BPartch. Unfortunately, the visual design of my website is based around the specific placement of the <!--more--> tag in most posts, so transitioning to excerpts may cause more issues than it resolves for me.

    If anyone knows how to write PHP to ignore the <!--more--> tag on my most recent post only, I look forward to learning it.

  4. Kafkaesqui

    Posted 6 years ago #

    York, change

    <?php the_content(); ?>

    in your template(s) to:

    <?php
    if( ($post == $posts[0]) && !is_paged() ) :
    	echo apply_filters('the_content', $post->post_content);
    else :
    	the_content();
    endif;
    ?>

    Description: If we're on the first post (0 in the $posts object) but also not on any but the first page of the current query type, the statement prints out the post content, though it applies any formatting or plugin filters first. This will bypass the 'more' functionality in the_content().

  5. York
    Member
    Posted 6 years ago #

    Thank you for the detailed response, Kafkaesqui. In my template the code is slightly different:

    <?php the_content(__('... (read more)'));?>

    Should I replace that entire line of code with what you've provided?

  6. moshu
    Member
    Posted 6 years ago #

    Yes... and you can always add to the_content part your code:
    (__('... (read more)'))

  7. York
    Member
    Posted 6 years ago #

    Thanks, moshu. That seems to have accomplished it. (On a side note, would you recommend any resources for learning PHP to improve my understanding of WordPress' source code?)

    For others who may be interested, I replaced

    <?php the_content(__('... (read more)'));?>

    in my index.php and home.php template files with

    <?php
    if( ($post == $posts[0]) && !is_paged() ) :
    	echo apply_filters('the_content', $post->post_content);
    else :
    	the_content();
    endif;
    ?>

    Now the most recent post on my main page displays in its entirety. The <more> tag I included when I wrote the post is ignored until it is no longer the most recent post, at which point the <more> tag is recognized.

    Thanks to the moderators for their excellent support.

  8. ivosilvestro
    Member
    Posted 6 years ago #

    apply_filters('the_content', $post->post_content) work in wordpress 2.5 too?

    Thank You

  9. York
    Member
    Posted 6 years ago #

    This code no longer works with WordPress 2.5.

    Does anyone know how to ignore the <!--more--> tag on the most recent post in 2.5?

    Thanks for your help.

  10. Ivovic
    Member
    Posted 6 years ago #

    I'm not using 2.5, but try:

    <?php
    global $more;
    if($post == $posts[0]) $more = 1;
    else $more = 0;
    the_content();
    ?>

    if that doesn't work try switching the 1 and the 0 for the $more variable, I keep forgetting which way around it should be.

  11. ivosilvestro
    Member
    Posted 6 years ago #

    @Ivovic: Your code works! Thanks!

  12. designkai
    Member
    Posted 6 years ago #

    @ Ivovic;
    Just tried your code to no avail, so I had a closer look and changed it to:

    <?php
    global $more;
    if($post == $posts[0]) $more = 0;
    else $more = 1;
    the_content();
    ?>
    This appears to work for me.

Topic Closed

This topic has been closed to new replies.

About this Topic