• Hello, i have made two custom page templates.The first is an Archive page that displays the posts and the categories of the posts.Here is the code:

    <?php
    /**
    * Template Name: Archive
    */
    /*Use Bootstramp*/
    get_header(); ?>
    
    <div class="CategoryMenu">
    <?php
    $args = array(
      'orderby' => 'name',
      'parent' => 0
      );
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
    	echo '<a class="CategoryArchive"  href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
    }
    ?>
    </div>
    
    <div class="container">
    <div class="row">
    
    <div id="PostData">
    <?php
    
    $theposts = get_posts(array('posts_per_page' => -1));
    
    foreach ( $theposts as $post ) :
      setup_postdata( $post );
    echo '<div class="ArchiveThumb">';
      echo get_the_post_thumbnail($post_id,'medium');
    echo '</div>';
      ?>
    
    	<p class="PostName"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    
    <?php endforeach;
    ?>
    
    </div>
    </div>
    </div>
    
    <?php
    
    get_footer();
    ?>

    And a second page template that i want to display a certain category of post for example awards.Here is the code for the second page:

    <?php
        /**
        * Template Name: Awards
        */
        get_header(); ?>
    
        <div class=InfoMenu>
            <?php if(!$post->post_parent){
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
        }else{
        if($post->ancestors)
        {
        $ancestors = end($post->ancestors);
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
        }
        }
        if ($children) {
        ?>
        <?php echo '<div class="InfoPage">' .$children. '</div>'; ?>
        <?php } ?>
        </div>
    
        <?php global $post; // required
        $args = array('category' => 1); // include category 9
        $custom_posts = get_posts($args);
        foreach($custom_posts as $post) : setup_postdata($post);
    
        // put here what you want to appear for each post like:
        //the title:
        the_title();
    
        // an excerpt:
        the_excerpt();
    
        //and so on...
    
        endforeach;
        ?>
    
        <?php
    
        get_footer();
        ?>

    The problem is that i want to exclude the category from the archive page, and display the posts from the category in the second page.

Viewing 15 replies - 1 through 15 (of 25 total)
  • Shariq Khan

    (@shariqkhan2012)

    1. Why are you creating a page template for this? Why not modify the archive.php template for the archives.
    Problem with your approach is that you are not making use of the provided loop here. Instead you hare calling wp_query again with your code and then going in the loop. The original loop is wasted here.

    2. Same holds for the ‘awards’ archive. Create archive-awards.php and put your code in there.

    Thread Starter phaidonas

    (@phaidonas)

    I have multiple pages,if I use the loop in the template file will be the same?

    Shariq Khan

    (@shariqkhan2012)

    In WordPress, posts are different than pages.

    To display the archives, you don’t need a separate page template. Instead, you should make use of archive.php.

    Whatever code you are writing, it should be in archive.php.
    And it will be applicable for ALL categories. And if you want the ‘awards’ category archive to be treated differently, then the corresponding code should be in archive-awards.php

    So, lets say, in your website you have these pages:
    1. Home
    2. About
    3. Contact

    And you have these posts:
    1. Sample Post 1
    Categories: Music

    2. Sample Post 2
    Categories: Technology

    3. Sample Post
    Categories: Music, Art, Movies

    4. Sample Post 4
    Categories: Awards

    5. Sample Post 5
    Categories: Art, Awards

    If you view any of these posts, it will use the single.php
    Now, if you want to view archive for any category (music, movies, etc), WordPress will use archive.php to display them

    However for archives of category awards, WordPress will use archive-awards.php

    Have a look at WordPress Template Heirarchy for more details

    Moderator bcworkz

    (@bcworkz)

    What shariqkhan2012 said ^^

    Additionally, if you want to exclude a particular category from the default archive list, that is possible by using the “pre_get_posts” action. After confirming the query object passed to your callback is the main query, and is an archive query for the ‘post’ post_type, set the “category__not_in” query var to an array of category IDs to be excluded from the posts returned. Use an array even if there is only one ID.

    You can alter any posts query this way. The key is determining when it is appropriate to alter the query and when to let it pass unmodified. This can be done by checking the query vars and the various is_*() methods. And/or selectively adding the callback just before it is needed, then having the callback remove itself from the action hook when it’s done.

    Thread Starter phaidonas

    (@phaidonas)

    I will try them, how i assign an archive-page.php to a page?

    Thread Starter phaidonas

    (@phaidonas)

    hi, i updated the code like this:

    <?php
    /**
    * Template Name: Archive
    */
    /*Use Bootstramp*/
    get_header(); ?>
    
    <div class="CategoryMenu">
    <?php
    $Awards_id= get_cat_ID('awards');
    $Press_id= get_cat_ID('press');
    $News_id= get_cat_ID('news');
    
    $args = array(
      'orderby' => 'name',
      'parent' => 0,
      'exclude' => array($Awards_id,$Press_id,$News_id )
      );
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
    	echo '<a class="CategoryArchive"  href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
    }
    ?>
    </div>
    
    <div class="container">
    <div class="row">
    
    <div id="PostData">
    
      <?php $args = array(
              'posts_per_page' => -1
          );
    ?>
    
      <?php $query = new WP_Query($args); ?>
      <?php if ( $query->have_posts()  ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
      <div class="post">
    
      <!-- Display the Title as a link to the Post's permalink. -->
      <p class="PostName"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    
      <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
    
        <?php if ( has_post_thumbnail() ) : ?>
          <div class="ArchiveThumb">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail('large'); ?>
            </a>
            </div>
        <?php endif; ?>
    
      <?php endwhile;
      wp_reset_postdata();
      else : ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
      <?php endif; ?>
    
    </div>
    </div>
    </div>
    
    <?php
    
    get_footer();
    ?>

    And it works, i think i will do swomething similar also to the other pages, the only problem is that the profile page of the wp bar moved to the other side.

    Moderator bcworkz

    (@bcworkz)

    Umm, OK, if that works for you. You should recognize this is sub-optimal. As shariqkhan2012 said,

    Problem with your approach is that you are not making use of the provided loop here. Instead you hare calling wp_query again with your code and then going in the loop. The original loop is wasted here.

    You are free to do what you want with your own site, even when contrary to well informed advice. Reluctance to rework your templates is understandable.

    I don’t understand what you mean about the profile page of the WP bar moving. If you want help with that you’ll need to explain further. If not, then no further explanation is needed.

    Thread Starter phaidonas

    (@phaidonas)

    i have arrange all the site like this, yes i understand that is sub-optimal, the bar avatar in the bar is going to the other side because a css interruption.Except the archive-{mypage}.php could i also use the page-{mypage}.php.And what is the point of the page templates?

    Thank you.

    • This reply was modified 9 years, 1 month ago by phaidonas.
    Moderator bcworkz

    (@bcworkz)

    I understand in general the problem with the avatar line feeding because the content is too wide for its container, but I cannot offer much of a solution without knowing how the bar content is generated or what the output looks like. All I can say is the bar needs to be narrower and/or the container wider. I imagine you already knew that much. A live link to a page demonstrating the problem would be ideal, but I understand that is not always possible.

    Whether you use archive-* or page-* depends on what situation you want the template to be used. Archive templates are used for requests involving multiple posts, usually custom post types. The scheme will work for any default post type as well: posts, pages, attachments. In your example archive-{mypage}.php, mypage would need to be a post type. If it is not, the template would be ignored.

    page-* templates are for single pages only. In your example page-{mypage}.php, the template would only be used for the page whose slug is “mypage”. To have a page template apply to multiple pages, do like you have done with the Template Name: header and specify that template when you publish the page.

    The point of page templates is the same as any other template. To structure output in a particular way. Only that page templates would only apply to pages. Page templates can be useful for receiving form submits that need extensive processing. All the code can reside right on the template. In fact, the form could be part of the template. What is output depends on whether the request is GET or POST.

    Those kind of templates may not even make use of page titles and content. The only purpose of the title is to establish a permalink to use to execute the template’s code. Page templates could be used for virtually anything. Something rather traditional, like a particular query as you have done, to a very complex, fully interactive app could be accomplished with a page template.

    Shariq Khan

    (@shariqkhan2012)

    You are grossly misunderstanding the template hierarchy.

    Page templates are different than post templates.
    Only point of convergence are index.php, home.php and front-page.php.

    But lets not get into that for now, else you will get even more confused

    If you have been using WordPress for a while, that posts are meant to be added every now and then, while the pages are meant to be more or less fixed.
    So ask yourself, if you have a website about yourself (or about your company), where would you put the information about yourself (or the company) i.e the ‘About Us’ information? In a post or a Page?
    Similarly where would you put the contact information i.e the address, contact form and possibly a google map? On a post or a page?
    If you understand WordPress correctly, you would put these in a Page, and not a post.

    Now again ask yourself, where would you put any updates/or thoughts oryour musing? on a post or a page?
    This time , if you understand WordPress correctly, you would put these in a Post, and not a page.

    If you agree to all that has been said this far, we are in a better position.
    Now you would also have noticed that posts often are grouped, while Pages are never.
    Your posts can be grouped by categories, or by month or by author.
    These groups are called archives.
    So by now we have established that archives are really a posts thing. They have got nothing to do with pages.
    This means there is no such thing as archive template for pages.
    The archive template is for posts only. Period.

    To sum up what he have gone through till now, posts can be displayed as a single post or can be displayed in groups called archives.
    These archives can be groups of categories or authors or publishing month or date etc.
    For each of these we can have a post template.
    This means we can have these templates:
    1. single.php
    2. category.php
    3. author.php
    4. date.php
    5. archive.php
    6. index.php

    These can further be made even more specific like:
    1. single.php
    2.a. category-news.php i.e. category-{category_slug}.php
    b. category-awards.php
    c. category.php
    3.a. author-admin.php i.e. author-{username}.php
    b. author-phaidonas.php
    c. author.php
    4. date.php
    5. archive.php
    6. index.php

    These are arranged in such a way that :
    A. for single posts, index.php is the fallback for single.php i.e if single.php does not exist then WordPress will use index.php (which is a mandatory file) to display the posts.

    B. For category archives, index.php is the fallback for archive.php, which is the fallback for category.php , which itself is the fallback for category-{category_slug}.php

    Lets say you are browsing the category archive for ‘news’ category,
    then WordPress will look for these files in the exact order:
    1. category-news.php.
    If it exists, WP will use the code in this file to display the posts.
    If it does not exist, WP will move to step 2.
    2. category.php
    If it exists, WP will use the code in this file to display the posts.
    If it does not exist, WP will move to step 3 .
    3. archive.php
    If it exists, WP will use the code in this file to display the posts.
    If it does not exist, WP will move to step 4.
    2. index.php
    Since this file is mandatory, WP will find this file, and use it to display the posts.

    C. For author archives, index.php is the fallback for archive.php, which is the fallback for category.php , which itself is the fallback for author-{username}.php

    Lets say you are browsing the archive for author ‘admin’,
    then WordPress will look for these files in the exact order:
    1. author-admin.php.
    If it exists, WP will use the code in this file to display the posts.
    If it does not exist, WP will move to step 2.
    2. author.php
    If it exists, WP will use the code in this file to display the posts.
    If it does not exist, WP will move to step 3 .
    3. archive.php
    If it exists, WP will use the code in this file to display the posts.
    If it does not exist, WP will move to step 4.
    2. index.php
    Since this file is mandatory, WP will find this file, and use it to display the posts.

    D. Similarly for date archives, the order of preference is :
    date.php > archive.php > index.php

    I hope this clears up what are post templates and what they are used for and what is their hierarchy

    Now, what are page templates for, you ask.
    These are to govern the layout or display of individual pages.
    For eg. you may want to have a left-sidebar on your ‘About Us’ page, and a right sidebar for you ‘Contact Us’ page, while at the same time a full-width (no-sidebar) layout for your home page.

    For this, you create 3 different custom page templates.
    Interestingly unlike post templates, you can rename these page templates to anything you want. Only condition is that they should contain this markup:

    <?php
    /**
     * Template Name: Full Width Content
     *
    ?>

    You can call this file as full-width-template.php or full-width.php or even rock-n-roll.php. As long as the mandatory markup is there, you are good.

    Now when you create the Home page you assign this template to this page, by selecting the correct template from the dropdown in the right side on the post edit screen.

    Similarly you create templates for left sidebar (eg. awesome.php), and right sidebar (eg. avangers.php or right-sidebar.php or anything you like), and assign the relevant template to the ‘About’ and ‘Contact pages.
    Unlike post templates, the page templates dont get picked automatically. You have to assign them to pages.

    Like post templates, there is a hierarchy for page templates to.

    For a page called ‘About’ , with the slug ‘/about-us’, this is going to be the order.

    1. custom template file – The page template assigned to the page. eg. awesome.php
    Note that just the existence of this file is not sufficient for it to be used by WP.
    You will have to explicitly assign it to the page, by selecting the correct template from the dropdown in the right side on the post edit screen.

    If the custom template has not been assigned to a page, WP will move to step 2

    2. page-{slug}.php – If the page slug is ‘about-us’, WordPress will look to use page-about-us.php.
    If it does not exist, WP will move to step 3.

    3. page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php
    If it does not exist, WP will move to step 4.

    4. page.php
    If it does not exist, WP will move to step 5.

    5. index.php
    Since this file is mandatory, WP will find this file, and use it to display the posts.

    Hope now you have answer to your questions, and you have a better understanding of template hierarchy now.

    Thread Starter phaidonas

    (@phaidonas)

    ok good,thank you very much very explain-full, now the final question, is any way to create a post-{mypost}.php or maybe post-{category slug of my post}.php, for custom display of inside the post? Because i have may categories and some of them have to be displayed differently.

    Thank you again.I think with practice it will be also more explainable.

    • This reply was modified 9 years, 1 month ago by phaidonas.
    Shariq Khan

    (@shariqkhan2012)

    Bi have may categories and some of them have to be displayed differently

    That is what the category archive is for.
    Lets see you want the ‘news’ category to be displayed differently than the other categories (awards, press).
    To accomplish this, you just need to create category-news.php and put your relevant code there.

    Thread Starter phaidonas

    (@phaidonas)

    i mean the display code for the post itself not the code that displays the posts of a category.The code that the content.php has of displaying the content of the post.

    Thread Starter phaidonas

    (@phaidonas)

    As i undestand it, and searched, i think i mean about post types,or (maybe) single-{posttype}.php.But i dont understand fully how to assign one or use it.
    Thanks and sorry for noob questions

    Shariq Khan

    (@shariqkhan2012)

    If you could provide acouple of screenshots or sketches of what you are after and what you have right now, that will help in answering the question better.
    Also please list all the files in your current theme directory.
    And if you are using a child theme, then list the files in the parent theme as well.

Viewing 15 replies - 1 through 15 (of 25 total)

The topic ‘Posts by catecory’ is closed to new replies.