php 神圣的史诗列表页面,蝙蝠侠!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 神圣的史诗列表页面,蝙蝠侠!相关的知识,希望对你有一定的参考价值。
<?php
/**
* Template Name: Listing Page
*/
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
$context['title'] = $post->get_title();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$listing_args = array(
// 'posts_per_page' => -1,
'posts_per_page' => (get_field('posts_per_page')) ? (int)get_field('posts_per_page') : 12,
'post_type' => get_field('post_type'),
'paged' => $paged,
'orderby' => (get_field('post_type') == 'person') ? '' : (get_field('orderby')) ? get_field('orderby') : 'date',
'order' => (get_field('sort')) ? get_field('sort') : 'ASC',
'ignore_sticky_posts' => true,
'has_password' => FALSE,
);
// This stuff needs to happen before the query
if (get_field('post_type') == 'post') {
$tax_slug = 'category';
if (get_field('stories_switch') == 'true') {
// echo("we're looking for stories");
if (!empty($_GET['category'])) {
$queryCategory = $_GET['category'];
$tax_term = $queryCategory;
$tax_field = 'slug';
$context['current_category'] = get_category_by_slug($queryCategory);
}
} else {
// echo("we're looking for posts");
$tax_term = get_field('post_cat');
}
} elseif (get_field('post_type') == 'faq') {
// echo("we're looking for faqs");
$tax_slug = 'post_tag_inm';
$tax_term = get_field('faq_tag');
} elseif (get_field('post_type') == 'partner') {
// echo("we're looking for partners");
$tax_slug = 'partner_type';
$tax_term = get_field('partner_cat');
// } elseif (get_field('post_type') == 'person') {
// // echo("we're looking for people");
// $tax_slug = 'person_role';
// $tax_term =get_field('person_cat');
}
if (!empty($tax_term)) {
// echo("hey, the tax_term isn't empty!");
$listing_args['tax_query'] = [
[
'taxonomy' => $tax_slug,
'terms' => $tax_term,
'field' => !empty($tax_field) ? $tax_field : 'term_id'
]
];
}
if (get_field('post_type') != 'video' && get_field('post_type') != 'person') {
// Here's the QUERY!!!
$context['listing_items'] = Timber::get_posts($listing_args);
// And the pagination:
query_posts($listing_args);
$context['pagination'] = Timber::get_pagination();
}
if(get_field('post_type') == 'video' || get_field('post_type') == 'person') {
// This is a method for paginating ACF galleries from
// https://aiocollective.com/blog/pagination-with-gallery-field-advanced-custom-fields/
// I'm hoping I can make it work with ACF repeater fields and Timber
https://aiocollective.com/blog/pagination-with-gallery-field-advanced-custom-fields/
// Instead of a query, just get the repeater contents
if (get_field('post_type') == 'video') {
$list_of_items = get_field('videos'); // get all the items
} else {
$list_of_items = get_field('people'); // get all the items
}
//Setup pagination variables
$current_page_items = array(); // Set listing item array for current page
$items_per_page = (get_field('posts_per_page')) ? (int)get_field('posts_per_page') : 12; // How many items we should display on each page
$total_items = count($list_of_items); // How many items we have in total
$total_pages = ceil($total_items / $items_per_page); // How many pages we have in total
//Get current page
if ( get_query_var( 'paged' ) ) {
$current_page = get_query_var( 'paged' );
}elseif ( get_query_var( 'page' ) ) {
//this is just in case some odd rewrite, but paged should work instead of page here
$current_page = get_query_var( 'page' );
}else{
$current_page = 1;
}
$starting_point = (($current_page-1)*$items_per_page); // Get starting point for current page
// Get elements for current page
if($list_of_items){
$current_page_items = array_slice($list_of_items,$starting_point,$items_per_page);
}
if(!empty($current_page_items)){
//your gallery loop here
// FUCK THE LOOP, LET'S TRY TIMBER
$context['listing_items'] = $current_page_items;
}
// And our pagination
$big = 999999999; // need an unlikely integer
$page_links = paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => 'Previous',
'next_text' => 'Next',
'prev_next' => false,
'type' => 'array'
));
// In order for my pagination to look like Timber's pagination, I needed to have next and previous links on every page, (I'll set visibility: hidden when they're disabled but they still need to exist and take up space) which isn't possible with paginate_links(). So I followed some instructions at https://wordpress.stackexchange.com/questions/52638/pagination-how-do-i-always-show-previous and generated my next and previous links manually.
if (!empty($page_links)) {
if ($current_page == 1 ) {
$prev_link = '<span class="page-numbers prev disabled">Previous</span>';
} else {
$prev_link = '<a class="page-numbers prev" href="' . get_page_link() . 'page/' . ($current_page - 1) . '/">Previous</a>';
}
if ($current_page == $total_pages ) {
$next_link = '<span class="page-numbers next disabled">Next</span>';
} else {
$next_link = '<a class="page-numbers next" href="' . get_page_link() . 'page/' . ($current_page + 1) . '/">Next</a>';
}
array_unshift($page_links, $prev_link);
$page_links[] = $next_link;
$pagination = implode($page_links);
}
if(!empty($pagination)) {
$context['pagination'] = $pagination;
}
}
// Ok, now we need to translate the query data for our dumb templates. Some stuff is specific per post type...
if (get_field('post_type') != 'video' && get_field('post_type') != 'person') {
// Get all the category names and links for each item and format them for the twig template :)
foreach($context['listing_items'] as &$listing_item) {
// Do whatever processing is needed to display the item.
if(!empty($tax_slug)) {
$terms = get_the_terms( $listing_item->id, $tax_slug );
}
if(!empty($terms)) {
$listing_item->labels = [];
if (get_field('post_type') == 'post' && get_field('stories_switch') == 'true') {
foreach($terms as $term) {
$listing_item->labels[] = [
'label' => $term->name,
'link' => get_page_link() . '?category=' . $term->slug
];
}
} else {
foreach($terms as $term) {
$listing_item->labels[] = [
'label' => $term->name
];
}
}
}
if (!empty($listing_item->get_field('social_media_links'))) {
$listing_item->social_links = $listing_item->get_field('social_media_links');
}
$listing_item->teaser = $listing_item->preview();
if(get_field('post_type') == 'partner') {
$listing_item->image = wp_get_attachment_image( get_post_thumbnail_id($listing_item->id), 'logo');
} else {
$listing_item->image = wp_get_attachment_image( get_post_thumbnail_id($listing_item->id), 'squarish', null , [ "sizes" => "(min-width: 992px) 25vw, (min-width: 768px) 33vw, (min-width: 576px) 50vw, 100vw"]);
}
}
} elseif(get_field('post_type') == 'person') {
if(!empty($context['listing_items'])) {
foreach($context['listing_items'] as &$listing_item) {
// Do whatever processing is needed to display the item.
$listing_item = $listing_item['person'];
$listing_item->title = $listing_item->post_title;
$listing_item->content = apply_filters('the_content', get_post_field('post_content', $listing_item->ID));
if (!empty(get_field('person_title', $listing_item->ID))) {
$listing_item->labels = [];
$listing_item->labels[] = [
'label' => get_field('person_title', $listing_item->ID)
];
}
if (!empty(get_field('social_media_links', $listing_item->ID))) {
$listing_item->social_links = get_field('social_media_links', $listing_item->ID);
}
$listing_item->image = get_the_post_thumbnail($listing_item->ID, 'squarish', [ "sizes" => "(min-width: 992px) 25vw, (min-width: 768px) 33vw, (min-width: 576px) 50vw, 100vw"]);
}
}
} else {
foreach ($context['listing_items'] as &$listing_item) {
$listing_item['post_type'] = 'video';
}
}
// We HAVE to unset the reference from our foreach loop or it will overwrite the last item in the results with the 2nd to last item!
// http://schlueters.de/blog/archives/141-References-and-foreach.html
unset($listing_item);
// the story page gets post categories in the place of the local menu
if (get_field('post_type') == 'post' && get_field('stories_switch') == 'true') {
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '1'
) );
$local_menu = [[
'title' => 'All',
'link' => get_page_link()
]];
foreach($categories as $category) {
$local_menu[] = [
'title' => $category -> name,
'link' => get_page_link() . '?category=' . $category -> slug
];
}
} else {
// other pages get to choose a menu
if($post->get_field('local_menu')) {
$local_menu_items = new TimberMenu($post->get_field('local_menu'));
$local_menu = [];
foreach($local_menu_items->items as $local_menu_item) {
$local_menu[] = [
'title' => $local_menu_item->name,
'link' => $local_menu_item->url
];
}
}
}
if (!empty($local_menu)) {
$context['local_menu'] = $local_menu;
}
$context['listing_page_description'] = $post->get_field('listing_page_description');
$context['listing_page_post_type'] = (get_field('post_type')) ? get_field('post_type') : 'post';
Timber::render( array( 'page/page-listing.twig'), $context );
以上是关于php 神圣的史诗列表页面,蝙蝠侠!的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView 是 “何方神圣“?为什么选择它呢?