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
:
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]
else
statements in your code, making it difficult to debug, but I would also say the way you're implementing this by using thewp
hook is highly unusual. I would strongly advise against it, and instead make use of a page template and the built in blogs page settingwp
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.The_Loop
page to get the code I show above. I've now added apage-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.