How to show a page excerpt as a widget with wordpress?

I’m making a website and using wordpress as a CMS.
I have a frontpage which has some widget ready regions.
I also have several other static pages (such as ‘about me’ for example)
These pages have an excerpt (above the ‘more’ line) and a body text (under the more line).

I’ve been trying for hours but I can’t find a good plugin or some usefull code that let’s me show the excerpt of a certain page in the widget area (sidebar).

Read More

If anyone could help me, that would be awesome…

Related posts

Leave a Reply

3 comments

  1. You can do this with a plugin but it might be more beneficial to implement it yourself. It’s not very complex at all. You’ll have to do two things to get a post excerpt inside of a widget. By default, WordPress doesn’t let you run php inside of a widget. To get around this, go into your functions.php file and add the following code at the bottom of the file:

    add_filter('widget_text', 'execute_php', 100);
    
    function execute_php($html) {
        if (strpos($html,"<"."?php")!==false) {
            ob_start();
            eval("?".">".$html);
            $html=ob_get_contents();
            ob_end_clean();
        }
        return $html;
    }
    

    This allows you to run php code inside widgets. With that in place, go to your widgets menu and drag a new text box to the sidebar and place the following code into the text box:

    <?php
        global $post;
        $tmp_post = $post;      
        $args = array( 'numberposts' => 5, 'category__in' => array(11));
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) : setup_postdata($post);
            the_excerpt();
        endforeach;
        $post = $tmp_post;
    ?>
    

    This code will go through and output the except for the first five posts in category 11. You can obviously modify the code to get whatever particular result you are looking for. Let me know if you need the code tailored more directly to your specific case and I’ll help you out.

  2. Try this code for excerpt..

    <?php query_posts('cat=ID'.'&showposts=NO. OF POST') ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_post_thumbnail(); ?>
    <p><?php echo substr(get_the_excerpt(), 0,65).' [...]'; ?></p>
    <a href="<?php the_permalink(); ?>">Read More...</a>
    
    
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    <?php endif;?>
    

    Change the category ID and limit of post’s…

  3. First you have to install PHP Code Widget plugin.

    Drag this plug widget on the sidebar and place this code there

    <?php
    
    // The Query
    $the_query = new WP_Query( 'pagename=your page slug' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_content('Read more...');
    echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
    

    Hope this will help