Support » Developing with WordPress » How to use paginate_links on front-page.php with a custom post type?

  • Resolved silviubogan

    (@silviubogan)


    Hello!

    How do I use the function paginate_links in front-page.php or, if I cannot use it as it should be used in front-page.php where it should be better to put it? I have found this post https://wordpress.stackexchange.com/a/254200/182585 and it helped me a lot, but the only issue I see is that, e.g., the link to the second page goes to http://localhost/page/2 and that page gives the user HTTP error 404.

    The code inside front-page.php that is relevant is this:

    echo paginate_links(array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'total' => $the_query->max_num_pages,
        'current' => max(1, get_query_var('paged')),
        // 'format' => '?paged=%#%',
        'show_all' => false,
        'type' => 'plain',
        'end_size' => 2,
        'mid_size' => 1,
        'prev_next' => true,
        'prev_text' => sprintf('<i></i> %1$s', __('...', '...')),
        'next_text' => sprintf('%1$s <i></i>', __('...', '...')),
        'add_args' => false,
        'add_fragment' => '',
    ));

    The posts I show in the front-page.php are cases of ill or poor people, and for these types of post I have made a custom post type. I wish to show them in more pages and display the first page on the home page of the website.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The answer depends on what your front page setting is (blog listing or static page) and which query you are trying to paginate (main query or custom query on the template). Because page 2 requests go 404, I assume you’ve set your site to use a static front page and have a custom post type query on the template. In that situation you cannot use the main query’s paged value because the main query only ever consists of one page. You’d need to implement your own paging query var instead of paged.

    Instead of doing that, I recommend going back to the blog listing front page setting and altering the main query through “pre_get_posts” action to get your CPT instead of the usual blog posts. Then pagination should work automatically without you having to do anything.

    If your front page needs other content than a CPT listing, build it into a custom template named “front-page.php”. The template could for example fetch the content of the current front page and output it before the CPT listing.

    Thread Starter silviubogan

    (@silviubogan)

    If your front page needs other content than a CPT listing, build it into a custom template named “front-page.php”. The template could for example fetch the content of the current front page and output it before the CPT listing.

    How can front-page.php include the content of the current front page?

    Thank you very very much for the information!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use paginate_links on front-page.php with a custom post type?’ is closed to new replies.