• Resolved Andrew Zhezhera

    (@andrew-zhezhera)


    I have WP4.1 with my own theme.

    I registered custom post type ‘products’ and tried to add some post formats to it.

    At first, I wrote this code in functions.php:

    add_action( 'after_setup_theme', 'my_theme_setup' );
    function my_theme_setup(){
        add_theme_support( 'post-formats', array( 'aside', 'image', 'quote' ) );
        add_post_type_support( 'products', 'post-formats', array( 'aside', 'image', 'quote' ) );
    }

    It worked okay and added metabox in admin page allowing to select post format (when adding/editing the posts/products)

    However, I didn’t want to add post format support for standard posts, only for products.

    So I removed add_theme_support() call from the above code.

    After that I tried to add/edit the product, but metabox with format selection was gone, and I could not assign the format to it.

    I’d like to know how to resolve this situation.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Andrew,

    If I understand correctly, you have already registered the new post type (i.e. products) –> a good resource is WordPress Codex on Post Types.

    Now you want it to support specific post formats as well.
    Using the example from the WordPress Codex above, you need to add one more line to the post-type registration function:

    'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt'),

    So the full registration function would look something like:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'acme_product',
        array(
          'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
          ),
          'public' => true,
          'has_archive' => true,
          'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt'),
        )
      );
    }

    Hope it helps.

    Thread Starter Andrew Zhezhera

    (@andrew-zhezhera)

    Hi kunstwerck,

    As far as I understand, there are two ways of adding post formats:
    1) by calling add_post_type_support( ‘products’, ‘post-formats’)
    2) by calling register_post_type(‘products’, … , ‘supports’ => array(‘post-formats’))

    The second method is actually alias of calling add_post_type_support(), so they both are basically the same.

    The problem is that without calling add_theme_support() I cannot select format of the product being added/edited, because there is no Format meta box (like this one – https://op111.net/wp-content/uploads/2010/12/wordpress-31-post-formats-aside.png)

    Hi Andrew,

    Sorry for the misunderstanding, now I know what you mean.
    Let me know if this approach works:

    add_action( 'after_setup_theme', 'my_theme_setup' );
    function my_theme_setup(){
        add_theme_support( 'post-formats', array( 'aside', 'image', 'quote' ) );
        remove_post_type_support( 'post', 'post-formats' );
        add_post_type_support( 'products', 'post-formats', array( 'aside', 'image', 'quote' ) );
    }
    Thread Starter Andrew Zhezhera

    (@andrew-zhezhera)

    Hi kunstwerck,

    Yes, it works, but only if add action on ‘init’ hook, not on ‘after_setup_theme’. Using removal function seems a bit awkward to me, but anyway, the original problem is solved.

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add post formats to custom post types’ is closed to new replies.