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.

On my front-page i have a 3 col block section below the fold. The final and third column is an excerpt of the most recent post from my art category blog, hopefully this is clear up until here in terms of explanation of setting the stage.

This is how i'm approaching the solution:

I created a php file called "content-art-blog" which i pull into my front-page file using this php code

<?php get_template_part( 'content', 'art-blog' ); ?>

On the actual content-art-blog.php file however i'm formulating my solution so far like this:

<div class="col-block-last">

<h2><span class="bold">art</span> <span class="font-condensed">blog</span></h2>

<?php get_post( $id, $output, $filter ); ?>

<?php
  $linkdata = array(
  'link_name' => 'art',
  'link_url' => 'http://domain.com/categories/art/'
  );
?>

<a href="http://dcio:8888/category/art/">Read more</a>

NOTE: the actual A tag i just put in the local directory file path (since i'm developing on MAMP) but i realize this won't work once the site goes live.

So the question is two fold:

1) How do i pull in 1 post excerpt for a specific category into this block section

2) How do i link to the post (or will this be pulled in with the excerpt meaning i don't have to actually include this A tag to begin with?)

I went through the functions to get an idea of what actually is out there in terms of what i can actually do with these functions but i'm such a noob it will take time to digest what all these functions do and hence properly interpret how i can go about solving problems with them.

In my case i'm not aware of what i don't know yet.

Thanks hope that was understandable.

share|improve this question
add comment

1 Answer

I think I understand your questions, so here's how I would achieve what I think you are looking to do.

The function is set up so that you can send it any category slug or ID and how many posts you'd like to retrieve from that category.

EDIT: I removed the_permalink() because I actually meant get_permalink() but mistakenly referred to the_permalink() instead.

<?php function wpse_114835_cat_latest_post($cat = null, $posts = 1) {
    //check for valid number of posts
    if(!is_int($posts)) {
        $posts = 1;
    }

    //set number posts
    $args = array(
        'numberposts' => $posts,
        'nopaging' => 1
    );

    //check what kind of category info we were sent
    if(isset($cat)) {
        //category id
        if(is_int($cat)) {
            $args['cat'] = $cat;
        //category slug
        } elseif(is_string($cat)) {
            $args['category_name'] = $cat;
        }
    }

    //get posts specified
    $cat_posts = get_posts($args);

    //return posts object
    return $cat_posts;
} ?>

You can add that function to your theme's functions.php file and then you can just call it from your template file, like so: $art_posts = wpse_114835_cat_latest_post('art');.

Getting permalink: to get the permalink, send the post ID to the get_permalink() functions, like get_permalink($art_posts[0]->ID);

Getting excerpt: the excerpt is part of the post object echo $art_posts[0]->post_excerpt;

If you want to display the excerpt with the [...] at the end or run it through any filters your theme or plugins might have set up for excerpts, you'll want to either a) set this post as the new global $post object so that you can use methods like the_excerpt() or b) create your own excerpt modification method that will trim down and append whatever you'd like.

Using your HTML from above, your template would look something like this:

<div class="col-block-last">
    <h2>
        <span class="bold">art</span> <span class="font-condensed">blog</span>
    </h2>

    <?php $art_posts = wpse_114835_cat_latest_post('art');

    echo $art_posts[0]->post_excerpt; ?>

    <a href="<?php echo get_permalink($art_posts[0]->ID); ?>">Read more</a>
</div>
share|improve this answer
    
thanks for the detailed answer i will have to do a deep dive and learn how these functions work and what's going on here as it's a bit more complicated than i had thought. Even though i'm grateful of how you broke down the function with comments i am still trying to understand multiple "if" statements and whether it matters where you put your array. These kinds of structural questions will help me understand how to solve problems myself at some point. Thanks again. –  MARS Sep 20 '13 at 13:28
add comment

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.