ThemeShaper Forums » Thematic

[closed]

Is this right way of adding page template to display posts from a category?

(11 posts)
  • Started 4 years ago by vijayrajesh
  • Latest reply from gregriley
  • This topic is not resolved
  1. vijayrajesh
    Member

    Hi all,

    I am doing one template where the homepage may not display a particular category (for example let us say cat id 3) and in a particular page (called with page template blog.php) i want to display posts only from cat id 3.

    I could manage to do the things right. And it works too, But I want to get guidance whether I am doing right on the page template.

    Here I am attaching the code, please tell me if this is the right way of doing it.

    http://pastebin.com/6A9cmnAp

    You can see the line number 36 to 43, the additional code i have added to display posts.

    Thank you so much for the reply,

    Posted 4 years ago #
  2. vijayrajesh
    Member

    I am sorry to reply my own post. Can any one guide me please?

    Posted 4 years ago #
  3. vijayrajesh
    Member

    Somebody please reply :)

    Posted 4 years ago #
  4. In the interests of discussion, I have included lines 36-43 below:

    // action hook creating the index loop
    function display_posts_on_blog_page()
    {
    query_posts("cat=3");
    thematic_index_loop();
    }
    add_action('thematic_below_indexloop', display_posts_on_blog_page' );

    In my newbieness, I would personally have removed the original indexloop action and created a custom one for your custom-page.php.

    However, I think you mentioned this works for you, so perhaps this is an easier approach?

    Posted 4 years ago #
  5. vijayrajesh
    Member

    Thanks to Flick.

    Yes, the approach. But i am afraid, i seem to be reinventing the wheel. Should i use all filters, hooks in page templates too?

    Posted 4 years ago #
  6. Hi,

    here's the shortest way :)

    Your blog.php:

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    /*
    Template Name: Blog
    */
    ?>
    <?php
    
        include TEMPLATEPATH . "/index.php";
    
    ?>

    And the code for your functions.php:

    function add_my_query()
    {
    	if (is_page_template('blog.php'))
    	{
    		query_posts("cat=275");
    	}
    }
    add_action('thematic_above_indexloop', 'add_my_query');

    Chris

    Posted 4 years ago #
  7. @Chris: Now that is some really short way!

    Posted 4 years ago #
  8. kbkisan
    Member

    Hi Chris,

    I was wondering if there is a way of making this function access more then one category and show different numbers of posts from each category

    function add_my_query()
    {
    	if (is_page_template('blog.php'))
    	{
    		query_posts("cat=275");
    	}
    }
    add_action('thematic_above_indexloop', 'add_my_query');

    I would like to show 1 post from category3 and 4 posts from category1 on the same page. Thanks in advance.

    Posted 4 years ago #
  9. jahzara
    Member

    Am I missing something, where exactly does the code go? I just tried it and it's not working. Thanks

    Posted 4 years ago #
  10. 1 Create file(template) in your child theme named blog.php
    - You'll want to assign a page to this template in the WP admin>Page>attributes"
    2 Then paste Chris's blog.php code in to the new file and save it.
    3 Next add the second block of code From Chris's example above into your child theme's functions.php
    - you'll need to adjust the code in this step to reflect your custom query

    If you cant figure it out Let us know what you are trying to accomplish with this query.
    -Gene

    Posted 4 years ago #
  11. Thanks for the great thread. I have followed the advice and got it working, I don't really understand it but it works which is great. Except on my new page that shows posts from only one category the posts only show the first 4 or 5 lines and I have to click on the post title to see the rest. My main posts page that shows all the other categories shows the full posts.

    Any ideas gratefully accepted.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.