• Resolved valleyflyin

    (@valleyflyin)


    Hello,
    This is my first time posting. I a pretty new at css. I am trying to have 3 or so recent posts appear on my static front page and also have a separate page with all my blog post on it. I am trying to do that in the customize is section, but it does not appear possible from there.

    Is this possible though the css editor?

    My website is

    I have already created a child profile.

    Thanks,
    Paul

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter valleyflyin

    (@valleyflyin)

    Maybe it’s possible to do with a plugin.
    Does anyone know of a plugin that would let me know how to do this?

    Most of the one’s I find only add the posts in the widget area, but I already have recent posts there.

    Thanks,
    Paul

    I’m drawing entirely from this post, which I have updated for 3.1.6 below. It should work as expected in future versions, but you might want to keep an eye on it.

    Credits to @acub.

    First, you need to set up your front page in Customiz’it so that it contains the blog posts. Use Dashboard > Settings > Reading to change the number you want on the font page.

    Then set up an empty page called “Blog” or “News”, whatever. This is where we will show the full set of blog posts.

    When you show your blog page at this point, it just shows a blank page.

    You will need a child theme for the next step. Create a file as follows:

    <?php
    /*
     * Template Name: My Extra Blog Page
     * @package Customizr
     * @since Customizr 3.1.6
     */
    ?>
    
    <?php do_action( '__before_main_wrapper' ); ##hook of the header with get_header ?>
    <div id="main-wrapper" class="<?php echo tc__f( 'tc_main_wrapper_classes' , '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?>
    
                  <!-- NEXT LINE INSERTED FOR BLOG PAGE (1 of 2) -->
                  <?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?> <!-- ADDED LINE --> 
    
                    <div id="content" 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(); ?>
    
                                    <?php do_action ('__before_article') ?>
                                        <article <?php tc__f('__article_selectors') ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action ('__after_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 -->
    
                  <!-- NEXT LINE INSERTED FOR BLOG PAGE (2 of 2) -->
                  <?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 ?>

    What is this? Basically it is the standard template (index.php) with the addition of two lines:

    • Just before the content, you query the WP databse to get the published posts (in this case 10 per page — change the number if you want)
    • Just after the content, you reset the query.

    Save this in your child-theme’s folder as blog.php.

    Caveat:
    I’m not a programmer. I note that the use of query_posts() is discouraged in the Codex (though the above code follows the recommendations in the Codex to avoid any consequences). Not being a programmer, I cannot adapt the two lines above to the recommended methods. Perhaps someone with more programming knowledge could help out.

    Now edit the blog page (reload it if it was already open) and change the template for the page to “My Extra Blog Page”.

    You should now have your result.

    (If this template doesn’t act as expected in future releases, you can recreate it the way I did above, taking the index.php and adding those 2 lines in the right place.)

    From WP’s point of view, the header, sidebars and footer of this template all know they’re on a page. The page you set the template for. If you want it to know you’re on a blog page, you have to extend the query accordingly, to include whatever you want it in (header, sidebars, footer, etc…). (In the case any of them displays different content on pots lists). To do that, move the lines 1 and 2 in the template, to include the parts you need.

    query_posts is discouraged because it creates another instance of the main query. But in our case it is what we need.

    Besides, it’s not efficient (does both the initial page query and, when it gets to the content it does a new one and replaces the first). And you also need to be pass the pagination from the list to the page.

    Not sure I understand what you’re recommending…?

    I was just clarifying why query_posts is not usually the right solution.
    Normally one should use this new WP_Query.

    $new_query = new WP_Query( $args );
    
    if ( $new_query->have_posts() ) {
            // stuff before posts here
            while ( $new_query->have_posts() ) {
    		$new_query->the_post();
    		// stuff for each post here
    	}
            // stuff after posts here
    } else {
    	// sorry mate, no posts
    }
    // reset post data to original
    wp_reset_postdata();

    And also, I was pointing out that if anyone is displaying content aware information in the sidebar, and the sidebar is left out of the custom query, it will output data for a page template, not for a post list template.

    Thread Starter valleyflyin

    (@valleyflyin)

    Thanks ElectricFeet and acub it worked great.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can I have recent blogs appear on front page and a seperate page’ is closed to new replies.