WordPress.org

Ready to get started?Download WordPress

Forums

Php The_Excerpt Not Working (6 posts)

  1. ianbee
    Member
    Posted 1 year ago #

    I'm trying to get excerpts for post to show up with the plugin "Category Grid View Gallery". Right now it displays the title of the post by default. Here is the code for displaying the title:

    $cgtitle='<div class="cgback cgnojs '.$this->params['showtitle'].'"></div><div class="cgtitle cgnojs '.$this->params['showtitle'].'"><p style="font-size:'.$cgfontsize.'px;line-height:'.(1.2*$cgfontsize).'px;"><a href='.$returnlink.'>'.$title.'</a></p></div>';
    		return $cgtitle;

    So yeah, this is the php code that spits out the title links to each post. I tried to add the excerpt by adding in the following code right after the closing "</p>" tag...
    <div id="lol"><?php the_excerpt(); ?> </div>

    I set the div "lol" to 200px and with a grey background.. the div shows up where it should, but there is no excerpt. And yes, I have went into the screen options ina wordpress post and made an excerpt, still nothing! Other than that, here are the folowing thing I have done to try and get this working:

    1) Change "<?php the_excerpt(); ?>" to "php the_excerpt();".
    2) Change "the_excerpt" to "the_content" to see if it would show.
    3) Move the div "lol" to a different spot.

    And none of the above showed the excerpt for each post. I know I'm doing something wrong, I just don't know what it is. Right now, I'm off to play video games with my brother for the next 15 minutes, if anyone could help me out and suprise me with a solution for this problem it would mean sooooooo(oooo) much to me! :)

  2. Marventus
    Member
    Posted 1 year ago #

    Hi. Are you calling the_excrpt() from inside the Loop as isntructed by the corresponding Codex article (the_excerpt)?

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    the_excerpt() directly prints/echos the excerpt;
    http://codex.wordpress.org/Function_Reference/the_excerpt

    in your code, you need a function which would return the excerpt - try to use get_the_excerpt()
    http://codex.wordpress.org/Function_Reference/get_the_excerpt

    or the combination apply_filters('the_excerpt',get_the_excerpt())

    the also check string concatenation; http://php.net/manual/en/language.operators.string.php

    example:

    $cgtitle='<div class="cgback cgnojs '.$this->params['showtitle'].'"></div><div class="cgtitle cgnojs '.$this->params['showtitle'].'"><p style="font-size:'.$cgfontsize.'px;line-height:'.(1.2*$cgfontsize).'px;"><a href='.$returnlink.'>'.$title.'</a></p><div id="lol">'.apply_filters('the_excerpt',get_the_excerpt()).'</div>
    </div>';
  4. ianbee
    Member
    Posted 1 year ago #

    @alchymyth

    Thanks so much for the reply! I took your advice and tried the following:

    1.
    <div id="lol"><?php get_the_excerpt(); ?></div>

    2.
    <div id="lol">get_the_excerpt();</div>

    3.
    <div id="lol">'.apply_filters('the_excerpt',get_the_excerpt()).'</div>

    Number one didn't do anything but when I inspected the element "lol" in google chrome, this was in it: <!--?php get_the_excerpt(); ?-->

    Number two spit out the words get_the_excerpt(); in my lol div

    Number three did nothing unfortunately. :( There is nothing instead the lol div.

    Here is a link to my site I'm testing on, I hope this can help further, I'd really like to get this working... http://www.enberwebsite.com/testtest/test-page/

    When you hover over the thumbnail it shows the div "lol" (grey box). Inside that should be an excerpt. Also this code is inside a .php file inside a plugin, it is not inside the loop. I don't know if that matters or not.

    Again, thanks so much for the help and if you, or anyone could actually get this working it would mean soo much! :D

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    can you post the full code of the template?
    please use the http://pastebin.com/ as described in http://codex.wordpress.org/Forum_Welcome#Posting_Code

    did you read and check the earlier reply by @Marventus ?

  6. ianbee
    Member
    Posted 1 year ago #

    can you post the full code of the template?
    please use the http://pastebin.com/ as described in http://codex.wordpress.org/Forum_Welcome#Posting_Code
    
    did you read and check the earlier reply by @Marventus ?

    Hi! I found out the problem is that I'm trying to post an excerpt outside of the loop. However, I did read an article on how to get excerpts outside of the loop! Here is the article:

    http://www.sean-barton.co.uk/2011/11/getting-the-wordpress-excerpt-outside-of-the-loop/

    Here is the coding:

    <?php
            function get_the_excerpt($id=false) {
                global $post;
    
                $old_post = $post;
                if ($id != $post->ID) {
                    $post = get_page($id);
                }
    
                if (!$excerpt = trim($post->post_excerpt)) {
                    $excerpt = $post->post_content;
                    $excerpt = strip_shortcodes( $excerpt );
                    $excerpt = apply_filters('the_content', $excerpt);
                    $excerpt = str_replace(']]>', ']]>', $excerpt);
                    $excerpt = strip_tags($excerpt);
                    $excerpt_length = apply_filters('excerpt_length', 55);
                    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    
                    $words = preg_split("/[\n\r\t ]+/", $excerpt, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
                    if ( count($words) > $excerpt_length ) {
                        array_pop($words);
                        $excerpt = implode(' ', $words);
                        $excerpt = $excerpt . $excerpt_more;
                    } else {
                        $excerpt = implode(' ', $words);
                    }
                }
    
                $post = $old_post;
    
                return $excerpt;
            }
    ?>

    I tried inserting the code in my theme functions, with and without the php tags, and it broke my website. I also tried inserting it in my plugins php file that I'm trying to show the excerpt in, again, didn't work. Where would I insert the code? Here is the code for my plugin that displays the title.

    http://pastebin.com/BwH5Rrmf

    Thanx for all the help so far! :D

    and just search "lol" in the pastebin to find where the title actually get posted. :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.