Recent posts on Static Frontpage

Mauer Themes Support Storyteller Recent posts on Static Frontpage

Tagged: , ,

This topic contains 8 replies, has 2 voices, and was last updated by  Paul 10 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1677

    johandewild
    Participant

    Hi there,

    I went through all topics but couldn’t find an answer to my issue.

    I absolutely love your theme, first of all 🙂

    There’s one thing I’m trying to accomplish which probably requires editing the PHP code.

    What I did:

    Installed the theme. Created a Child Theme. Changed Reading from ‘Latest Posts’ to Static Front Page. Created a custom template, where I copied the code of ‘Main Index’ of the Parent theme. Created a Page called ‘Home’, which uses this template. Added 2 Revolution Sliders for some content above the posts. Want now to have a ‘Recent Posts’ below these two sliders, with the latest 3 posts.

    I’ve used the code I found in another topic:

    function change_number_of_posts_on_home_page( $query ) {
    		if ( $query->is_front_page() && $query->is_main_query() ) {
    				$query->set( 'posts_per_page', 3 );
    		}
    }
    add_action( 'pre_get_posts', 'change_number_of_posts_on_home_page' );

    However, this is what I get:

    https://i.imgur.com/keyZUZ9.png

    Any idea if this is even possible at all, or am I going about this in the wrong way

    • This topic was modified 10 months ago by  johandewild.
    #1687

    Paul
    Keymaster

    Hello Johan,

    It’s great to know you like the theme.

    Do I understand it right that you were successful in creating a static page for posts and in adding those sliders, so the only issue you have now is to output those 3 posts?

    Also, can you please post the full code of your template?

    If you enjoy the theme, please take a moment and rate it on ThemeForest. This literally takes a moment and creates a huge difference.

    Regards,
    Paul

    #1688

    johandewild
    Participant

    Hi Paul,

    Thank you for your reply.

    Correct, I created a child theme and created a new template called ‘Custom Template’ and copied the original code of Main Index Template and then tweaked it a bit.

    I actually got it working with the following code:

    <?php /* Template Name: Custom */ ?>
    
    <?php get_header(); ?>
    
    <div>
    	<?php putRevSlider('main', 'homepage'); ?>
    </div>
    
    <div>
    	<?php putRevSlider('about', 'homepage'); ?>
    </div>
    	
    <h1 id='blog-title'>
    	Recent Posts
    </h1>
    
    <div class="section-main-content">
    	<div class="container">
    		
    <!-- BEGINNING -->
    		
    		<?php 
    		$temp = $wp_query; $wp_query= null;
    		$wp_query = new WP_Query(); $wp_query->query('posts_per_page=3');
    		get_template_part("content", mauer_get_layout_setting("layout_style"));		
    		wp_reset_postdata(); 		
    		?>
    		
    <!-- ENDING -->
    
    		<?php if (is_search()): ?> 
    			<h3 class="archive-title"><?php echo esc_html_x('Search results for: ', 'Search results page heading', 'mauer-storyteller'); echo the_search_query(); ?></h3>
    		<?php endif ?>
    
    	</div><!-- /.container -->
    </div><!-- /.section-main-content -->
    
    <?php get_footer();?>

    I’m now facing the next issue where I need to change the pagination part. It automatically adds the next_page_link which I don’t want, I just want it to link to my blog page. I copied the content-grid_3_cols.php to my child theme and edited out the pagination part. It shows the button I want, but BOTH on the front page, as well on the blog page… Here’s the code I used:

    <?php
    /*
     * Posts list
     */
    ?>
    
    <div class="posts-list-layout-grid_3_cols">
    
    	<?php if ( have_posts() ) : ?>
    		<?php global $wp_query; ?>
    		<?php  while ( have_posts() ) : the_post(); ?>
    
    			<?php $current_post_index = $wp_query->current_post + 1; ?>
    
    			<?php if (mauer_get_layout_setting("big_latest") && $current_post_index==1 && get_query_var('paged')==0 && is_home()): ?>
    				<!-- big latest -->
    				<div class="row">
    					<div class="col-xs-12">
    						<div <?php post_class('post-card big'); ?>>	
    							<a href="<?php the_permalink(); ?>" class="entry-thumb-link">
    								<div class="entry-thumb-wrapper">
    									<?php the_post_thumbnail('mauer_cover_thumb'); ?>
    									<div class="entry-thumb-overlay"></div>
    								</div>
    							</a>
    							<div class="row">
    
    								<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3 text-center">
    									<div class="entry-meta">
    										<span class="entry-date"><a href="<?php the_permalink(); ?>"><?php echo get_the_date(); ?></a></span>
    										<?php if (is_sticky()): ?><span class="entry-sticky"> &mdash; <i class="fa fa-sticky-note-o"></i> <?php esc_html_e('Sticky', 'mauer-storyteller') ?></span><?php endif ?><span class="entry-cats"><?php echo get_the_category_list( esc_html_x( ', ', 'blog entry categories separator', 'mauer-storyteller' ) ); ?></span>
    									</div>
    									<h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    									<div class="entry-excerpt">
    										<?php if (is_search()) {the_excerpt();} else {the_content();} ?>
    									</div>
    								</div>
    
    							</div>
    						</div><!-- /.post-card -->
    					</div>
    				</div>
    				<?php continue ?>
    			<?php endif ?>
    
    			<?php if (mauer_get_layout_setting("big_latest") && get_query_var('paged')==0 && is_home()) {$row_threshold = 2;} else {$row_threshold = 1;} ?>
    
    			<?php if ( ($current_post_index % 3) == $row_threshold ): ?><div class="row posts-row"><?php endif ?>
    
    				<div class="col-xs-12 col-sm-4">
    					<div <?php post_class('post-card small'); ?>>	
    						<a href="<?php the_permalink(); ?>" class="entry-thumb-link">
    							<?php the_post_thumbnail('mauer_thumb_2' ); ?>
    							<div class="entry-thumb-overlay"></div>
    						</a>
    						<div class="entry-meta">
    							<span class="entry-date"><a href="<?php the_permalink(); ?>"><?php echo get_the_date(); ?></a></span>
    							<?php if (is_sticky()): ?><span class="entry-sticky"> &mdash; <i class="fa fa-sticky-note-o"></i> <?php esc_html_e('Sticky', 'mauer-storyteller') ?></span><?php endif ?><span class="entry-cats"><?php echo get_the_category_list( esc_html_x( ', ', 'blog entry categories separator', 'mauer-storyteller' ) ); ?></span>
    						</div>
    						<h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    						<div class="entry-excerpt">
    							<?php if (is_search()) {the_excerpt();} else {the_content();} ?>
    						</div>
    					</div><!-- /.post-card -->
    				</div>
    	
    
    			<?php if ( ($current_post_index % 3) == $row_threshold-1 || $current_post_index == $wp_query->post_count ): ?></div><!-- /.posts-row --> <?php endif ?>
    
    		<?php endwhile; ?>
    
    		<div class="clearfix"></div>
    	
    	<?php if ( is_front_page() ) : ?>
    				<div class="row">
    					<div class="col-sm-4 col-sm-offset-4">
    						<a href='/blog'><div class="ghost-button-link">More Posts</div></a>
    					</div>
    				</div>
            <?php endif; ?>
    	
    		<?php endif ?>
    
    </div>

    I know this is probably way outside the scope of support, but can you at least tell me if I’m heading in the right direction? 🙂

    #1689

    johandewild
    Participant

    I’ve added back the pagination because I realized it doesn’t make sense to take it out completely.

    The first three posts work with the above code. However, like I mentioned I am now getting the pagination button also on the frontpage. I am trying to include if (!front_page()) within the content-grid-3.php file, but I can’t seem to get it to work…

    #1690

    Paul
    Keymaster

    Ok, the following should work:

    Don’t do anything to the parent theme.

    Create your child theme and the ‘Custom’ template in it (the same code as you’ve posted above).

    Then change the code inside the <!-- BEGINNING -->...<!-- ENDING --> block to this:

    <?php
    	$args = array('posts_per_page' => 3);
    	query_posts($args);
    	get_template_part("content", mauer_get_layout_setting("layout_style"));
    	wp_reset_postdata();
    ?>

    No other template modifications needed. Don’t do anything to any other files.

    Please let me know if the above code has worked out for you. Then I’ll let you know how to hide pagination with CSS.

    #1691

    johandewild
    Participant

    Works like a charm, thanks 🙂

    However, regarding hide pagination, let me clarify that i DO want pagination on the Blog Page with the Older and Newer Posts buttons. I just don’t want it to show on the front page, because there I would like to have a button which says: ‘More Posts’ or whatever, which links to the /blog url. Hope that makes sense.

    I rated the theme, nothing but <3

    #1694

    Paul
    Keymaster

    First, hide the pagination.

    To do that use the following CSS code:
    .page-template-your_file_name-php .mauer-pagination {display:none;}
    In this code change .page-template-your_file_name-php to reflect your template file name. E.g. if you template file is called custom.php, this should be .page-template-custom-php

    Second,
    Add the button to the template file using the following code. Mind that this should be inside the .section-main-content div. For instance, a good place for this code would be right after your <!-- ENDING --> mark.

    <div class="row">
    	<div class="col-sm-4 col-sm-offset-4">
    		<a href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>" class="ghost-button-link">More posts</a>
    	</div>
    </div>

    Thank you for having rated the theme. I really appreciate that.

    Regards,
    Paul

    #1696

    johandewild
    Participant

    Hi Paul,

    Works like a charm, appreciate it 🙂 Will definitely spread the word for this theme, and the awesome support you deliver!

    Regards,

    Johan

    #1699

    Paul
    Keymaster

    You are very welcome, Johan!

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.