1

I'm trying to get posts with a specific category to show in a box on my home page. But ive only been able to get one post on my homepage. How can I make this work in the code below? With getting the title, excerpt (limit the characters - without read more) and post link (in the read more button below?)

echo '<section class="row"><div class="container-item">
            <div class="item">
            <div class="item-overlay">
            <div class="item-content">
            <div class="item-top-content">
            <div class="item-top-content-inner">
            <div class="item-product">
            <div class="item-top-title">
            <h2>';
            echo get_the_title();
            echo '</h2><p class="subdescription">';
            the_excerpt();
            echo '</p></div></div></div>
            <div class="item-add-content">
            <div class="item-add-content-inner">
            <div class="section">
            <a href="#" class="btn buy expand">Read More</a>
            </div>
            </div>
            </div>
            </div>
            </div>
        </div>
</section>';

EDIT:

Thanks Pieter Goosen. This is the code with your suggestions and some minor edits which I ended up using with some jquery and css to create a pretty sliding box:

function homepage_highlights() { ?>
<?php $my_query = new WP_Query('category_name=accommodation&posts_per_page=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<section class="row"><div class="container-item">

       <div class="item" <body style="background-image:url(<?php echo catch_that_image() ?>)">
        <div class="item-overlay">
        <div class="item-content">
        <div class="item-top-content">
        <div class="item-top-content-inner">
        <div class="item-product">
        <div class="item-top-title">
        <h3 class="title-front-page"><?php echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>'; ?>
        </h2></div>
    </div>
    </div>  
    </div>
    <div class="item-add-content">
    <div class="item-add-content-inner">
    <div class="section">
    <p class="subdescription">
        <?php the_excerpt(); ?>
    </p>

        </div>
   </div>
</div>
</div>
</div>
</div>
</section>

1
  • Glad you sorted it. If my answer was usefull, please mark as such. You can also add your solution as an answer and mark it as accepted Apr 18, 2014 at 15:22

1 Answer 1

0

Is it really necessary to echo so much. If you make proper use of opening and closing php tags you can eliminate using echo. And why are you echoing get_the_title() when there is a build in feature for that. the_title() does exactly the same.

You can use WP_Query to create a special loop to get posts from a specific category. Just remember to reset your loop as you are going to start a new loop. For multiple loops, also check The Loop. One word of advice, the author of this page used query_posts, which you must not use at all, use WP_Query

As for the excerpt, you can change the read more link as follow

function new_excerpt_more( $more ) {
    return ' <---the code of your button here--->';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

So in the end, your code should like like this

<?php $my_query = new WP_Query('category_name=special_cat&posts_per_page=10'); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<section class="row"><div class="container-item">
            <div class="item">
            <div class="item-overlay">
            <div class="item-content">
            <div class="item-top-content">
            <div class="item-top-content-inner">
            <div class="item-product">
            <div class="item-top-title">
            <h2>
            <?php the_title(); ?>
            </h2><p class="subdescription">
            <?php the_excerpt(); ?>
            </p></div></div></div>
            </div>
            </div>
            </div>
            </div>
        </div>
</section>

<?php endwhile; ?>
<?php rewind_posts(); ?>

<---Start your new loop here --->
1
  • Thanks Pieter, you set me on the right track. I didn't realize I could avoid using echo by using <?php ?> tags properly.
    – sander
    Apr 18, 2014 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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