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?
1 Answer
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',
));