Support » Developing with WordPress » Make custom post type archive as front page while keeping blog page for ‘post’

  • Resolved kghugo2000

    (@kghugo2000)


    Hi,

    How can I make CPT archive as front page while keeping a blog page for ‘post’? Most of the solutions I found online simply add new parameter into the query for blog page, but I need to keep both page. What should I do?

    Thank you for reading and your help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Support Team Volunteer

    What I’d do is create a custom template in my theme that is functionally the same as a CPT archive, assign that template to a new, blank page, and then make that new page the home page of the site.

    Keep in mind that the custom template has to do its own query, because the main query will have been for the Page.

    I see. If I do it this way, how can I make that page return false for is_single() and return true for is_post_type_archive()? I used a lot of conditional loading so I have to fix this as well.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Support Team Volunteer

    You wouldn’t. You’d have a different PHP file to display a single CPT or you could use get_template_part to pull in different pieces and put the logic into choosing which template parts you call in.

    If you do it in front-page.php it is expected to handle both cases, but still the main query is based on the settings for show_on_front.

    Thank you so much for your reply. I am not sure if I understand you correctly, but I mean if I make a page template(whatever the name is) and do a WP_query in that page for my CPT, and use that page template in a page, that page will functions like a CPT archive, but how can I make that page to work correctly with conditional tags in WordPress, like is_single() and is_archive()?

    Technically this is still a page, thus it will return true for is_single() and false for is_archive().

    Moderator bcworkz

    (@bcworkz)

    It depends on how you drive the query’s output loop. If you are simply doing foreach on every post returned by the query, you need to use the query object’s equivalent methods. For example $query->is_single(). If you’ve used setup_postdata() and drive the loop in the standard while( have_posts()): manner, functions like is_single() will work as expected within the loop. When the looping is complete you must call wp_reset_postdata() so functions again refer to the main page’s query. Failure to do so will then reference the last post in the loop outside of the loop, causing confusing errors.

    Thank you so much for your reply.My conditionals are used in wp_enqueue_scripts hook to load CSS conditionally for the current page. It seems too late for me to change those value inside the loop. What is the earliest hook I can use to change values of these methods?

    Code I tried but didn’t work for me.

    $args = array ('post_type' => 'news');
    $the_query = new WP_Query($args);
    
    $the_query->is_post_type_archive = true;
    $the_query->is_archive = true;
    $the_query->is_page = false;
    $the_query->is_singular = false;

    I found a solution here:

    https://wordpress.stackexchange.com/questions/30851/how-to-use-a-custom-post-type-archive-as-front-page

    This helped me to set CPT archive as the front page of my site.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.