I am trying to simply display the three most recent posts. The original method only lets me display a list of linked titles and I don't want to install a heavy plugin to just get something so simple to work. I know that the following code is not working, it is more a visualization of what I want to achieve. If I remove Line 7
, 9
, 10
and 12
, the code works, just that it doesn't display enough of what I want to have displayed.
Thank you for your assistance!
(a search on Stackoverflow didn't bring up any solutions...)
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="col-lg-4 col-md-6">
<h3>'. apply_filters('the_title', get_post_field('post_title', $recent["ID"])) .'</h3>
<p class="timestamp">'. get_the_time("j. F Y", $recent["ID"]) . '</p>
<p class="category">'. foreach((get_the_category()) as $category) { echo $category->cat_name; } .'</p>'.
get_the_content(" ...", $recent["ID")
.'<p><a href="' . get_permalink($recent["ID"]) . '" class="btn" role="button">Weiterlesen</a></p></div>';
}
wp_reset_query();
?>
EDIT: I have updated the code for the titles. Now links and titles work, time stamps, categories and excerpts don't work.
Working code. Important is to open and reset WP_Query at the beginning and the end, the rest inside is standard procedure.
<div class="row">
<?php $the_query = new WP_Query( 'posts_per_page=3' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div <?php post_class('col-lg-4 col-md-6'); ?> id="post-<?php the_ID(); ?>">
<h3><?php the_title(); ?></h3>
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name; } ?></p>
<p class="timestamp"><?php the_time('j. F Y') ?></p>
<?php the_content(' ...'); ?>
<p><a href="<?php the_permalink() ?>" class="btn" role="button">Weiterlesen</a></p>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>