Support » Themes and Templates » Add metabox "featured" content and show checked content in theme files

  • Hi there people!

    Last week I’ve had some great help from Chip Bennett and alchymyth on this post what helped allot. Now I was wondering if I (again) could get some php wizardry from you people. I tried to explain and express myself as clear as possible.

    Where I am so far, what is working. (see code), and a little grid sketch.
    At the moment I’ve accomplished with the help from above… a page with 3 loops.

    Loop / Query 1
    The 1th loop pulls some posts out of a category, nothing to it…. works. Functionality here is unclear and still in debate.

    Loop / Query 2
    The second loop gets post from the category “news”, puts the newest in the left column with thumbnail, title and excerpt. A few in the middle column, only title and excerpt and in the last and third column a list with titles. Also working like it should.

    Loop / Query 3 (this is what this thread is about)
    At the moment this gets a few posts out of a category.

    What it needs to do is the following:

    • client must be able to check a checkbox on a page or post when that post is featured content or not.
    • in the html it will be a list with items, every item containing the featured image, the title and the excerpt.

    So I’m guessing there will be some PHP in functions.php, like the example above. When a post or page will be checked with a checkbox that it is featured content it will shop up. If there is no featured content, nothing will show.

    I know this has to be done with “Add Meta Box” and that this Example gets me a long way… but my PHP just ain’t good enough.

    Well, thanks in advance, I’m almost thinking I’m asking to much here.

    It’s much appreciated…


Viewing 12 replies - 1 through 12 (of 12 total)
  • esmi


    Forum Moderator

    I think you’re definitely on the right track with a custom field and a custom meta box but having a checkbox for the custom field could add an extra layer of complexity. I’d suggest that you start with something a lot simpler. Create a custom meta box called “featured” and just enter “yes” into a basic text input. Then construct your query based upon something like:

    $query = new WP_Query( array( 'meta_key' => 'featured', 'meta_value' => 'yes' ) );

    and see how that goes. You could then look at turning the custom meta text input into a checkbox alter on.

    Just had another idea of a way to approach this. What about hijacking post formats? If you added theme support for the “status” format (for example), the client could then mark featured posts as “status” and you could add:

    	'taxonomy' => 'post_format',
    	'field' => 'slug',
    	'terms' => array( 'post-format-status' )

    to your query. I’ve not tried this myself and it is a but hackish but it would be very quick to code. 🙂

    hmm i’m intrigued by your idea using post formats…

    Thing is that clients love it when stuff is named for what it is, where they have… well somewhat of a point, but I certainly will think this over.

    Starting to get late here.
    Thanks and i’ll let you know.


    Yeah, well… kinda does what it needs to…

    got this in functions.php

    add_theme_support( 'post-formats', array('status'));
    add_post_type_support( 'page', 'post-formats' );

    and this for the loop (this probably could be a little cleaner)

        <article id="">
    		<?php $args = array(
        		'tax_query' => array(
            		'taxonomy' => 'post_format',
            		'field'    => 'slug',
            		'terms'    => 'post-format-status'
            query_posts( $args );
            if ( have_posts() ) : while ( have_posts() ) : the_post();
                echo '<br />';
                endwhile; else:
                echo 'no';

    Now if I could rename status in Featured someway… that possible?
    I love the easiness but think my client prefers a more… tailored solution.




    Forum Moderator

    I had a look through the core scripts in the hopes of finding a filter that you could use to change the label text but there’s nothing 🙁

    Looks like it’s back to the custom meta box solution…

    Hmm… must say, really liking this codewise, quick clean and simple.

    It’s just a matter of educating the people who will be using it. Nevertheless still good practice to fiddle with the meta-box.

    Ill update when I have something new, maybe somebody else takes a peak here and post’s some other thoughts.




    Forum Moderator

    I’ve dropped the code I’ve used previously to create text-based meta boxes into Not sating it’s perfect but I do try to build using recommended best WP practices. The code is commented, so there may be bits in there that you can re-use.

    wow thanks a allot!
    again, ill keep you posted.



    Forum Moderator

    No problem 🙂

    If I am reading this right you want to dispaly a checkbox and set this to true if it is in the post meta?

    If so this will display a checkbox and check it if it is set true in the post meta!

    <tr class="form-field">
    	<th scope="row" valign="top"><label for="my_cb"><?php _e('My Checkbox'); ?></label></th>
    		<?php if( get_post_meta( $post->ID, 'my_cb', true ) ){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
    		<input style="float:left; width: 15px; border: none;" type="checkbox" id="my_cb" name="my_cb" value="true" <?php echo $checked; ?> />

    Could shorten the $checked to something like:

    <?php $checked= get_post_meta( $post->ID, 'my_cb', true ) ? "checked=\"checked\"" : ""; ?>



    I do have a couple of post with downloadable child themes that might help, between the two.
    This Page of Posts has meta textboxes, and select dropdown boxes.

    This one extends the categories with additional boxes including a check box.



    Hey thanks,

    Ill check in to that… will let you know!


    I found this…

    Only gives a parse error for me in an empty functions.php.

    But the description does exactly what I need.

    Like Esmi’s idea… but without post format. (what by the way works great), only I want to try and get it a bit more user friendly… and what if I need the post-format for something else later on….

    So I have a checkbox in post and page edit page where I can select if it is featured content or not.

    So on index.php we can get it by a query like:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Add metabox "featured" content and show checked content in theme files’ is closed to new replies.