0

After much troubleshooting and searching, I think I finally understand how to get The Loop going. Except I can't seem to get it to actually loop!

I have created a Child theme, and added a functions.php file in there with this code:

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'grow-thinkup-style-minimal'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}

function show_posts(){
  if (is_page("test-1")){
  if (have_posts()){
      echo "You have posts!";
      while  (have_posts()){
          the_post();
          echo("This is " . the_title());
      }
  }
}
}

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
add_action('wp', 'show_posts');

?>

The idea is that I have a page, titled test-1 (www.example.com/test-1). If that URL is visited, I would like to list all the posts I have in my Wordpress.

All the current code does is show the_title() for test-1. Not my post pages. It also doesn't seem to be pulling the information from the_post(), i.e. the_title().

Here's what I see when I visit www.example.com/test-1: enter image description here

Is the reason it's not showing other information is that I have this in functions.php? I'm very new to WordPress so I'm still learning how their file system works, so that may be the issue?

Again, end of the day I'd like a page to simply have all posts listed using The Loop.

FYI here's how my files are located:

  • .../wp-content/themes/grow-minimal-child/ [this has style.css, index.php, and functions.php with the code shown above]
  • I notice there are no else statements in your code, making it difficult to debug, but I would also say the way you're implementing this by using the wp hook is highly unusual. I would strongly advise against it, and instead make use of a page template and the built in blogs page setting – Tom J Nowell Sep 26 '18 at 21:32
  • The wp comes from another tip I gave in another context, but I see it's not very helpful in this situation. A good resource to start with the loop and templates is the Codex: codex.wordpress.org/The_Loop and maybe developer.wordpress.org/themes/basics/template-files. – Michael Sep 26 '18 at 23:56
  • @Michael - Thanks for your continued help. I used that The_Loop page to get the code I show above. I've now added a page-allposts.php to my child theme folder, and put the above code in (a few tweaks)...nothing happens though I don't get the loop at all. I added an "else" statement too, and nothing shows. – BruceWayne Sep 27 '18 at 15:20
1

I'm following your initial question. In your backend, create a page with the title Test 1, which should result in the page slug test-1.

In your child theme folder, create a page template, using the filename page-test-1.php. Wordpress will be loading this template, following this pattern: page-$slug.php. For more details, see here: https://developer.wordpress.org/files/2014/10/wp-hierarchy.png.

In your page template, load the header and footer of your theme and query your posts. By default, Wordpress would display the content of that page, but since you want to display all of your posts, you need a custom query:

<?php
get_header();

$args = [
 'post_type' => 'post',
 'post_status' => 'publish',
 'posts_per_page' => -1
];

$posts = new WP_Query( $args );

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

    the_title( '<h1>', '</h1>' );
    the_excerpt();

    ?>
 </article>
<?php
endwhile;

get_footer();

Another way would be to go to your settings->reading section and select the page Test 1 as default template for your posts page. However in this case, Wordpress will load the index.php file to show a list of your posts. You could then display all posts with a default loop:

while( have_posts() ) : the_post();
   the_title();
endwhile;
  • Holy cow, that worked! :D Thanks a gadrillion for following the breadcrumbs of my WordPress issues. This makes it super clear, after reading a few articles on templates etc. I think I get it now! However, to be clear, this is not a template that I can choose from "Templates" when doing a New Page. This is [semantic word]..."a specific page template"? When I use your code in a "template" (where I can choose as such from the list of templates), the page returns a HTTP 500 error. I'm just trying to get this all straight and super appreciate everything you've helped with so far! :D – BruceWayne Sep 27 '18 at 17:06
  • 1
    Refer to this page, ie "Creating Custom Page Templates for Global Use": developer.wordpress.org/themes/template-files-section/… – Michael Sep 27 '18 at 17:13
  • Thanks, I read that page and that's where I tried the "global" loop, but it never looped. Just kept returning a single post's information. But, I'll read more carefully, thanks! – BruceWayne Sep 27 '18 at 17:17
  • 1
    A default loop simply returns the information that Wordpress has queried by default. This depends on the Url you are visiting, ie. localhost/mypage will query the post object of mypage. Hence if you do a the_title() in a default loop on that page, you will just see the title of that page. – Michael Sep 27 '18 at 17:20
  • 1
    That's Correct. – Michael Sep 27 '18 at 18:26
1

The Loop doesn't work inside functions.php. That's because functions.php controls theme-wide actions. In most cases, you need more than just one Loop for a site. So for example, you'll have one Loop on single Posts - that one shows the full contents of the post. There'll be a different Loop on Categories - that one typically shows post excerpts of a limited number of Posts, with pagination below. Yet other loops are quite common and just depend on what you want to achieve with what post types and content types.

So, create yourself an index.php to start with. This is the fallback. Most often this file doesn't even get used because again, you want to customize each content type, but this is your starting point where your most basic Loop should live. Grab header, loop, grab footer.

After that, decide what other types of loops you want in your theme. Another common file to start in is single.php to control individual Posts. You may have more custom markup here.

If you really want to consolidate and have a Loop with the same output on multiple templates, you can look into Template Parts. You call them from inside another file, like single.php, and they handle just one portion - such as a generic Loop - of that file's output. So you could have a full-content.php Full Content template part that you call inside single.php like so:

get_template_part('full-content');

and you can then call it from inside other templates, like page.php which controls Pages. This way you're not repeating yourself from file to file.

  • Thanks for your explanations. I've created an index.php inside my child theme folder, with the code above...doesn't seem to work either. Note I added get_header() after the <?php line, and get_footer() before the final ?>. – BruceWayne Sep 27 '18 at 15:21
  • 1
    Do you have any content on the site? Normally as I mentioned you won't see the effects of index.php because there is usually a more specific template. If there aren't any other templates, make sure you're viewing a Post or a Page. I would recommend looking at a basic, early theme, such as perhaps Kubrick, to see some of the WP theme basics. That one is outdated but much simpler than say the Twenty (X) themes to start learning your way around theming. – WebElaine Sep 27 '18 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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