How Do I Create a List View in WordPress?
Creating a list view in WordPress is a fairly simple process. First, create a new template file, name it list.
php, and place it in your WordPress themes directory. Inside list.php, add the following code:.
ID; } // create the list view $list = new WP_List_View(); $list->set_title(‘Posts’); $list->set_items( $items ); $list->set_per_page(5); $list->set_sort_column(‘date’); $list->set_filter_column(‘category’); $list->set_filter_order(‘date’, ‘date_order’); $list->set_show_date(); $list->set_show_time(); // add the list view to the page $page = new WP_Page(); $page->add_action( ‘admin_menu’, ‘add_list_view’); $page->add_action( ‘wp_print_styles’, ‘print_list_view’); $page->add_action( ‘wp_enqueue_scripts’, ‘enqueue_list_view’); $page->set_title(‘Posts’); $page->set_header_image( ‘images/header-list.jpg’, array( ‘alt’ => ‘Posts List View’, ) ); $page->add_subpage_header( ‘Posts’, ‘List View’, array( ‘class’ => ‘sub-header’, ‘title’ => __(‘Posts List View’), ‘description’ => __(‘This is a list of posts.’), ‘keywords’ => ‘posts, lists, list view, blog post’, ) ); $page->add_image( ‘images/list-view-icon.
png’, ‘list-view’); $page->set_content_width( ‘600px’); $page->set_content_height( ‘400px’); $page->set_title(‘Posts List View’); $page->add_subpage_header( ‘Posts’, ‘List View’, array( ‘class’ => ‘sub-header’, ‘title’ => __(‘Posts List View’), ‘description’ => __(‘This is a list of posts.png’, ‘list-view’); $page->add_action( ‘admin_menu’, ‘add_list_view’); $page->add_action( ‘wp_print_styles’, ‘print_list_view’); $page->add_action( ‘wp_enqueue_scripts’, ‘enqueue_list_view’); ?>.
In this code, we first require the WP_List_View plugin. Next, we create an empty array and loop through all the posts in the current blog post format.
For each post, we add its ID to the $items array. Finally, we create the list view object and set its various properties.
We then add the list view to the page. We set its title, items, per_page, sort_column, and filter_column properties.
We also set the show_date and show_time properties to true. Finally, we add some subpage headers and images.
Now that the list view is created, we need to add some actions to the admin menu and manage its settings. We do this by adding the following code to the admin_menu action:
add_list_view
This action will add the list view to the admin menu. We then add the following code to the wp_print_styles action:
print_list_view
This will print the list view to the screen. Finally, we add the following code to the wp_enqueue_scripts action:
enqueue_list_view
This will enqueue the list view plugin into WordPress.