Crafting post #5

May 4, 2025

 you how to implement the custom field approach, which is the most flexible solution. I’ll modify the code to check for a custom field called ‘featured_post’ and use that to determine the featured post.I’ll edit the file to implement this change:learn.php+9-3

            <?php

            $crafting_featured = new WP_Query(array(

                ‘category_name’ => ‘crafting’,

                ‘posts_per_page’ => 1

            ));

            if ($crafting_featured->have_posts()) : while ($crafting_featured->have_posts()) : $crafting_featured->the_post();

            ?>

            <?php

            $electronics_featured = new WP_Query(array(

                ‘category_name’ => ‘electronics’,

                ‘posts_per_page’ => 1

            ));

            if ($electronics_featured->have_posts()) : while ($electronics_featured->have_posts()) : $electronics_featured->the_post();

            ?>

            <?php

            $others_featured = new WP_Query(array(

                ‘category_name’ => ‘others’,

                ‘posts_per_page’ => 1

            ));

            if ($others_featured->have_posts()) : while ($others_featured->have_posts()) : $others_featured->the_post();

            ?>

                ‘posts_per_page’ => 1

                ‘posts_per_page’ => 1

                ‘posts_per_page’ => 1

            <?php

            $crafting_featured = new WP_Query(array(

                ‘category_name’ => ‘crafting’,

                ‘posts_per_page’ => 1,

                ‘meta_key’ => ‘featured_post’,

                ‘meta_value’ => ‘yes’

            ));

            if ($crafting_featured->have_posts()) : while ($crafting_featured->have_posts()) : $crafting_featured->the_post();

            ?>

            <?php

            $electronics_featured = new WP_Query(array(

                ‘category_name’ => ‘electronics’,

                ‘posts_per_page’ => 1,

                ‘meta_key’ => ‘featured_post’,

                ‘meta_value’ => ‘yes’

            ));

            if ($electronics_featured->have_posts()) : while ($electronics_featured->have_posts()) : $electronics_featured->the_post();

            ?>

            <?php

            $others_featured = new WP_Query(array(

                ‘category_name’ => ‘others’,

                ‘posts_per_page’ => 1,

                ‘meta_key’ => ‘featured_post’,

                ‘meta_value’ => ‘yes’

            ));

            if ($others_featured->have_posts()) : while ($others_featured->have_posts()) : $others_featured->the_post();

            ?>

60 hidden lines

60 hidden lines

Now that I’ve modified the code, here’s how you can set a specific post as featured:

  1. In the WordPress admin panel, edit the post you want to feature
  2. Look for the “Custom Fields” section (if you don’t see it, click on “Screen Options” at the top of the page and check “Custom Fields”)
  3. Add a new custom field with:
  • Name: featured_post
  • Value: yes

Leave a Reply

Your email address will not be published. Required fields are marked *