WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Read More Conditional? (5 posts)

  1. badrad
    Member
    Posted 8 years ago #

    First, a quick thanks to everyone working on WordPress. It is my first time using it, and it is hands down the most well written, powerful piece of web software I have ever used. Its amazing how it balances being easy to use and completely extensible.

    Anyway, I am using <!--more--> to show only the first part of a post on the index and in the archives. I use strip_teaser to strip the teaser off and show my own click here for more link, as it appears in the postmetadata section.

    What I want to do is be able to post short entries that do not use the <!--more--> functionality. But for those shorter entries, I don't want the text to be "Read More", I want to be able to say something like "End of Post, click for permalink" or "permalink" or whatever I want.

    SO, what I want is a conditional for if the <!--more--> functionality is being used or not. So I could say

    if (use_read_more == true) {
    echo 'Fancy Read More link';
    } else {
    echo 'End of Article, Permalink';
    }

    or something along those lines.

    From what I can tell, there is no built in way to do this. So, please correct me if I am wrong.

    I did come up with my own way. It increases the page load time on the index page by like .05 seconds, so tell me if you can think of an easier way to do it.


    <?php
    // lets see if there is a moretag or not

    // start buffer
    ob_start();

    // output post
    the_content('uniquestringoftext234hhk32lh432hlk3', true);

    // empty buffer and capture contents
    $moretest = ob_get_contents();
    ob_end_clean();

    // lets see if our unique string was present
    $moreresult = strstr($moretest, 'uniquestringoftext234hhk32lh432hlk3');

    if (empty($moreresult)) {
    /* do whataver I want for permalinks */
    } else {
    /* do wahtever I want for read more links */
    }
    ?>

    So, my question is if there is a built in way to do this, and if not, can anyone think of a more efficient way then what I am doing?

    Thanks in advance! Feel free to email me at badrad@gmail.com

  2. Kafkaesqui

    Posted 8 years ago #

    First I'd suggest dropping the use of an output buffer; there's ways around that in WordPress when dealing with the $post object. In any case, here's a suggestion for a more efficient method (and with far less code):

    <?php
    the_content('Fancy Read More link');
    if(!strpos($post->post_content, '<!--more-->')) :
    ?>
    End of Article, <a href="<?php the_permalink(); ?>">Permalink</a>
    <?php endif; ?>

    This runs the_content() as normal, then checks to see if the <!--more--> quicktag exists in the post's content; if not it adds the permalink (as it assumes a "shorter" post has just been displayed). That's really all you need.

  3. badrad
    Member
    Posted 8 years ago #

    Very cool. I knew there had to be a way to access the post contents without using the template tag that was requiring me to use output buffering, but I am still getting the hang of wordpress :)

    Now I know I can directly access stuff in the $post array.

    Using the new method, there is almost no increase in processing time, versus a big one otherwise. Thanks a lot!

  4. joshuaone
    Member
    Posted 8 years ago #

    All I want to do is add a permalink button at the end of each post (rather than being satisfied with the title as the permalink). Kafkaesqui, it looks to me like the code you wrote above may be close to what I need. Would you please have pity on my non-geek brain on tell me what code I need to do that -- and where to put it. I would be grateful.

  5. Kafkaesqui

    Posted 8 years ago #

    joshuaone, the code above is far more than what you need. To provide a permalink at the end of each post, just do something like:

    <?php the_content(); ?>
    <div class="permalink"><a href="<?php the_permalink(); ?>">Permalink</a></div>

    You can use the .permalink class I gave to the div to style the link and it's placement in your style.css. To use an image (i.e. button) instead of a text link, change Permalink to <img src="permalink.gif" alt="Permalink" /> -- making sure your src attribute points to the image and it's correct location on your site, naturally.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags