WordPress.org

Support

Support » Themes and Templates » [Resolved] Page of posts loop not working

[Resolved] Page of posts loop not working

  • Hello,

    I would like to have posts from my ‘events’ category display on the events page. I have used the code from the ‘Pages‘ codex document but it does not seem to be working as you can see here. If you view my homepage you can also see how it is disrupting the ‘page’ div.

    Any help would be really appreciated. Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi physoc

    There are two ways you can get the events page to display posts from the events category only, and both require you to create a page template with a unique name, i.e. events-page.php. In the admin page are, on the right, you will see a dropdown with the template name. Select it and save.

    To create the template, at the very top of the page in php comments you need to write:

    /*
    Template Name: Events Page
    */

    After this you have two options to call events posts.
    Option 1) In the events page create a new WordPress query. i.e.

    $events = get_posts(array(
              'numberposts' => 10,
    	  'orderby' => 'post_date',
    	  'post_type' => 'post',
    	  'post_status' => 'publish',
    	  'category' => '3'
    	   )
          );
    foreach($writing as $post) : setup_postdata($post);

    Write the HTML markup and necessary WordPress functions here. Remember to close the loop when finished with. endforeach;

    Option 2) Create a category page template for events called category-events.php. Create a standard loop, and the posts will automatically be populated by event category posts. The back

    To get posts in the events category only, you need to create a template page specifically for them. The page should be called category-events.php, surprise! 🙂

    In the original events page template then use php includes to pull in the category template.

    include('category-events.php')

    This will then display the category posts inside the events page.

    Let me know if this helps or is not clear enough.

    Thank you very much – that works perfectly :). Very much appreciated.

    Hello,

    Sorry, actually it is not quite right. I would like only the ‘events’ category posts to be listed and each post to have an excerpt displayed and nested comments. I also don’t want the header, sidebar or footer. Thank you 🙂

    p.s. I used option 2

    Here is the code for ‘category-events.php’

    Let’s start with the easy stuff. To not have the header, footer and sidebar not display do not call their functions. On your paste bin source code on lines 9, 42, 43 simply delete the function with the corresponding name and they will not appear. Just know that any widget area or hooks located in there will not display either.

    To have an excerpt only and not the full contents you need to call the function the_excerpt() . This will prevent the entire post content from displaying.

    To display comments you need to call wp_list_comments() to display all the comments. The function can take an array as an argument to style and order it.
    https://codex.wordpress.org/Function_Reference/wp_list_comments

    On the included category-events.php page are you saying that all post categories are displaying?

    Many thanks. Yes, all post categories are displaying.

    First double check that the category template and name, events match. If they do and it is not working then you will have to use the first option I originally gave you, the get posts function.

    You will need to login to the admin area and click on the posts->categories page. On the categories page click the Events category. On the resulting page look at the query string and you should see an id=someNumber. The id number is the category id# which you need to write down.

    $events = get_posts(array(
              'numberposts' => 10,
    	  'orderby' => 'post_date',
    	  'post_type' => 'post',
    	  'post_status' => 'publish',
    	  'category' => 1// Write the category id # in here
    	   )
          );
    foreach($events as $post) : setup_postdata($post);

    The code inside the loop, that you posted in pastebin, will go immediately after this. Sorry the other option didn’t work as a php include.

    Many thanks for your help, I have now resolved the issue.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Page of posts loop not working’ is closed to new replies.