I am using a custom post type to allow the editor of the site to add a number of blocks of content to the site - infoboxes.

Now i would like to give the editor the possibility to choose which boxes appear on which pages.

So if i have defined page 1 and page 2 is there a way to allow the user to choose that on page 1 the infoboxes a and b are shows and on page 2 the infoboxes m and n?



One of two ways:

Add a shortcode, so someone could drop in [content-block id="12"] wherever they wanted. Here's a very simple example without a lot of error checking.

add_action( 'init', 'wpse29418_add_shortcode' );
function wpse29418_add_shortcode()
    add_shortcode( 'content-block', 'wpse29418_shortcode_cb' );

function wpse29418_shortcode_cb( $atts )
    $atts = shortcode_atts(
                    'id' => 0
    // then use get_post to fetch the post content and spit out.
    $p = get_post( $atts['id'] );
    return $p->post_content;

Alternatively, you could create a very large options page on the back end that allows users to assign content blocks to pages. Then hook into the_content and place the info boxes.

add_filter( 'the_content', 'wpse29418_content_filter' );
function wpse29418_content_filter( $content )
    global $post;

    // get the option that stores the data.
    $opts = get_option( 'wspe29418_opts' );

    // check to see if the post has a content block assigned.
    if( isset( $opts[$post->ID] ) )
        //do stuff here
    return $content;

The downside to the second approach is there you're limited to either placing the content block before or after the original post content or replacing that post's content all together.

Shortcodes would be the most flexible for your end user.

  • 1
    hi Christopher, thank you for your answer. I have found the plugin reusables wordpress.org/extend/plugins/reusables it seems this is what i need. it is based on your solution with shortcodes.
    – memical
    Sep 25 '11 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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