WordPress.org

Ready to get started?Download WordPress

Forums

code for including "excerpts" in headlines (11 posts)

  1. Graphics
    Member
    Posted 8 years ago #

    hey-
    i'm using this code for "mini-loops":

    http://codex.wordpress.org/Creating_a_Static_Front_Page#Adding_a_Mini-Loop

    but it doesn't include "excerpts" of the posts, just the headlines.
    anyone know how i can add in the excerpts?

    Please!- dont refer me to the codex pages- I've spent hrs in there with no results

  2. Yngwin
    Member
    Posted 8 years ago #

    <?php the_excerpt(); ?>
    is what is normally used for excerpts, but that may not be applicable to the mini-loop solution there. You may also want to see what I've done in my latest MilkXT theme.

  3. tomhanna
    Member
    Posted 8 years ago #

    You should be able to do that by calling "post_excerpt" in addition to "post_title". The exact code for doing that isn't beyond my ken, but I'd suggest repeating that whole mySQL function immediately following the first one and just replacing post_title with post_excerpt.

  4. Graphics
    Member
    Posted 8 years ago #

    i hate to sound stupid...
    but to php i am.
    where would i place
    either of these tags to make them work, in accordance with the code i am using?

  5. tomhanna
    Member
    Posted 8 years ago #

    Put the code in the Sidebar template file if you want it in your sidebar. Here you go:

    <h2 class="coltitle">Latest News</h2>
    <?php
    $today = current_time('mysql', 1);

    if ( $recentposts = $wpdb->get_results("SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 5")):
    ?>
    <ul>
    <?php
    foreach ($recentposts as $post) {
    if ($post->post_title == '')
    $post->post_title = sprintf(__('Post #%s'), $post->ID);
    echo "<li><a href='?p=$post->ID'>";
    the_title();
    echo "</a>";
    the_excerpt();
    echo "</li>";
    }
    ?>
    </ul>
    <?php endif; ?>

  6. Graphics
    Member
    Posted 8 years ago #

    it didnt work, gave me MySql errors.

    heres the code i am using: ( link at bottom probaby wont be written out due to the way this forum handle code)

    '<td>
    <?php define('WP_USE_THEMES', false);
    require('./wordpress/wp-blog-header.php');
    ?>
    <div id="content" class="narrowcolumn">

    <h2>Lastest Posts:</h2>

    <?php
    # Here starts Mini Loop trick: You can use this code, but _at your own risk_
    # If you want to improve this code, you're welcome ;)
    $how_many=7; //How many posts do you want to show ?>

    <ul align='left'>
    <?
    $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
    WHERE post_status = \"publish\" ORDER BY ID DESC LIMIT ".$how_many);
    foreach($news as $np){
    printf ("<li align='left'>%s", $np->ID,$np->post_title,$np->post_title);
    }?>

    </div>
    </td>'

  7. Kafkaesqui

    Posted 8 years ago #

    This should do what you're after:

    <?php
    $how_many=5;
    require_once("wp-config.php");
    ?>

    <ol id="whats-new">
    <?
    $news=$wpdb->get_results("SELECT ID, post_title, post_excerpt FROM $wpdb->posts
    WHERE post_status='publish' ORDER BY ID DESC LIMIT $how_many");
    foreach($news as $np){
    printf ("<li><a href="index.php?p=%s">%s</a>%s
    </li>", $np->ID,$np->post_title,$np->post_excerpt);
    }?>
    </ol>

    Note it displays an excerpt if a post actually has an excerpt, as in text entered in the excerpt field. It does not extract and excerpt from the post's content, like the_excerpt() would. Here's a version based more on WordPress' own functionality:

    <?php
    $how_many=5;
    require_once("wp-config.php");
    ?>

    <ol id="whats-new">
    <?php $news = new WP_Query("showposts=$how_many");
    while($news->have_posts()) : $news->the_post();
    ?>
    <li><a href="index.php?p=<?php the_ID(); ?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    </li>
    <?php endwhile; ?>
    </ol>

  8. Graphics
    Member
    Posted 8 years ago #

    both gave this error

    Parse error: parse error, unexpected T_STRING in /usr/local/apache2/vhosts/osga/feedtest.php on line 44

  9. Kafkaesqui

    Posted 8 years ago #

    And line 44 in feedtest.php is?

    I've just run the code bits on my test site, and both worked without error except I noticed <a href="index.php?p=%s"> was missing the slashes before each " in the first one I posted (it's not needed in the second). Still, that wouldn't cause a T_STRING error.

  10. Graphics
    Member
    Posted 8 years ago #

    BIG THANKS!
    apparently the first time i tried it, it was "user error"
    one or two more fixes and i'll be stylin!

  11. tomhanna
    Member
    Posted 8 years ago #

    Hmm...no idea why that would have given you mySQL errors. I tested it here - http://www.tom-hanna.net/testblog/ - before I posted it and all I did was copy and paste.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.