Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been trying to utilize the PHP excerpt function to call from her recent blog post to post on a main landing page without avail. Any thoughts?

<div id="home_news" class="prefix_9 grid_3">
    <div id="newsbox" style="display: block;">
        <div id="news">
            <h2>Welcome</h2>
            <div id="news_content">Welcome to the... .check back shortly for a new selection of oil paintings, latest prints and greeting cards</a>. 

<?php 
$my_query = new WP_Query('category_name=blog&posts_per_page=1'); 

while ($my_query->have_posts()) : $my_query->the_post(); 

    $do_not_duplicate = $post->505;
    the_excerpt();

endwhile; ?> 

            </div>
        </div>
    </div>
</div>
share|improve this question

2 Answers 2

up vote 0 down vote accepted

While this was far from clear in the OP at first, I think this might be a good solution. Just make a shortcode to place the excerpt in the body. (This is a bad idea if you want this on every page. This is a good idea if you want it once in a while on some pages in the body.)

Here's code to put in your functions.php:

function the_excerpt_shortcode() {
    return get_the_excerpt();
}
add_shortcode( 'the_excerpt', 'the_excerpt_shortcode' );

Once you have the, just put this in your page body:

[the_excerpt]

And you're good to go.

If you're using Twenty Ten, Twenty Eleven, or some other theme that hooks to the excerpt_more filter, you may have to modify that short code to strip an auto-generated "Continue Reading..." link, but for many themes that's unnecessary. Consider this a starting point.

share|improve this answer

the_excerpt() won't work on a landing page. It must be used inside the post loop in order to function.

Inside the loop, WordPress populates a global $post variable with information about the current post. Then, various template tags reference that global variable and output content.


Based on your edits above ...

You almost have your loop structured correctly. But instead, you'll need to:

  1. Create your own page query to grab the latest post
  2. Display the post
  3. Restore the original query

The code below should do what you need:

<div id="home_news" class="prefix_9 grid_3">
    <div id="newsbox" style="display: block;">
        <div id="news">
            <h2>Welcome</h2>
            <div id="news_content">Welcome to the... .check back shortly for a new selection of oil paintings, latest prints and greeting cards</a>. 

<?php 
// Create a new query and grab the latest post from the "blog" category
$my_query = new WP_Query( 'category_name=blog&posts_per_page=1' ); 

// If we have a post, populate the necessary global variables and run the loop
if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); 

    // Echo the post excerpt on the page
    the_excerpt();

endwhile; endif;

// Reset the main loop data so global variables have the right values
wp_reset_postdata();
?> 

            </div>
        </div>
    </div>
</div>
share|improve this answer
    
Thank you for your response. Is it possible to add the loop within a page? I've been playing around with it… No luck yet. –  Charles May 15 '12 at 13:02
    
Yes. Take a look at the multiple loops examples in the Codex. –  EAMann May 15 '12 at 13:41
    
I edited my question because I couldn't get the markdown format to appear in my comment. does the post->ID need to be specified? Is the ID a global named variable? –  Charles May 16 '12 at 23:26
    
After you call the_post() you populate a global $post variable that has an ID member. –  EAMann May 17 '12 at 3:46
    
okay–so I replaced the ID with a number of the post? –  Charles May 17 '12 at 12:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.