• Resolved svedish

    (@svedish)


    Hi guys,

    I’m struggling to get some basic principles here. I have been reading he Codex here and there and in the Pages section I see an example that shows how to display posts inside a page. I have managed to tweak it a little bit, still way to go, but I’m afraid I don’t really know what I am doing. I’d prefer to understand properly. Therefore my question:

    I am studying WP a little bit and I’m trying to create a Staff page that display a title, some content and a list of employees with a portrait and a biography for each of them.

    In order to make them “dynamic”, I have used posts to create employees (all under the category “Staff”), then I have created my page and my page template (staff_page.php) and all is fine until I get to the Loop. I initially pasted the index.php Loop hoping to then tweak it (to only list the -Staff- category posts), but the Loop was not even showing up. Then I’ve found a Loop on the Page Codex section which makes use of the wp_query class, which is not very simple to grasp.

    To cut the long story short, can’t I just use the index.php Loop on pages to display posts? Do I need to use different specific tags and functions? Also, the page content itself seems to be generated by a Loop which “talks” about “posts”, has posts etc. What is the difference between pages and posts in this regard?

    Many thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter svedish

    (@svedish)

    Sorry if I add here. What I’m trying to achieve here is understanding why I’m using the Loop that way there, as opposed to how I use it on the posts home page.

    I could copy and paste that code, label it with “use this and tweak it if you want to display posts on a page” and forget the whole thing, but I’ll never understand how this works

    Hope it makes sense. ??

    I have used posts to create employees (all under the category “Staff”), then I have created my page and my page template (staff_page.php) and all is fine until I get to the Loop.

    You don’t need to create Pages to display posts in the category “Staff” as WordPress will do that as part of the natural archive thing.

    Don’t be confused by what an archive is–it is just a generated display of your posts at that moment. You don’t do anything to create the archive, that’s an automatic thing WordPress does for you. Usually, archives are date, category, tag, or author, based.

    Access to archives is typically presented via links in a sidebar under an Archive (date based), Category, or Tag Cloud, headings. Widgets, or Template Tags such as wp_get_archives(), wp_list_categories(), wp_tag_cloud(), and wp_list_authors(), are the constructs used to present links to users to visit your various archives. The process of placing code in your Theme’s Templates is explained in Stepping Into Templates and Stepping Into Template Tags.

    Once a user clicks on a Category link in the sidebar, the display of those posts can be controlled by a Category Template. Other Templates, such as Author Templates, and Tag Templates, are available if you set them up. These Templates can be coded via Template Tags such as the_title(), the_content(), or the_excerpt(), to display just a post title, the full content of the post, or just an excerpt of the post.

    Also, it is important to understand the Template Hierarchy, as that is how WordPress determines what Template to use to render the posts for reading by your readers.

    If a user visits a Category archive, then clicks on a given post title in that Category archive, the display of that single post is again presented by another Template, and again, the Template Hierarchy determines what Template displays that single post. Finally, that single post Template can be coded to display just the title, the full post content, or an excerpt.

    Thread Starter svedish

    (@svedish)

    hi Michael,

    thanks a lot for your answer and I think I fairly understand the things you told me about what WP does out of the box. In fact I get into trouble when I try to do something that possibly requires some extra “touches” ??

    In my scenario I’m studying how build a brochure website using WP, by building a website for a fictional company X. Therefore I will have, I don’t know, say 6 links to 6 pages and inside 1 of these pages (Staff) some content + a list of images / biogs for each employee of this fictional company. I thought it could be a good idea to use posts (with custom fields ofc) to create the employees’ records.

    Another consequence is that I’ll not have any blog-like menus showing up (Unless I decide to implement a news section in the website). That’s why I need to create a page showing posts from a category. Of course I could use children pages for the employees. I just happened to use posts for them.

    That’s why I would like to know a bit more about querying posts from any page. When to use the normal index.php Loop, why in the Page Codex section (A Page of Posts sample in https://codex.www.ads-software.com/Pages) wp_query is instead used etc. I have already taken that piece of code and tweaked it. My employees are showing up nicely. They have a portrait and the titles are not links anymore. Also, these info are coming both from 2 custom fields. Pretty advanced huh?? ??
    In short, I get things to work and all is fine. I just don’t understand why I’m using that code as opposed to, I dunno, say the regular “while-if-has-posts” standard Loop? I just found the example and bent it to my needs. Not very insightful…

    Hope what i say makes sense. Please let me know if it does not! ??

    Cheers

    Oh you’re going to love this thread ;}

    https://www.ads-software.com/support/topic/354905?replies=19

    Thread Starter svedish

    (@svedish)

    Hi Michael,

    thanks for your help. Exactly the kind of stuff I’m looking for. Some more in depth conversation about how things really work under the bonnet. And at first sight, I agree with him, the Codex is rather confusing in that regard. I’ll have a proper read and may possibly participate to the discussion. ??

    Cheers.

    Thread Starter svedish

    (@svedish)

    Also, and sorry for being a complete newbie…

    Before I possibly feel comfortable with the other conversation, which is just one step ahead of my knowledge, can you please help me to clarify the basic principle here?

    If I go on the Loop Codex page I read that the loop starts with <?php if ( have_posts() etc. and ends with <?php endif; ?>.
    I can also read that “The Loop should be placed in index.php and in any other Templates used to display post information, but looks slightly different depending on your version of WordPress.” And I can see this in action if I open the index page.

    So far so good…So I thought: “Good, I just grab the Loop from the index page and throw it in my Staff page template, then I tweak it to display on posts in XX category and voila’! I get all the Staff posts”. Then I realized it didn’t work. The Loop was displaying nothing. I needed to use WP_query – or maybe query_post – and this is what you guys discuss in the other thread. Which one is best…

    But I still miss the previous bit. ?? Which, in short, is the following:

    1) Why does the Loop as it is (without any WP_query or query_post or get_posts), the one that starts with <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> and ends with <?php endif; ?>, work in the index.php page out of the box?
    2) therefore…why do I instead need to use those other functions (WP_query / query_post / get_posts) to query my posts from other places?
    3) Where do I need to use the other functions (WP_query / query_post / get_posts)? Just all pages that are not index.php?
    4) Finally, how many ways of querying posts are there? Just 2? The “regular one” and the “wp-query-posts-get-posts” one?

    As you can see, since the documentation is not extremely clear, I just need 2-3 basic principles clarified to then dive into if wp_query is preferable to get_posts.

    Hope it makes sense. ??

    Thanks again in advance.

    This is my view of it:
    1. WordPress automatically fills the query before accessing the template (index.php, category.php, archive.php)
    2. I like the new WP_query structure
    3. Use new WP_query when doing your own loops that are not part of the standard query/template hierarchy
    4. query_posts, new WP_query, get_posts with setup_postdata, and even wpdb with setup_postdata

    Thread Starter svedish

    (@svedish)

    Short, concise, to the point. Thanks a lot. Great and helpful reply. ??

    One comment only about your answer number 2:

    ? I understand you like WP-query (and I am also catching up about why WP_Query is better than other functions – reading around and starting to grasp the point). However when I said “why do I instead need to use those other functions” I meant, why do I “have” to. In other words, why doesn’t the index.php query output anything if I paste it into one of my page templates? Because of what you said under point 1. perhaps? Or for some other reason? Or perhaps it works and i did a mistake when I copied the code? ??

    Thanks.

    Not sure–but if you look at the WordPress Default theme you will see that the loop in page.php is very similar to the loop in index.php.

    Thread Starter svedish

    (@svedish)

    Exactly!

    That’s why I was wondering why I can’t use the same loop in the page template to query posts? Maybe if you use it in a page template it queries the page contents (title, copy, custom fields etc.). If you use it in a “posts page-template” it automatically queries posts? Maybe WP has got automatic ways of knowing the scope of pages? I have no clue. It would be nice if anyone could enlighten me.

    Anyone out there that can help us with the answer to this question please?? ??

    Thanks a lot for all your help so far Michael!

    Thread Starter svedish

    (@svedish)

    I think I’ll open a new thread with this last question, should I feed the urgent need to know why this works this way. ??
    I think we are quite off topic by now. As for “displaying posts on a page template”, which was the original title, I’d say the thread is Resolved. Thanks Michael! ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Display posts on a page template’ is closed to new replies.