Skip to content

Instantly share code, notes, and snippets.

@quimo
Last active July 13, 2017 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quimo/f53c92bf3d986465009f51ed624b89cc to your computer and use it in GitHub Desktop.
Save quimo/f53c92bf3d986465009f51ed624b89cc to your computer and use it in GitHub Desktop.
Compound | Simple WordPress template chunks manager
<?php
/**
* Plugin Name: Compound
* Plugin URI:
* Description: Simple WordPress template chunks manager
* Version: 1.0
* Author: Simone Alati
* Author URI: http://www.simonealati.it
*/
/*
██████╗ ██████╗ ███╗ ███╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗██████╗
██╔════╝██╔═══██╗████╗ ████║██╔══██╗██╔═══██╗██║ ██║████╗ ██║██╔══██╗
██║ ██║ ██║██╔████╔██║██████╔╝██║ ██║██║ ██║██╔██╗ ██║██║ ██║
██║ ██║ ██║██║╚██╔╝██║██╔═══╝ ██║ ██║██║ ██║██║╚██╗██║██║ ██║
╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚██████╔╝╚██████╔╝██║ ╚████║██████╔╝
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═════╝
ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
ping_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mime_type
comment_count
filter
post_thumbnail_full *
*/
class Compound {
public static function render() {
global $post;
$args = array(
'sort_order' => 'asc',
'sort_column' => 'menu_order',
'hierarchical' => 0,
'parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'private'
);
$pages = get_pages($args);
for ($i = 0; $i < count($pages); $i++) {
//aggiungo l'immagine in evidenza
$pages[$i]->post_thumbnail_full = self::addFeaturedImage($pages[$i]->ID);
//recupero il nome del template
$template_name = get_page_template_slug($pages[$i]->ID);
if ($template_name) {
$template = file_get_contents(get_stylesheet_directory_uri().'/'.$template_name);
//percorro i campi estratti da get_pages
foreach($pages[$i] as $key => $value) {
$template = str_replace("[+".$key."+]",$value,$template);
}
//verifico se è presente il plugin ACF e se si estraggo i campi
if (function_exists('get_fields')) {
$fields = get_fields($pages[$i]->ID);
foreach ($fields as $name => $value) {
if ($value) $template = str_replace("[+".$name."+]",$value,$template);
}
}
echo $template;
}
}
}
public static function renderPart($slug) {
$args = array(
'name' => $slug,
'post_type' => 'page',
'post_status' => 'private'
);
$page = get_posts($args);
$page[0]->post_thumbnail_full = self::addFeaturedImage($page[0]->ID);
$template_name = get_page_template_slug($page[0]->ID);
if ($template_name) {
$template = file_get_contents(get_stylesheet_directory_uri().'/'.$template_name);
//percorro i campi estratti da get_pages
foreach($page[0] as $key => $value) {
$template = str_replace("[+".$key."+]",$value,$template);
}
echo $template;
}
}
private static function addFeaturedImage($id) {
if (function_exists('has_post_thumbnail') && has_post_thumbnail($id)) {
$dummy = wp_get_attachment_image_src(get_post_thumbnail_id($id),'full');
return $dummy[0];
}
return 0;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<?php wp_head() ?>
</head>
<body>
<main>
<?php
/* recupera tutte le pagine figlie della pagina corrente in ordine menu_order */
Compound::render();
?>
</main>
<footer>
<?php wp_footer() ?>
</footer>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<?php wp_head() ?>
</head>
<body>
<main>
<?php
/* recupera la pagina con slug 'sezione1' */
Compound::renderPart('sezione1');
Compound::renderPart('sezione2');
?>
</main>
<aside>
<?php
Compound::renderPart('barra-laterale');
?>
</aside>
<footer>
<?php wp_footer() ?>
</footer>
</body>
</html>
<?php
/* Template Name: sezione1 */
?>
<div class="servizio">
<div class="servizio__descrizione">
<header>
<h3>[+post_title+]</h3>
</header>
[+post_content+]
</div>
<div class="servizio__immagine" style="background: url('[+post_thumbnail_full+]') top left no-repeat; background-size: cover;"></div>
</div>
@quimo
Copy link
Author

quimo commented Dec 13, 2016

INSTALLAZIONE

  1. salvare il file compound.php in plugins/compound/
  2. creare un template di WordPress che usi Compound::renderPart o Compound::render (in questo esempio front-page.php e index.php)
  3. creare una pagina che usi questo template
  4. creare un template di WordPress per ogni sezione di template chiamata con Compound::renderPart (in questo esempio sezione1.php)
  5. creare una pagina private per ogni sezione di template del punto (4) che abbia come genitore la pagina al punto (3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment