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

admin2025-06-03  2

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',
)); 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748962270a315190.html

最新回复(0)