WordPress.org

Forums

Customizr
[resolved] [closed] Add blog or custom post list templates to Customizr [How to] (36 posts)

  1. acub
    Member
    Posted 1 year ago #

    <?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.

  2. ElectricFeet
    Member
    Posted 1 year ago #

    Wow! Thanks! :-)

  3. stickFinger
    Member
    Posted 1 year ago #

    Thanks again acub.

    That feature could be among future Customizr upgrades :-)

  4. acub
    Member
    Posted 1 year ago #

    @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!

  5. kinggronan
    Member
    Posted 1 year ago #

    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)

  6. rdellconsulting
    Member
    Posted 1 year ago #

    Start a new topic please.

  7. niknaz
    Member
    Posted 1 year ago #

    This is brilliant, thank you!!

  8. halffinn
    Member
    Posted 1 year ago #

    Just a quick thank you - this is great! :-)

  9. narcopastello
    Member
    Posted 1 year ago #

    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.

  10. rdellconsulting
    Member
    Posted 1 year ago #

    Start a new Topic please.

  11. acub
    Member
    Posted 1 year ago #

    @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...

  12. pfrainho
    Member
    Posted 1 year ago #

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

  13. acub
    Member
    Posted 1 year ago #

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

  14. baileydesigns1
    Member
    Posted 1 year ago #

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

  15. zenny
    Member
    Posted 1 year ago #

    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?

  16. d4z_c0nf
    Member
    Posted 1 year ago #

    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' ) ?>">
  17. zenny
    Member
    Posted 1 year ago #

    Hmm, thanks d4z_c0nf, but I can't get this to work swapping out the the row that includes the hook for the sidebar. The article-container still displays with:
    <div class="span12 article-container" >
    regardless of which page layout I choose and crowds out the sidebar.

    Maybe full width is not so bad after all. LOL

  18. d4z_c0nf
    Member
    Posted 1 year ago #

    wanna copy the entire custom page template you're using in pastebin.com and paste here the link?

  19. zenny
    Member
    Posted 1 year ago #

    Sure, here you go. Apologies for the lack of coding skills.
    http://pastebin.com/XjU2kx26

    Thanks for the help.

  20. d4z_c0nf
    Member
    Posted 1 year ago #

    Oh I see, looks like the query_posts was place a bit too early, which, as you noticed, bothered tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' )
    :D

    wanna try this?
    http://pastebin.com/MGAC1Tf5

  21. zenny
    Member
    Posted 1 year ago #

    SWEET! Works like a charm!! Wish I new how to figure this stuff out myself, but I'm sure glad you were willing to help a newb. Thanks so much!

    Cheers!

  22. d4z_c0nf
    Member
    Posted 1 year ago #

    WordPress is well documented, and if you look into customizr code, step by step, you will learn a lot of things.
    For rapid snippets:
    http://www.themesandco.com/code-snippets/

  23. Leapmkt12
    Member
    Posted 1 year ago #

    HI, this is fantastic! Thank you - just one query, in Customizr the right side bar is span3 & for some reason my side bar is now underneath this blog container rather than to the right. Is there some code I am missing?

  24. Leapmkt12
    Member
    Posted 1 year ago #

    Sorted :-)

  25. Leapmkt12
    Member
    Posted 1 year ago #

    OK don't have this sorted! Damn! Have changed

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

    then added in the get side bar:
    ' </div><!--.article-container -->

    <?php wp_reset_query(); ?>

    <?php do_action( '__after_article_container'); ##hook of left sidebar?>

    <?php get_sidebar( 'right' ); ?>

    </div><!--.row -->
    </div><!-- .container role: main -->'

    Still have the right side bar under the blog list. The website link is: http://saegroup.com.au/about-us/sae-group-news/

  26. acub
    Member
    Posted 1 year ago #

    What you need to do is change

    <div class="span12 article-container">

    into

    <div class="span9 article-container">

    Give me a pastebin of your current template and I'll tell you where and what to change.

  27. Leapmkt12
    Member
    Posted 1 year ago #

    Hey acub! The pastebin is http://pastebin.com/7aE6ZRP0 between you ElectricFeet & rdellconsulting I'll be php 'fundi' in record time (well I'll actually understand what I'm doing and be able to develop more great sites for small businesses. You have no idea how invaluable your help & sharing of your know-how is. THANKYOU!

  28. acub
    Member
    Posted 1 year ago #

    Replace

    <div class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">

    with

    <div class="span9 article-container">

    and you're good to go.

    You're welcome.

  29. d4z_c0nf
    Member
    Posted 1 year ago #

    But, the problem was solved in a slightly modified version of @acub template, I posted some comments above. That version leaves the filter where it should stay and is >= 3.1.9 compatible
    Heres a new link: http://pastebin.com/BaXPVxBj
    Any problem with that?

    You can achieve the same thing much more easily in another way, just putting the following into your child theme functions.php, without using templates, which in my opinion is a better choice. It will not suffer any changes to the core templates of customizr, unless nikeo decides to change the hook "__before_loop." But in that case the maintenance is simple only having to change a string.

    add_action('__before_loop', 'my_blog_page', 5);
    function my_blog_page($query){
        if ( ! is_page('blog') ){ //assuming 'blog' is the page slug of the page you want to be your blog page
            return;
        }
        query_posts('post_type=post&paged='. get_query_var('paged'));
        add_action('__after_article_container', 'wp_reset_query', 5);
    }
  30. acub
    Member
    Posted 1 year ago #

    The problem with your solution is that it also adds the featured pages block, duplicating the home page, while the template solution only produces a blog list. But basically, they use the same method: they both replace the main (page) query with a blog query, maintaining pagination.

    But, than again, why are we talking about problems? There is no problem. If you look closely, this was originally posted as a how-to, because a lot of people were asking how to achieve this. Replacing the query or its parameters can be done in numerous ways: raw sql, changing query params with pre_get_posts, replacing the query based on page slug or page id (your solution) or templating (my original solution).
    I opted for the template solution as I find it more WordPress-like: it gives the user the ability to switch from blog to normal page from the page back-end, not from functions.php. Please note that I am usually a big fan of functions.php mods and child themes.

    Cheers.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic