Support » Themes and Templates » The three most recent posts in different div's, how?

  • Resolved Marijn

    (@marijn-bent)


    Hello everybody,

    I work at the following project:
    http://cl.ly/image/1e1X271T0k3P

    I want the three most recent post all in different div’s. How can I do this? The first one is easy:

    <div id="firstfield">
    <ul>
    <?php
    global $post;
    $args = array( 'numberposts' => 1, 'offset'=> 1 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	<li><?php $excerpt = get_the_excerpt( $deprecated ) ?></li>
    <?php endforeach; ?>
    </ul>

    Right?

    Regards,
    Marijn

Viewing 15 replies - 1 through 15 (of 25 total)
  • Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    You need to be adding the <div> within the foreach loop.
    You can’t wrap a <div> outside an <li> element within a <ul> element.

    esmi

    (@esmi)

    Forum Moderator

    <?php $args = array( 'posts_per_page' => 3 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <div <?php post_class();?> id="post-<?php the_ID();?>">
    <ul>
    <?php
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <li><?php $excerpt = get_the_excerpt( $deprecated ) ?></li>
    </ul>
    </div>
    <?php endforeach; ?>
    Marijn

    (@marijn-bent)

    Thanks Esmi, although if I paste this piece of code in the <div id="firstfield">, will the other two post go to the <div id="secondfield"> and <div id="thirdfield">?

    esmi

    (@esmi)

    Forum Moderator

    Forget your previous divs. The code above will generate unique ids and classes for each of the posts.

    Marijn

    (@marijn-bent)

    Beautiful! Yet, it doesn’t work right now.

    I took the orginal page code (I use the _S theme):

    <?php
    /**
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages
     * and that other 'pages' on your WordPress site will use a
     * different template.
     *
     * @package Jong Gooiland
     * @since Jong Gooiland 1.0
     */
    
    get_header(); ?>
    
    		<div id="primary" class="content-area">
    			<div id="content" class="site-content" role="main">
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php get_template_part( 'content', 'page' ); ?>
    
    					<?php comments_template( '', true ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			</div><!-- #content .site-content -->
    		</div><!-- #primary .content-area -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    And made a page template based on the code above:

    <?php
    /*
    Template Name: Voorpagina
     */
    
    get_header(); ?>
    
    		<div id="primary" class="content-area">
    			<div id="content" class="site-content" role="main">
    
    <?php $args = array( 'posts_per_page' => 3 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <div <?php post_class();?> id="post-<?php the_ID();?>">
    <ul>
    <?php
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <li><?php $excerpt = get_the_excerpt( $deprecated ) ?></li>
    </ul>
    </div>
    <?php endforeach; ?>
    
    			</div><!-- #content .site-content -->
    		</div><!-- #primary .content-area -->
    
    <?php get_footer(); ?>

    The page where the template is activated won’t work. Do you see the problem?

    I appreciate your help very much!

    esmi

    (@esmi)

    Forum Moderator

    Ah – now as this is going to be the main query/loop in the template, you have to use a slightly different approach. get_posts() is reserved for secondary queries/loops – which is what I assumed this was. Try replacing:

    <?php $args = array( 'posts_per_page' => 3 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <div <?php post_class();?> id="post-<?php the_ID();?>">
    <ul>
    <?php
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <li><?php $excerpt = get_the_excerpt( $deprecated ) ?></li>
    </ul>
    </div>
    <?php endforeach; ?>

    with:

    <?php query_posts( $query_string . '&posts_per_page=3' );
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div <?php post_class();?> id="post-<?php the_ID();?>">
    <ul>
    <?php
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <li><?php $excerpt = get_the_excerpt( $deprecated ) ?></li>
    </ul>
    </div>
    <?php endwhile;?>
    <?php endif;?>
    Marijn

    (@marijn-bent)

    Sorry for the confusion and thanks for the quick fix!

    I did the things you sad, but it won’t work. I still get the error. I don’t know or the following image will help, although maybe it does.
    http://cl.ly/image/1u16131v0h0i

    esmi

    (@esmi)

    Forum Moderator

    Can you translate that for me? Do you have access to your site’s error logs for a more precise error report?

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    A slight error on line 1, Esmi

    <?php query_posts( $query_string . '&posts_per_page=3' );
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    Edit:
    Missing PHP closing tag

    esmi

    (@esmi)

    Forum Moderator

    Um – I’m not seeing it. (Ok – it’s late here).

    esmi

    (@esmi)

    Forum Moderator

    Ah! Got it! It should be:

    <?php query_posts( $query_string . '&posts_per_page=3' );
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    Marijn

    (@marijn-bent)

    I have implemented te fix, but get the same error over an over again.

    It says: “An unexpected condition was encountered while the server was attempting to fulfill the request”

    The header and footer work in the ‘normal’ template as expected, so there isn’t the error.

    Thanks!

    esmi

    (@esmi)

    Forum Moderator

    Do you have access to your site’s error logs for a more precise error report?

    Marijn

    (@marijn-bent)

    I will look for it!

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Could be an error here, Esmi;

    <ul>
    <?php
    <li>

    Excerpted from;

    <?php query_posts( $query_string . '&posts_per_page=3' );
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div <?php post_class();?> id="post-<?php the_ID();?>">
    <ul>
    <?php
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <li><?php $excerpt = get_the_excerpt( $deprecated ) ?></li>

    Unnecessary opening PHP element on line 5?

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘The three most recent posts in different div's, how?’ is closed to new replies.