admin管理员组

文章数量:1023273

I'm making my first Wordpress theme and I've faced an issue lately.

I have a template file page-event.php. I intend to use it to answer the /events path (I have also created an events page). This works at the moment, but I want to make the events template loop through all the posts in categories events.

For some reason, this doesn't work. When I use the loop in the page-event.php template, it brings up only one post named events. I don't know why this happens.

I think it's showing just the category holder for events.

My question is

  • Isn't it possible to use the Wordpress loop to loop posts in any template file?
  • Why does the code loop through categories instead of posts?

Here's a look through my code in my page-events.php

<?php get_header();?>

<?php 
    if(have_posts()){
        while(have_posts()){
            the_post();?>
            <p><?php the_title();?></p>
        <?php }
    } 

    wp_reset_query();
    ?>

<?php get_footer();?>

I expect this code to at least provide a simple list of all the posts on the blog, but it returns a single category with the name events.

Thanks in advance.

Update.

I recently echoed out the $query_string variable to see what Wordpress was doing, it returned pagename=events. So it turns out Wordpress is only searching for pages because the page-event.php was a post type of page.

Need more help on how to make the loop search for posts instead of pages.

I'm making my first Wordpress theme and I've faced an issue lately.

I have a template file page-event.php. I intend to use it to answer the /events path (I have also created an events page). This works at the moment, but I want to make the events template loop through all the posts in categories events.

For some reason, this doesn't work. When I use the loop in the page-event.php template, it brings up only one post named events. I don't know why this happens.

I think it's showing just the category holder for events.

My question is

  • Isn't it possible to use the Wordpress loop to loop posts in any template file?
  • Why does the code loop through categories instead of posts?

Here's a look through my code in my page-events.php

<?php get_header();?>

<?php 
    if(have_posts()){
        while(have_posts()){
            the_post();?>
            <p><?php the_title();?></p>
        <?php }
    } 

    wp_reset_query();
    ?>

<?php get_footer();?>

I expect this code to at least provide a simple list of all the posts on the blog, but it returns a single category with the name events.

Thanks in advance.

Update.

I recently echoed out the $query_string variable to see what Wordpress was doing, it returned pagename=events. So it turns out Wordpress is only searching for pages because the page-event.php was a post type of page.

Need more help on how to make the loop search for posts instead of pages.

本文标签: Can I use the wordpress loop in pages