Content Options on Custom Post Types

Home Page – v1.1 Forums Extensions Layout Manager Content Options on Custom Post Types

This topic contains 9 replies, has 2 voices, and was last updated by  Mark Woodroof 4 years, 1 month ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #4862

    Mark Woodroof
    Participant

    In my Bootstrap Starter theme, I would like to have the ‘Content Options’ enabled on the edit pages for various custom post types (some of which are created by third party plugins such as Courses and Lessons set up in LearnDash).

    Can you recommend how to go about this?

    Also, is there a way of specifying different layouts for single posts and archives for the same custom post type in Layout Manager or do I need to do this using normal WordPress templating? For example on my I want my LearnDash course archive to have a different layout to the single courses.

    Any tips gratefully received.

    Many thanks in advance

    Mark

    PS: Like a lot of others in this forum, I think the Runway framework is absolutely brilliant but I am not in your league programming-wise (I am a marketing consultant / designer first and intermediate level web developer second!) and could really do with more documentation, code snippets and tutorials. I would be happy to contribute to documentation if I felt confident enough knowing what I am doing!

    #4869

    Andy Wilkerson
    Keymaster

    The meta boxes for the Layout Manager can be added to any post type with a filter:

    function my_cpt_for_layout_manager( $types ) {
        $types[] = 'your-post-type'; // add post type slug here
        $types[] = 'another-post-type'; // add post type slug here
    
        return $types;
    }
    add_filter('layout_manager_post_types', 'my_cpt_for_layout_manager');
    

    You can apply a custom layout to nearly any template or view generated by WordPress. If you can test for the condition in the code, it can have a custom layout applied. When you register a new ‘template’ in the layout manager, it’s essentially using whatever test is provided to determine when to show that layout. We’re doing it for a number of areas automatically but there are no limits to adding additional ones. Below is an example of how we are registering all categories in WP automatically. We apply a function instead of a template file for the registration function.

    // Auto-register all Categories
    // -----------------------------------------------------------------
    /**
     * This looks up the WP category and calls the "register_context()" 
     * function. It verifies the loaded page with a function instead of 
     * specifying a template file as the method of knowing when to apply 
     * the custom context.
     */
    if ( ! function_exists( 'auto_register_wp_category_context' ) ) :
    	function auto_register_wp_category_context() {
    
    		$category_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') ); // database look up of all categories
    
    		foreach ( $category_ids as $cat_id ) {
    			$cat_name = get_cat_name($cat_id);
    			register_context( "<strong>".__('Category', 'framework').": </strong>".esc_attr($cat_name), 'category-'.$cat_id, 'verify_wp_category_context');
    		}
    	}
    
    	// Apply filters
    	add_action( 'wp_loaded', 'auto_register_wp_category_context' );
    endif;
    
    /**
     * Called by the "register_context()" function to verify the current 
     * page meets the requirements of a registered context and if so, 
     * returns the $context value. 
     */
    if ( ! function_exists( 'verify_wp_category_context' ) ) :
    	function verify_wp_category_context($context, $template, $values) {
    
    		// If current Category matches a registered $context (format "category-[ID]), return the $context value
    		if ( is_category() && $values['context'] == 'category-'.get_query_var('cat') ) {
    			// Make sure the value is set for this context, otherwise we'll let the default assignment occur
    			$layout = context_cascade($values['context']); 
    			if (isset($layout)) {
    				$context = $values['context'];
    			}
    		}
    
    		return $context;
    	}
    endif;
    

    You could do something like that, using a function to register your archive and single post template for the CPT, or if you are more comfortable registering a specific template file, use the standard register_context() function to point to a physical template file. You can make archive and single CPT templates using the WP structure “archive-{post-type-slug}.php” and “single-{post-type-slug}.php”. Once you have those physical files it’s easy to specify a layout to associate with the template.

    #4873

    Mark Woodroof
    Participant

    Hi Andy

    Thanks for that superb reply – really appreciate it (as will others!)

    That is everything I need to really extend the layouts / implement sitewide (on a site using WP Toolset and LearnDash)

    Thanks again.

    Mark

    #4890

    Mark Woodroof
    Participant

    Hi Andy

    Thanks – the metabox filter example works a treat.

    However, I am struggling slightly with your example on adding a custom layout (I am using the Vellum theme). The new custom post type is automatically registered (presumably by your code above) but I want to register a different layout for the single and archive displays…which I am still not understanding.

    Do you have a simpler example for adding/splitting the custom layout for a separate single and archive display?

    Once I crack this, I will stop bugging you for a while as I will have asked all the questions I need for my current project!

    On the plus side, I am sure others are reading this and finding it helpful.

    Many thanks

    Mark

    #4894

    Andy Wilkerson
    Keymaster

    I’m working on something for you right now.

    #4908

    Andy Wilkerson
    Keymaster

    I think the simplest solution is to create a template file in the theme directory for the single and archive files. Then you can register these directly with the standard template path method.

    Each custom post type can have a unique template file:

    archive-{post_type}.php
    single-{post_type}.php

    https://codex.wordpress.org/Post_Type_Templates

    Once that is done you can register them with the Layout Manager:

    // Register custom post type template file
    // -------------------------------------------------
     if ( ! function_exists( 'my_cpt_template_layouts' ) ) :
    	function my_cpt_template_layouts() {
    
    		// Single CTP Template
    		$cpt_single_template = locate_template('single-{post_type}.php'); // change {post_type} to your CPT
    		register_context( 'My CPT Single', 'my_cpt_single', $cpt_single_template); // $name, $context, $template_file
    
    		// Archive CTP Template
    		$cpt_archive_template = locate_template('archive-{post_type}.php'); // change {post_type} to your CPT
    		register_context( 'My CPT Archive', 'my_cpt_archive', $cpt_archive_template); // $name, $context, $template_file
    
    		}
    
    	// Apply filters
    	add_action( 'wp_loaded', 'my_cpt_template_layouts' );
    endif;
    
    #4912

    Mark Woodroof
    Participant

    Woohoo…thanks Andy…that works…almost perfectly…

    One slight issue, is that the CPT is now registered as a single and archive (by me using the above code) but is also shown again in the auto-generated section…

    If I don’t set the autogenerated one, it goes to the default…I can’t quite work out what is taking precedent…I have tested a bit so if I set it as:

    autogenerated – full width
    archive – full width
    single – right sidebar

    the archive shows as full width and single as right sidebar. However, if I set the autogenerated one as right sidebar it goes awry and does not output what I registered…

    This works for what I need in this instance but ideally, I would like to remove the autogenerated one…is that tricky?

    Cheers

    Mark

    PS: I hope it is OK me asking so many questions as I do not see anyone else posting so much?! Am I the only one trying to do something this complex?! My site is going to be brilliantly flexible once I have completed it and I am really pleased that I persevered with the Runway framework (it has been a bit of a learning curve over the last week).

    #4913

    Mark Woodroof
    Participant

    Hmmm…I spoke too soon, it is not consistently showing sidebars as they should…

    Unfortunately, it is late (here in the UK) and I am too tired to do structured testing now but I think it definitely needs the autogenerated on de-registering to stop the confusion…

    #4919

    Andy Wilkerson
    Keymaster

    It’s always OK to ask questions! We encourage it.

    We may have to add a filter to make it possible to de-register the auto-generated, or simply changing the priority of the custom one might take care of this. In your custom filter, try setting the priority to 11 or 99 (10 is default).

    add_action( 'wp_loaded', 'my_cpt_template_layouts', 11 );

    If that doesn’t help, try 99 instead of 11. It really shouldn’t matter as long as it’s higher than 10, but worth a shot. This will let that filter fire after the auto-generated so it’s the last one and then assured to be used for the layout.

    #4921

    Mark Woodroof
    Participant

    Cool. That worked.

    Now I am going to stop asking questions for a while and get on with content development!

    Mark

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

Fork us on Github.

Runway on Github

Learn with User Guides.

View the Guides


Thank you for being a part of Runway. We hope you have a great experience and together we can make Runway one of the best additions to the WordPress ecosystem and theme development. We're excited to have you here!

escort kutahya escort bornova escort beylikduzu escort amasya escort diyarbakir
Google+ ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?> ?>