WordPress.org

Support

Support » Themes and Templates » Customizr » [Resolved] [Theme: Customizr] Add blog or custom post list templates to Customizr [How to]

[Resolved] [Theme: Customizr] Add blog or custom post list templates to Customizr [How to]

  • <?php
    /*
     * Template Name: Blog Posts
     * @package Customizr
     * @since Customizr 3.0.12
     */
    ?>
    
    <?php do_action( '__before_main_wrapper' ); ##hook of the header with get_header ?>
    <?php tc__f('rec' , __FILE__ , __FUNCTION__ ); ?>
    <div id="main-wrapper" class="container">
    
        <?php do_action( '__before_main_container' ); ##hook of the featured page (priority 10) and breadcrumb (priority 20)...and whatever you need! ?>
    
        <div class="container" role="main">
            <div class="row">
    
                <?php do_action( '__before_article_container'); ##hook of left sidebar?>
    
                    <?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
    
                    <div class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">
    
                        <?php do_action ('__before_loop');##hooks the header of the list of post : archive, search... ?>
    
                            <?php if ( tc__f('__is_no_results') || is_404() ) : ##no search results or 404 cases ?>
                                <article <?php tc__f('__article_selectors') ?>>
                                    <?php do_action( '__loop' ); ?>
                                </article>
                            <?php endif; ?>
    
                            <?php if ( have_posts() && !is_404() ) : ?>
                                <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
                                    <?php the_post(); ?>
                                    <article <?php tc__f('__article_selectors') ?>>
                                        <?php
                                            do_action( '__loop' );
                                        ?>
                                    </article>
                                <?php endwhile; ?>
    
                            <?php endif; ##end if have posts ?>
    
                        <?php do_action ('__after_loop');##hook of the comments and the posts navigation with priorities 10 and 20 ?>
    
                    </div><!--.article-container -->
    
            	<?php wp_reset_query(); ?>
    
            	<?php do_action( '__after_article_container'); ##hook of left sidebar?>
    
            </div><!--.row -->
        </div><!-- .container role: main -->
    
        <?php do_action( '__after_main_container' ); ?>
    
    </div><!--#main-wrapper"-->
    
    <?php do_action( '__after_main_wrapper' );##hook of the footer with get_get_footer ?>

    Save the code above in your child theme folder, as blog.php, than select it as the page template for the page you want to display your blog on.

    You’re done. You can now have your blog on both front page and any other page at the same time.

    Besides, you can modify this template to display a different list of posts. Or anything else, really… like custom posts, posts from custom taxonomies, posts from a select number of categories, posts by one or more authors, posts with a specific value in a specific custom post meta, or any combination of the above. And more. Just see query_posts documentation. That’s where all the magic of this template resides.

    After you’ve made your query_posts() return the results you want, you need to change the name of the template accordingly, change the name of the file accordingly (there is a naming structure logic for templates in WP, if you want your template to apply automatically to different archives, for example) and select it as page template for the page you want to display the custom list on. Just don’t remove the

    &paged='. get_query_var('paged')

    part from query_posts(), or you will lose pagination.

    Enjoy.

Viewing 15 replies - 1 through 15 (of 35 total)
  • Wow! Thanks! 🙂

    Thanks again acub.

    That feature could be among future Customizr upgrades 🙂

    @stickfinger: I don’t see that happening, really.

    The blog template itself doesn’t add a lot of functionality, except for people who want to have the blog list in two places on their site: the first page (through theme options) and on a separate blog page, using this template. If you don’t want the blog on first page, you can just select the blog page from theme options without a template.
    The real use of the code above is the possibility to tailor the query string to your needs and create a the custom list of posts you need, in a static page.

    The snippet I posted may seem like a lot of code, but it’s not. Besides declaring the template, I only added two lines of code to index.php:

    <?php query_posts(' some_qyuery_string '); ?>

    and

    <?php wp_reset_query(); ?>

    Looking at it now, I even think I could have done it better. I should have placed them so that sidebars get included in the custom query, not excluded. On a lot of complex CMS’s sidebars display different content depending on the page/query being displayed and my sidebars (in the example above) are told they’re on a static page, while they should be told they’re on a custom query page and display accordingly.

    What I’m saying is that Customizr doesn’t need a blog template so much, but having a guide on how to make a template for a custom list of posts may have some value in some situations. But you need to come up with the custom query string yourself to make it useful, and that cannot be added in a “future Customizr upgrade”.

    You have to Customiz’it!

    Hi,

    I can’t get Customizr to work the way I want it to.
    I would like no page as the front page (so only the slider
    and the 3 highlighted pages) and a link in my menu to my blog
    page. I’ve made an blog.php, but I can only get it to work if
    I put my blog as the first page… Can you help me out?

    Thanks!!

    (my website is http://www.gelukkiglijf.nl)

    Start a new topic please.

    This is brilliant, thank you!!

    halffinn

    @halffinn

    Just a quick thank you – this is great! 🙂

    the blog page works fine, but i can’t create different templates by post category.

    i just tried to edit the query_posts() adding &cat=categoryname
    and it doesn’t work.

    sorry for my inexperience and thanks in advance for any suggestion.

    Start a new Topic please.

    @narcopastello: you can’t pass category slugs to cat in query_posts, read the docs. Only ids work. Use -{cat_id} to exclude categories.
    Example: &cat=1,3,-2 // includes posts from cats with cat_ids of 1 and 3 but not from cat with id of 2

    Or just use the array mode:

    $args = array('cat' => array(1, 3, -2), 'paged' => get_query_var('paged')); // etc...
    query_posts($args);

    This way you can pass more complex params, like meta query parameters, order by postmeta values, only posts with custom meta set, etc…

    I’m having the same problem as kinggronan can anyone explain how can I archive that same behavior?

    Here is the answer, for anyone having a similar request as pfrainho or kinggronan.

    How can I adjust the width of the posts so they don’t overlap the widget sidebar?
    http://dev.srbenefitsolutions.com/sbsinwords/

    I would like to show sidebars as well and it looks like the article-container needs to be something like span9 instead of span12. I think it has something to do with the following code in blog.php, but I don’t know what to change.

    <div class=”<?php echo tc__f( ‘__screen_layout’ , tc__f ( ‘__ID’ ) , ‘class’ ) ?> article-container”>

    Can anyone help?

    Yep, but when you select this page template in your page, you can set the layout. So no code needed.
    Look that if you’re using a recent customizr version (I think > 3.1.9) you have to change this line:

    <div class="row">

    with this line:

    <div class="<?php echo tc__f( 'tc_column_content_wrapper_classes' , 'row column-content-wrapper' ) ?>">

Viewing 15 replies - 1 through 15 (of 35 total)
  • The topic ‘[Resolved] [Theme: Customizr] Add blog or custom post list templates to Customizr [How to]’ is closed to new replies.
Skip to toolbar