Support » Themes and Templates » [Theme: twentyeleven] Add a div

  • I’m building a child theme on twentyeleven. I’m using the Showcase feature to have a big sticky post at the top of the page. Between that and the regular feed of recent posts, I want to have a horizontal advertisement — the width of the column (585 px I think it is and 100px deep or so.

    One idea I had was to make a custom post type for the client to insert and remove ads (they do all the ads themselves). So I want to be able to have the most recent of those custom post type posts sit in that spot indefinitely.

    I thought if I create a new div (#ad or something), I could just float it into that spot and have it pull in the custom post.

    I want to know how to create the div and how to have it pull in the right post. This is probably elementary stuff, but I’m a novice with php. I’m wanting to learn, though — so if somebody could point me in the right direction I’ll happy to try to figure it out.

    Or if there is a better/simpler way to do this, let me know that too 🙂


Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Bob,
    Try this, based on a twenty eleven child theme!
    Create a new “Child Theme” file called content-advert.php and add code like this:

    $args = array('post_type' => 'advert', 'numberposts' => 1, 'post_status' => 'publish' );
    $post_query = new WP_Query( $args );
    if ( $post_query->have_posts() ) : $post_query->the_post(); ?>
       <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <div class="aligncenter"><?<?php the_content(); ?></div>
       <div class="cleared"></div>
    <?php endif; ?>

    To call this file, us get template part to load the file!

    <?php get_template_part('content','advert'); ?>

    To center it, twenty eleven already has a style for that, so we use a wrapper div!

    <div class="aligncenter"><?php the_content(); ?></div>




    Thanks! That’s great.

    Just so I’m clear, I’m going to paste the get_template_part bit of code into showcase.php just after the all the slider stuff, and right above the // Our new query for the Recent Posts section comment, right?

    And assuming I have that bit of code you passed on, I’m not clear on this: When I add the post type using something like the following (from the codex) …

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    	register_post_type( 'acme_product',
    			'labels' => array(
    				'name' => __( 'Products' ),
    				'singular_name' => __( 'Product' )
    		'public' => true,
    		'has_archive' => true,

    … do I register the post type as ‘advert’ or give it the name ‘Adverts’ & ‘Advert’? That is: which is being referenced by the ‘advert’ in this line?

    $args = array('post_type' => 'advert', 'numberposts' => 1, 'post_status' => 'publish' );



    Hi Bob,
    That is correct, but a little more, you only want editor and title, and you do not want it in the Nav Menu’s!

    This is what you might put in the “child themes” functions.php file, if adding to a functions php then omit the opening <?php, else save as functions.php in the child themes folder.

    The code in this pastebin post, is ‘copied and edited’ as an example from one of my themes, so is un-tested.



Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Theme: twentyeleven] Add a div’ is closed to new replies.