WordPress.org

Ready to get started?Download WordPress

Forums

Add metabox "featured" content and show checked content in theme files (13 posts)

  1. kortschot
    Member
    Posted 2 years ago #

    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...

    Paul

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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:

    array(
    	'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. :-)

  3. kortschot
    Member
    Posted 2 years ago #

    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.

    Paul

  4. kortschot
    Member
    Posted 2 years ago #

    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)

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

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

    Paul

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    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...

  6. kortschot
    Member
    Posted 2 years ago #

    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.

    thanks.
    Paul

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    I've dropped the code I've used previously to create text-based meta boxes into http://pastebin.com/hZpnmhGd 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.

  8. kortschot
    Member
    Posted 2 years ago #

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

  9. esmi
    Forum Moderator
    Posted 2 years ago #

    No problem :-)

  10. Digital Raindrops
    Member
    Posted 2 years ago #

    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>
    	<td>
    		<?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; ?> />
    	</td>
    </tr>

    Could shorten the $checked to something like:

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

    HTH

    David

  11. Digital Raindrops
    Member
    Posted 2 years ago #

    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.

    HTH

    David

  12. kortschot
    Member
    Posted 2 years ago #

    Hey thanks,

    Ill check in to that... will let you know!

    Paul

  13. kortschot
    Member
    Posted 2 years ago #

    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]

Topic Closed

This topic has been closed to new replies.

About this Topic