0

I've registered a custom post type named portfolio and have coded it's respective archive-portfolio.php file. Now, my requirement is to make this page available to be set as front page. For which I want to adopt the same approach as WooCommerce has adopted, it allows us to assign any static page to products archive page one time and then afterwards we can easily set/unset that page as our front page anytime without any extra step.

I know there are some solutions floating around like this: How to use a custom post type archive as front page?

If I adopt the above-mentioned solution, It'll work but if I decide to change my front page afterwards to something else I'll have to remove/disable this code, in addition. Whereas, in the case of WooCommerce, I don't need this extra step.

My question: How to set archive-portfolio.php as my front page in the same way as WooCommerce allows us to set archive-product.php as home page (i.e. without extra step)?

5
  • you don't need the extra step in Woocommerce because they've provided a setting for you to choose the shop page, to duplicate the way Woocommerce works would that not also require that same sort of setting?
    – Milo
    Commented Feb 28, 2016 at 18:01
  • WooCommerce provides an option to assign any static page to archive-product.php only one time afterwards that static page can be set/unset as front-page without any additional step afterwards, Whereas, the solution linked above requires to do an additional step (adding/removing relevant code) every time we want to set/unset our archive page. Commented Feb 28, 2016 at 18:29
  • 1
    What I'm suggesting is to add an option that stores the archive page ID, then change the option check in the code you linked from page_on_front to your_archive_page, that would duplicate the Woocommerce behavior. You need to add some way to set that option though, the same way Woocommerce does.
    – Milo
    Commented Feb 28, 2016 at 18:43
  • Solved! The logic was pretty straight forward yet I didn't thought of it before. Thanks very much for the suggestion. Commented Feb 28, 2016 at 19:01
  • You can add an answer explaining how you solved it so that it may help others in the future!
    – Milo
    Commented Feb 29, 2016 at 1:21

1 Answer 1

2

I was able to solve the issue on https://wordpress.stackexchange.com/users/4771/milo suggestion. For this, I created a drop down menu in theme customizer from where I can select a dummy static page to act as Portfolio Page. I've used following code for drop down menu:

add_action( 'customize_register', 'th_customize_register' );

function th_customize_register($wp_customize) { 

    // Add: Drop Down Pages
    $wp_customize->add_setting( 'th_portfolio_page_id', array (
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control('th_portfolio_page_id', array(
        'label'    => esc_html__('Portfolio Page', 'themeshash'),
        'description' => esc_html__( 'You must have to define it if you want to set your portfolio as your homepage.', 'themeshash' ),
        'section'  => 'title_tagline',
        'type'     => 'dropdown-pages',
    ) );


}

Once this drop down is setup, now we can add the following code to conditionally assign above defined static page to archive-{post-type}.php

if ( get_option('page_on_front') == get_theme_mod('th_portfolio_page_id')  ) {

    add_action("pre_get_posts", "th_assign_portfolio_page");

    function th_assign_portfolio_page($wp_query){
        //Ensure this filter isn't applied to the admin area
        if(is_admin()) {
            return;
        }

        if($wp_query->get('page_id') == get_option('page_on_front')):

            $wp_query->set('post_type', 'post-type-name-here');
            $wp_query->set('page_id', ''); //Empty

            //Set properties that describe the page to reflect that
            //we aren't really displaying a static page
            $wp_query->is_page = 0;
            $wp_query->is_singular = 0;
            $wp_query->is_post_type_archive = 1;
            $wp_query->is_archive = 1;

        endif;

    }

}

Using this method, now one can choose any dummy static page to act as our post type archive page only once and then we can set/unset that static page as front page just like any other page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.