Adding content to archive and taxonomy pages on custom post types?

I have a custom post type named “Products” and a taxonomy called “Usage”. On my “Products” landing page I’d like to have custom content that can be edited via WYSIWYG by the client. Same goes for the taxonomy landing page. My thought was to create fake Pages and just pull the content via ID into my archive-products.php and taxonomy-usage.php templates but I was curious if there was a better way to handle it.

Related posts

2 comments

  1. First solution can be using the Settings API and create 2 fields “Products Description” and “Usage Description”, after that showing in your template that fields is easy like a:

    $options = get_option('my_theme_options');
    echo $options['prod_description'];
    // echo $options['usage_description'];
    

    However, settings API is not the best part of WP core, and probably create a settings page for only that fields doesn’t worth it.

    Another approach is use page (with a custom page template) as archive.

    Create a page and call it “Product Archive”

    In it put something like that:

    <?php
    /*
    Template Name: Products Archive
    */
    
     get_header();
    
     if ( have_posts() ) the post();
    
     the_content(); // this will output the page content     
    
     $p_query = new WP_Query('post_type=products');
     if ( $p_query->have_posts() ) { while( $p_query->have_posts() ) {
       $p_query->the_post();
       // this will require a 'entry-product.php' where you can put all your product markup
       get_template_part('entry', 'product');
     } }
     wp_reset_postdata();
    
     get_footer();
    

    After that, in your backend, create a page and assign it to the template just created. Write whatever you want in the page content and when you will open the page, you’ll see the page content and the products.

    The same can be done for taxonomy page. Just change the page template and the query in it.

    If, for any reason, you need to use archive-products.php as product archive, an alternative is create a custom template, but use it only to retrieve the page content.

    Create a php file in your theme, and name it ‘page-prod-description.php’.
    In this file put only:

    <?php
    /*
    Template Name: Products Description
    */
    wp_safe_redirect( home_url() );
    exit();
    

    What this file does, is creating a custom page template. That template can be attached to pages, but those pages can’t be called directly, because if you try it you’ll be redirected to home page.

    Now login on your backend and create a page, title it “Products Description” and assign the page template just created. If you try to access the page http://example.com/product-description you’ll be redirected to home page.

    In your product archive template, archive-products.php, you can use the content inserted in that page like so:

    $desc = get_pages('meta_key=_wp_page_template&meta_value=page-prod-description.php');
    if ( ! empty($desc) ) {
       $page = array_shift($desc);
       echo apply_filters('the_content', $page->post_content );
    }
    

    Now your clients can login in backend and edit the page “Products Description” and everything is wrote in the page content will be shown in the archive page.

    The same, off course, can be done for taxonomy archive.

  2. You could create normal pages, then apply custom templates which contain the appropriate custom loop to output the CPT. Or you could create an options page (or add an option to the Theme Customizer), where the client can add content via WYSIWYG, and then output this content in the appropriate archive template.

Comments are closed.