• Resolved halffinn

    (@halffinn)


    hi,
    i’m using the Customizr theme, with a child theme, and trying to figure out a way to get only specific blog posts to appear on the front page. my site is https://www.auditbucket.com
    – i have the front page set to “your latest posts”
    – posts & pages layout is set to max 5 posts
    – but there are 2 specific posts that i want to display, and the rest i would like to hide from the front page, and just have it appear on the blog.

    any way to do this? i thought about a adding a rewrite function to the child theme functions.php that would intercept the bit of code that selects the blog posts and displays them on the front page, and then replacing that with what i need, but i’m having trouble figuring out the exact function and how to specify only the 2 blog posts that i want.

Viewing 4 replies - 1 through 4 (of 4 total)
  • tomaja

    (@tomaja)

    I guess the simpliest way is to add those two posts to specific category which will be then shown on the front page.

    Thread Starter halffinn

    (@halffinn)

    but how would that work? i want the front-page slider and featured texts, and to do that, i need to choose “your latest posts”

    acub

    (@acub)

    Go to posts list in backend. Quick edit the two posts you want on first page and make them sticky (it’s near status dropbox, in the right side).

    Add this code in your child theme’s functions.php.

    add_action( 'pre_get_posts', 'sticky_posts_on_frontpage' );
    function sticky_posts_on_frontpage ( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'post__in', get_option( 'sticky_posts' ));
    	} else {
    		$query->set('post__not_in', get_option( 'sticky_posts'));
    	}
    }

    The function will make your first page display only sticky posts and will hide them from the rest of the website. If you don’t want to hide them from the rest of the website, just delete the else part, including the accolade after it. However, being sticky posts they will always show up at the top of the list of your posts. You’re not limited to two posts.

    This could be done by using a category instead of sticky posts, if you think you’ll actually use sticky posts option in the way it was meant (make some posts stick to the top of the list). Look over more examples for pre_get_posts here.

    Thread Starter halffinn

    (@halffinn)

    awesome – thanks so much! that’s a great solution for me. the only downside is that on the main blog page, the sticky articles are now on top, and everything else is chronologically descending after it. but that’s fine. i want people to see the sticky posts first anyway.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘choose specific posts for front page’ is closed to new replies.