最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom field - How set featured posts using checkbox in post edit screen?

matteradmin9PV0评论

I want to make selecting a featured post is just by checking a checkbox in edit screen AND be able to retrieve these featured articles from only specific category ?

in short what I'm looking for :

  • set featured post

  • loop throw featured posts in a specific category (not all posts)

Any help? and thanks in advance :)

I want to make selecting a featured post is just by checking a checkbox in edit screen AND be able to retrieve these featured articles from only specific category ?

in short what I'm looking for :

  • set featured post

  • loop throw featured posts in a specific category (not all posts)

Any help? and thanks in advance :)

Share Improve this question asked Sep 17, 2011 at 19:42 BialyBialy 711 gold badge1 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

You can do this by following these steps:

  1. Add a custom meta box to your posts
  2. Create a function to save the meta data using the save_post action
  3. Add the 'meta_key' query argument to whatever query you are using.

Place this in your themes functions.php file:

function register_post_assets(){
    add_meta_box('featured-post', __('Featured Post'), 'add_featured_meta_box', 'post', 'advanced', 'high');
}
add_action('admin_init', 'register_post_assets', 1);

function add_featured_meta_box($post){
    $featured = get_post_meta($post->ID, '_featured-post', true);
    echo "<label for='_featured-post'>".__('Feature this post?', 'foobar')."</label>";
    echo "<input type='checkbox' name='_featured-post' id='featured-post' value='1' ".checked(1, $featured)." />";
}

function save_featured_meta($post_id){
    // Do validation here for post_type, nonces, autosave, etc...
    if (isset($_REQUEST['_featured-post']))
        update_post_meta(esc_attr($post_id, '_featured-post', esc_attr($_REQUEST['_featured-post']))); 
        // I like using _ before my custom fields, so they are only editable within my form rather than the normal custom fields UI
}
add_action('save_post', 'save_featured_meta');

Then in your template file use the following for the main loop:

    $args = array(
        'meta_key' => '_featured-post', // include underscore prefix in key name
        'meta_value' => 1
    );
    // The number of posts displayed would be determined under Settings->Reading
    query_posts($args);

    if(have_posts()): while(have_posts()): the_post();
        // Do your bidding here

    endwhile; else:

    endif;

FOR EXAMPLE PURPOSES ONLY: For custom loops (If running multiple loops on one page), you would use this:

    $args = array(
        'posts_per_page' => 5,
        'meta_key' => '_featured-post',
        'meta_value' => 1
    );

    $featured = new WP_Query($args);

    if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post();
        the_title();
        the_content();
    endwhile; else:

    endif;

I tried to run this code and it helps me a lot for displaying my featured post on my homepage for my case study custom post type. Thanks a lot.

         <?php $args = array( 
                'post_type' => 'case_studies',
                'posts_per_page' => 1,
                'meta_key' => 'featured_post',
                'meta_value' => 1
            );

            $featured = new WP_Query($args);

            if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
                <div class="c-image"><?php the_post_thumbnail('large'); ?></div>
                <div class="cstext">
                     <article>
                        <h2><?php the_title(); ?></h2>  
                        <span class="sub-title"> USAF SOCONS </span>
                        <?php the_content(); ?>
                        <a href="<?php echo get_permalink(); ?>" class="readmore">Learn More</a>
                    </article> 

                    <a href="/case-studies" class="btn-primary">View all Case Studies</a>

                </div> 

            <?php 
                endwhile; 
                    else: 'No Content Added';
            endif; ?>
    </div>
</div>  

Post a comment

comment list (0)

  1. No comments so far