Site logo

Archived topic

How do I only show 1 blog post on the home page?

10 replies · Started by Chris on December 13, 2018

Viewing posts 1–11 of 11

Right now I have infinite scroll that shows all my blog posts.

My settings-->reading is set to 1 post to display. The home page displays all of my posts

How do I only show 1 blog post on the home page?

Second I want to have buttons at the bottom of my posts like Seth Godin's Blog Page. How do I create code to do that?

https://seths.blog/2018/06/the-two-simple-secrets-to-good-ideas/

Hi there,

Sorry I'm not sure if I understand.

If you only want to show 1 post at the time, why would you need to use infinite scroll?

As for buttons, are you just referring to the style or you want the Next and Back text instead of the default post titles?

Let me know :)

For the buttons, can you answer my question above:

As for buttons, are you just referring to the style or you want the Next and Back text instead of the default post titles?

For dividers, any chance you can link me to the page in question?

The link you provided isn't working.

To add the buttons, you can do something like this:

add_filter( 'next_post_link', function( $output, $format, $link, $post ) {
    if ( ! $post ) {
        return '';
    }
  
    $next = sprintf(
        '<a class="next-post button" href="%1$s" title="%2$s">Next</a>',
        get_permalink( $post ),
        $post->post_title
    );
  
  	$currentID = get_the_ID();
  	$args = array(
	  	'numberposts' => 1,
	  	'order'    => 'ASC',
	  	'orderby' => 'rand',
	  	'post__not_in' => array( $currentID ),
	);
  
    $random_post = get_posts( $args );

    $random_post = sprintf(
        '<a class="button random-post" href="%1$s" title="%2$s">Random</a>',
        get_permalink( $random_post[0]->ID ),
        get_the_title( $random_post[0]->ID )
    );

    return $random_post . $next;
}, 10, 4 );

add_filter( 'previous_post_link', function( $output, $format, $link, $post ) {
    if ( ! $post ) {
        return '';
    }
  
    $prev = sprintf(
        '<a class="button previous-post" href="%1$s" title="%2$s">Previous</a>',
        get_permalink( $post ),
        $post->post_title
    );

    return $prev;
}, 10, 4 );

Then you'll need to style them however you need.

Let me know if that does it or not :)

Do I put this in the editor single post php? or somewhere else?

ok I did code snippets. I don't see any change even after clearing website cache

Make sure post navigation is turned on in Customize > Layout > Blog.

Didn't work

I went to layout > blog > turned on all the options

cleared my cache and nothing happened

I have this as my code in snippets to run everywhere

add_filter( 'next_post_link', function( $output, $format, $link, $post ) {
if ( ! $post ) {
return '';
}

$next = sprintf(
'Next',
get_permalink( $post ),
$post->post_title
);

$currentID = get_the_ID();
$args = array(
'numberposts' => 1,
'order' => 'ASC',
'orderby' => 'rand',
'post__not_in' => array( $currentID ),
);

$random_post = get_posts( $args );

$random_post = sprintf(
'Random',
get_permalink( $random_post[0]->ID ),
get_the_title( $random_post[0]->ID )
);

return $random_post . $next;
}, 10, 4 );

add_filter( 'previous_post_link', function( $output, $format, $link, $post ) {
if ( ! $post ) {
return '';
}

$prev = sprintf(
'Previous',
get_permalink( $post ),
$post->post_title
);

return $prev;
}, 10, 4 );

This archived topic is closed to new replies.