• Resolved dnk3232

    (@dnk3232)


    Hello,

    I am new to PHP and am new to the loop. I have created a static homepage, and wish to display my blog feed on my homepage (website can be found here

    For my homepage, I created a home-page.php template to use. In the template I added the following code:

    	<?php
    	  if (have_posts()) :
    	   
    	  while (have_posts()) : the_post();
    
    	  endwhile;
    	  
    	  else :
    	  echo '<p>No posts to display</p>';
    
    	  endif;
    
    	  ?>

    Which to my understanding is the start of a very simple loop. When I view my homepage, nothing is showing. I have test posts so I am unsure why nothing is being pulled.

    I’d appreciate any help as to why this it’s not showing and if I have done something wrong, as I cannot see anything wrong with my code so far.

    Thank you.

Viewing 10 replies - 1 through 10 (of 10 total)
  • First, since you are using a static page to display your posts. Did you add this new file as a Page Template and assign the Page you are creating this on to this page template?

    Add the following to the top of this file.

    <?php /* Template Name: Home Template */ ?>

    Re-upload this file. Head over to that page and assign this page template as the template to use for that page. (Template box is on the right).

    To learn about page templates, please visit https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

    To display your latest posts on this page template, use WP_Query

    An example

    <?php
    $args = array('post_type'=>'post');
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            // post info here
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Page Data */
    wp_reset_postdata();
    ?>
    • This reply was modified 7 years, 8 months ago by Patrick.
    Thread Starter dnk3232

    (@dnk3232)

    Thank you Patrick, I have got the loop working now.

    The last piece is adding pagination to my loop – is that something that can be done easily or should I open a new thread in regards to that?

    From what I’m reading, I will need to use JQuery or AJAX and I haven’t gotten into either languages yet so I’d like to find an easier way (preferably with PHP).

    If anything, even just a “load more posts” link would work.

    Hello,

    There are several ways to create pagination.

    1. By using posts_nav_link()
    https://codex.wordpress.org/Function_Reference/posts_nav_link

    2. For a more customized output, use next_posts_link() and previous_posts_link()
    https://codex.wordpress.org/Pagination

    3. For numeric pagination or page numbers as pagination use the following https://codex.wordpress.org/Function_Reference/paginate_links

    4. If you don’t want to do any of the above, just use this plugin https://wordpress.org/plugins/wp-pagenavi/ 🙂

    Make sure you add the “paged” parameter in all the above.

    I will need to use JQuery or AJAX

    You have to use this only if you want an Ajax pagination where the next set of posts is loaded without “reloading” the page.

    Thread Starter dnk3232

    (@dnk3232)

    Thanks again Patrick!

    I’m not getting this to work for me though..this is creating pagination on the actual blog (i.e. if you click on the blog, once you get to the end there is a next/previous page link).

    What I’m looking for is to add page numbers to the bottom of my home page like this: http://imgur.com/xvESdix

    Currently mine looks like this: http://imgur.com/bjuDWQo aka nothing is there.

    I can figure out how to style everything later, but even a simple “More Posts” link would be beneficial, I just can’t seem to figure it out!

    Let me know if you know of a solution, greatly appreciate it.

    Thread Starter dnk3232

    (@dnk3232)

    Also this is the website I am working on if you’d like to take a closer look.

    Can you post the code you are using to hastebin.com

    Thread Starter dnk3232

    (@dnk3232)

    Try this one.

    https://hastebin.com/yegikoxugu.xml

    Look at the ‘paged’ part. 'paged' => $paged,

    Thread Starter dnk3232

    (@dnk3232)

    Thank you, that isn’t working either. I’m not quite sure why nothing is working for me…

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Having issues displaying loop on homepage’ is closed to new replies.