php Acf Pro&Sage 9 - 灵活的内容块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Acf Pro&Sage 9 - 灵活的内容块相关的知识,希望对你有一定的参考价值。
<?php
namespace App;
use Sober\Controller\Controller;
class App extends Controller
{
public function siteName()
{
return get_bloginfo('name');
}
public static function title()
{
if (is_home()) {
if ($home = get_option('page_for_posts', true)) {
return get_the_title($home);
}
return __('Latest Posts', 'sage');
}
if (is_archive()) {
return get_the_archive_title();
}
if (is_search()) {
return sprintf(__('Search Results for %s', 'sage'), get_search_query());
}
if (is_404()) {
return __('Not Found', 'sage');
}
return get_the_title();
}
/**
* get a list of content blocks for the page add blocks and data to a data object
* object will then be looped through with data passed to each blade using a variable
* e.g. @include('view.name', ['some' => 'data']) see https://laravel.com/docs/5.6/blade#including-sub-views
*/
public function flexible_content_blocks()
{
$data = array();
$page_id = get_the_ID();
$block_count = 0;
if (have_rows('flexible_content', $page_id) && !post_password_required()) {
while (have_rows('flexible_content', $page_id)){
the_row();
$content_block = (object) array();
$gid = '';
if (get_row_layout() == 'predefined_block') {
/* predefined content block */
$global_block = get_sub_field('global_content_block');
$content_block->layout = get_field('block_layout', $global_block->ID);
$gid = $global_block->ID;
$content_block->slug = 'block-' . $global_block->post_name;
$content_block->title = $global_block->post_title;
$content_block->source = 'global';
} else {
$row = get_row();
$keys = array_keys($row);
$content_block->layout = get_row_layout();
$content_block->slug = strtolower(get_sub_field('block_slug'));
$content_block->title = ''; // has no title by default
$content_block->source = 'page';
if (empty($block_slug)) {
$content_block->slug = 'block-' . substr(md5($page_id . $content_block->layout . $block_count), 0, 8);
}
}
// defined at flexible content level
$block_background_colour = get_sub_field('background_colour');
$content_block->background_colour = (!empty($block_background_colour) ? ' bg-' . $block_background_colour : ' bg-transparent');
/* main : Main column width
** full-indent : Full container width
** full : Full browser width
* full is main container width, main has container class and bootstrap width & offset, full indent is container
*/
$block_width = get_sub_field('block_width');
$content_block->block_width = $block_width;
switch ($block_width) {
case 'main':
$block_width = (object) array('container_class' => 'container', 'subcontainer_class' => true);
break;
case 'full':
$block_width = (object) array('container_class' => '', 'subcontainer_class' => false);
break;
/* full-indent & default are same */
case 'full-indent':
$block_width = (object) array('container_class' => 'container', 'subcontainer_class' => false);
default:
}
$content_block->width = $block_width;
$content_block->class = 'fcb-'.$content_block->layout;
$special_case = '';
// special case based on global block slug
if(get_row_layout() == 'predefined_block'){
switch ($global_block->post_name) {
case 'example-switch-slug':
$special_case = 1;
break;
}
}
/**
* general switch - there should be one case for each layout style in flexible_content_blocks
* if a predefined block has a corresponding flexible block it should also appear in this switch
*/
if (empty($special_case)) {
switch ($content_block->layout) {
/**
* predefined OR page blocks - we must test source and use get_sub_field OR get_field accordingly
*/
case 'text_block':
$content_block->content = ($content_block->source == 'page'?get_sub_field('block_text'):get_field('block_text'));
break;
case 'image_block':
$content_block->text_layout = ($content_block->source == 'page'?get_sub_field('text_layout'):get_field('text_layout', $global_block->ID));
$content_block->content = ($content_block->source == 'page'?get_sub_field('block_text'):get_field('block_text'));
$content_block->image = ($content_block->source == 'page'?get_sub_field('image_image'):get_field('image_image'));
$content_block->block_links = ($content_block->source == 'page'?get_sub_field('block_links'):get_field('block_links'));
$content_block->image_position = ($content_block->source == 'page'?get_sub_field('image_position'):get_field('image_position'));
break;
/**
* page level blocks only - we can just use get_sub_field
*/
case 'clients_block':
$content_block->content = get_sub_field('block_text');
/* clients block requires to show 5 clients so x 5 */
$client_list = get_sub_field('client_list');
$arr_logo_clients = array();
foreach($client_list as $client){
if(get_the_post_thumbnail_url($client->ID)){
array_push($arr_logo_clients, $client);
}
}
$content_block->posts = array();
for($i=0; $i<5; $i++){
foreach($arr_logo_clients as $logo_client){
array_push($content_block->posts, $logo_client);
}
}
break;
case 'services_block':
$content_block->title = get_sub_field('services_title');
$content_block->services = get_sub_field('services');
$content_block->business_category = get_sub_field('business_category');
break;
case 'giant_tab_block':
$content_block->tabs = get_sub_field('tabs');
break;
case 'multi_column_text_block':
$content_block->column_title = get_sub_field('column_title');
$content_block->columns = get_sub_field('columns');
break;
case 'feature_block':
$content_block->text_layout = get_sub_field('text_layout');
$content_block->content = get_sub_field('block_text');
$content_block->ribbon_colour = get_sub_field('feature_ribbon_colour');
break;
case 'tab_block':
$content_block->text_layout = get_sub_field('text_layout');
$content_block->content = get_sub_field('block_text');
$content_block->tabs = get_sub_field('tabs');
$content_block->business_category = get_sub_field('business_category');
$content_block->block_links = get_sub_field('block_links');
break;
/**
* predefined blocks only - we can just use get_field
*/
case 'news_list':
$post_data = get_field('post_data', $gid); // ACF group
if( $post_data ){
$post_count = (!empty($post_data['post_count'])?$post_data['post_count']:get_option('posts_per_page'));
$post_orderby = $post_data['post_orderby'];
$post_order = $post_data['post_order'];
}
$orderby = array($post_orderby => $post_order);
$args = array(
'posts_per_page' => (int)$post_count,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => $orderby,
);
$content_block->posts = get_posts( $args );
break;
case 'services_list':
$post_data = get_field('post_data', $gid); // ACF group
$post_parent_id = 7;
if( $post_data ){
$post_count = (!empty($post_data['post_count'])?$post_data['post_count']:get_option('posts_per_page'));
$post_orderby = $post_data['post_orderby'];
$post_order = $post_data['post_order'];
}
$orderby = array('post_parent' => 'DESC', $post_orderby => $post_order);
$args = array(
'posts_per_page' => (int)$post_count,
'post_type' => 'page',
'post_parent' => $post_parent_id,
'post_status' => 'publish',
'orderby' => $orderby,
);
$child_pages = get_posts( $args );
$page_ids = wp_list_pluck( $child_pages, 'ID' );
// add parent page
array_push($page_ids,$post_parent_id);
$args = array(
'posts_per_page' => (int)$post_count,
'post_type' => 'page',
'post__in' => $page_ids,
'post_status' => 'publish',
'orderby' => $orderby,
);
$content_block->posts = get_posts( $args );
$content_block->title = get_field('block_title', $gid); // ACF group
break;
case 'contact_form':
$content_block->content = get_field('block_text', $global_block->ID);
break;
}
}
array_push($data, $content_block);
$block_count++;
}
}
return $data;
}
}
@foreach($flexible_content_blocks as $block_data)
@switch($block_data->layout)
{{-- list in alphabetical order --}}
{{-- pass each partial it's own content block data from app.php --}}
@case('clients_block')
@include('partials.clients_block', ['content_block' => $block_data])
@break
@case('contact_form')
@include('partials.contact_form', ['content_block' => $block_data])
@break
@case('feature_block')
@include('partials.feature_block', ['content_block' => $block_data])
@break
@case('giant_tab_block')
@include('partials.giant_tab_block', ['content_block' => $block_data])
@break
@case('multi_column_text_block')
@include('partials.multi_column_text_block', ['content_block' => $block_data])
@break
@case('image_block')
@include('partials.image_block', ['content_block' => $block_data])
@break
@case('news_list')
@include('partials.news_list', ['content_block' => $block_data])
@break
@case('search_bar')
@include('partials.search_bar', ['content_block' => $block_data])
@break
@case('services_block')
@include('partials.services_block', ['content_block' => $block_data])
@break
@case('services_list')
@include('partials.services_list', ['content_block' => $block_data])
@break
@case('tab_block')
@include('partials.tab_block', ['content_block' => $block_data])
@break
@case('text_block')
@include('partials.text_block', ['content_block' => $block_data])
@break
{{-- default --}}
@default
@include('partials.text_block', ['content_block' => $block_data])
@endswitch
@endforeach
<div class="{{ $content_block->class }} {{ $content_block->background_colour }}" id="{{ $content_block->slug }}">
<div class="{{ $content_block->width->container_class }} py-5">
{{-- if subcontainer start offset wrap --}}
@if($content_block->width->subcontainer_class)
<div class="row">
<div class="col-lg-8 offset-lg-2">
@endif
{{-- start content --}}
{!! $content_block->content !!}
{{-- end content --}}
{{-- if subcontainer end offset wrap --}}
@if($content_block->width->subcontainer_class)
</div>
</div>
@endif
</div>
</div>
以上是关于php Acf Pro&Sage 9 - 灵活的内容块的主要内容,如果未能解决你的问题,请参考以下文章