$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How show categories in admin and get that selected to show posts in index|Programmer puzzle solving
最新消息: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)

How show categories in admin and get that selected to show posts in index

matteradmin6PV0评论

my problem is: I need to offer in admin a page to user select 2 categories. I have 2 containers in index.php showing some posts from some category, but i need to allow user to change what category he want to show posts in those containers. So, how can i make this work?

my problem is: I need to offer in admin a page to user select 2 categories. I have 2 containers in index.php showing some posts from some category, but i need to allow user to change what category he want to show posts in those containers. So, how can i make this work?

Share Improve this question asked Jan 30, 2019 at 10:53 Matheus RibeiroMatheus Ribeiro 1
Add a comment  | 

1 Answer 1

Reset to default 2

You can use Advanced Custom Fields to add two fields where the admin can select a category. On your index page, you then check the two fields using get_field for which category you want to display.

The field can return a WP taxonomy object, or just the category ID (which I have used). In your index, you can then get the posts:

$category_id_1 = get_field('category_1'));
$category_id_2 = get_field('category_2'));

$container_1_posts = get_posts(array(
   'posts_per_page'   => 5,
   'cat'              => $category_id_1,
   'orderby'          => 'date',
   'order'            => 'DESC',
   'post_type'        => 'post',
   'post_status'      => 'publish',
)); 

$container_2_posts = get_posts(array(
   'posts_per_page'   => 5,
   'cat'              => $category_id_2,
   'orderby'          => 'date',
   'order'            => 'DESC',
   'post_type'        => 'post',
   'post_status'      => 'publish',
)); 
Post a comment

comment list (0)

  1. No comments so far