Support » Developing with WordPress » Creating a post loop on homepage?

  • dnk3232

    (@dnk3232)


    Hi everyone,

    I am not new to WordPress, but a new to PHP so I apologize if this is an easy question that I can find online but I’ve searched online and am slightly confused with the different answers I am getting.

    I am wanting to have a homepage display all my blog posts (easy). However, if I do that, I am unable to edit the homepage itself.

    I figured I could create a static home page, and somehow display the posts with a post loop. That way I can then edit the homepage and add things such as a header image, image slider etc.

    Is this correct? Is there a better way to do this?

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • If you want to display the posts by adding short code use the below function to your functions.php

    Short code: [all_post]

    add_shortcode('all_post','HomeAllposts');
    function HomeAllposts(){
    	global $post;
    	$args = array('orderby'=>'post_date','order'=>  'DESC', 'posts_per_page' => -1,'post_type'=>'post' );
    	$myposts = get_posts( $args );
    	foreach( $myposts as $key => $post ){  setup_postdata($post);?>
    		<h1><?php the_title(); ?></h1>
    		<?php the_excerpt(); ?>
    		<a href="<?php the_permalink(); ?>">More</a>
    	<?php }wp_reset_postdata();
    }

    Otherwise want to use template, use the code at template where you want.

    	global $post;
    	$args = array('orderby'=>'post_date','order'=>  'DESC', 'posts_per_page' => -1,'post_type'=>'post' );
    	$myposts = get_posts( $args );
    	foreach( $myposts as $key => $post ){  setup_postdata($post);?>
    		<h1><?php the_title(); ?></h1>
    		<?php the_excerpt(); ?>
    		<a href="<?php the_permalink(); ?>">More</a>
    	<?php }wp_reset_postdata();
    Thread Starter dnk3232

    (@dnk3232)

    Thank you! This is exactly what I am looking for.

    Moderator bcworkz

    (@bcworkz)

    A great response by Anandaraj! Nicely done, except the shortcode handler has a common mistake. We must never directly output content from a shortcode handler. Such output often ends up in an undesirable location. Other times it works perfectly, which is why this error is insidious.

    All output should be collected into a variable that is returned by the handler. Other internal code then echoes out the content at the proper location. PHP’s output buffering is useful for converting direct output code into a functional shortcode handler.

    Thread Starter dnk3232

    (@dnk3232)

    Thank you. In order to echo the shortcode in the proper place, would you mind explaining where I would do that/how? I am very to to PHP so I apologize!

    As it stands, I included the shortcode on the actual page (not in any php files).

    As well, I was trying to figure out how to add pagination to the loop – everything I have tried is resulting in an error so I would really appreciate some direction with that.

    • This reply was modified 4 years, 12 months ago by dnk3232.
    • This reply was modified 4 years, 12 months ago by dnk3232.
    Moderator bcworkz

    (@bcworkz)

    I’ll skip over the output buffer solution since it complicates things in some ways even though it’s a good solution. The usual technique involves collecting all output into a single variable.

    function shortcode_handler( $atts ){
    	global $post;
    	$myposts = get_posts();
            $output = "";
    	foreach( $myposts as $key => $post ){
               setup_postdata($post);
    	   $output .= '<h1>' . get_the_title() . '</h1>';
    	   $output .= get_the_excerpt();
    	   $output .= '<a href="' . get_the_permalink() . '">More</a>';
    	}
            wp_reset_postdata();
            return $output;
    }

    This function will not do exactly what you want because I simplified some things for the sake of an example. Just add your specific details back in. Note that the post functions are slightly different, they are variants that return content instead of echoing.

    You actually need to add shortcodes to content to get them to work. PHP does not recognize shortcodes as themselves. If you must execute a shortcode within PHP, use the do_shortcode() function.

    Pagination is generally handled as part of the query, not within a WP Loop. The query limits any results to the posts_per_page amount. The Loop outputs everything returned. If a different page is requested, the offset into matching results is calculated. If you want page 3 and there are 10 posts per page, the offset will be 20. (page 1 is posts 0-9, page 2 is 10-19, etc.)

    While WP_Query has an offset argument, using it will break the default pagination, which in some cases is what you need in order to have pagination work. If the default pagination functions don’t work, that’s a sign that you need to take over the process and manage it yourself.

    See Making Custom Queries using Offset and Pagination.

    Thank you @bcworkz

    So i encountered Cannot modify header information – headers already sent by (output) error by using one variable output.

    Moderator bcworkz

    (@bcworkz)

    Where did you put the shortcode?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Creating a post loop on homepage?’ is closed to new replies.