From c4b2eb526c5733cc32b0550eb76fd072531e223c Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 17 Feb 2019 17:20:43 +0100 Subject: Creates custom post type and custom taxonomy and renders them correctly. --- angebote/angebote.php | 2 - angebote/inc/functions.php | 82 ++++++++++++++++++++++ angebote/inc/template.php | 107 +++++++++++++++++++++++++++++ functions.php | 16 +++-- loop-templates/content-angebote.php | 51 ++++++++++++++ loop-templates/content-angebotenotitle.php | 15 ++++ sass/theme/_offer.scss | 14 ++++ search.php | 6 +- single-angebote.php | 53 ++++++++++++++ taxonomy-type.php | 88 ++++++++++++++++++++++++ 10 files changed, 427 insertions(+), 7 deletions(-) create mode 100644 angebote/inc/template.php create mode 100644 loop-templates/content-angebote.php create mode 100644 loop-templates/content-angebotenotitle.php create mode 100644 single-angebote.php create mode 100644 taxonomy-type.php diff --git a/angebote/angebote.php b/angebote/angebote.php index 5409b55..06998f3 100644 --- a/angebote/angebote.php +++ b/angebote/angebote.php @@ -7,8 +7,6 @@ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } -require_once __DIR__ . '/inc/functions.php'; - get_header(); $container = get_theme_mod( 'understrap_container_type' ); diff --git a/angebote/inc/functions.php b/angebote/inc/functions.php index de9a03d..42dab03 100644 --- a/angebote/inc/functions.php +++ b/angebote/inc/functions.php @@ -41,3 +41,85 @@ function create_offer_link() { return "https://angebote.fuselkoenig.de/wp/whisky?page=" . $page . "&_sort=" . $sort . "&_order=" . $order; } + +function create_offer_post_type() { + register_post_type( + 'angebote', + array( + 'labels' => array( + 'name' => __('Angebote'), + 'singular_name' => __('Angebot') + ), + 'public' => true, + 'has_archive' => true, + 'hierarchical' => false, + 'taxonomies' => array( 'type' ), + 'supports' => array( + 'title', + 'editor', + 'thumbnail', + 'comments' + ), + 'rewrite' => array( + #'slug' => 'aangebote/%custom-taxonomy-name%', + 'slug' => 'angebote/%type%', + 'with_front' => true, + ) + ) + ); + // create a new taxonomy + register_taxonomy( + 'type', + 'angebote', + array( + 'label' => __( 'Type' ), + 'hierarchical' => false, + 'rewrite' => array( + 'slug' => 'angebote', + 'with_front' => true + ) + ) + ); +} + +function generate_taxonomy_rewrite_rules( $wp_rewrite ) { + $rules = array(); + $post_types = get_post_types( array( 'name' => 'angebote', 'public' => true, '_builtin' => false ), 'objects' ); + $taxonomies = get_taxonomies( array( 'name' => 'type', 'public' => true, '_builtin' => false ), 'objects' ); + + foreach ( $post_types as $post_type ) { + $post_type_name = $post_type->name; // 'developer' + $post_type_slug = $post_type->rewrite['slug']; // 'developers' + + foreach ( $taxonomies as $taxonomy ) { + if ( $taxonomy->object_type[0] == $post_type_name ) { + $terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) ); + foreach ( $terms as $term ) { + $rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug; + } + } + } + } + $wp_rewrite->rules = $rules + $wp_rewrite->rules; +} + +function post_link_includes_taxonomy( $post_link, $id = 0 ){ + $post = get_post($id); + if ( is_object( $post ) ){ + $terms = wp_get_object_terms( $post->ID, 'type' ); + file_put_contents("/tmp/wp.debug", json_encode($terms)); + if( $terms ){ + # file_put_contents("/tmp/wp.debug", str_replace( 'angebote/' , 'angebote/' . $terms[0]->slug , $post_link )); + # return $post_link; + # return str_replace( '/angebote/' , '/angebote/' . $terms[0]->slug . '/', $post_link ); + return str_replace( '%type%' , $terms[0]->slug, $post_link ); + } else { + return str_replace( '%type%' , '', $post_link ); + } + } + return $post_link; +} + +add_action('init', 'create_offer_post_type'); +add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules'); +add_filter( 'post_type_link', 'post_link_includes_taxonomy', 1, 3 ); diff --git a/angebote/inc/template.php b/angebote/inc/template.php new file mode 100644 index 0000000..d0bbcc3 --- /dev/null +++ b/angebote/inc/template.php @@ -0,0 +1,107 @@ +name; + } + + $include_post_link = false; + if ( is_archive() || is_search() ) { + $include_post_link = true; + } + if ( is_search() ) { + $css_border_left = " card-border-left"; + } else { + $css_border_left = ""; + } + + $offer = shortcode_atts( array( + 'spirit_type' => $spirit_type, + 'shop' => '', + 'procent' => '', + 'img_url' => '', + 'name' => '', + 'discounted_price' => '', + 'original_price' => '', + 'shipping_costs' => 'Versand ist nie kostenlos', + 'abv' => '', + 'volume' => '', + 'base_price' => '', + 'url' => '', + 'created_at' => '', + 'include_post_link' => $include_post_link, + ), $atts + ); +?> +
+
+
+
+

+ + + +

+
+
+

%

+
+ +
+
+
+

+ +

+
+
+ +

+launch +

+ +

+ +
+

+ Preis: € +
+ Alter Preis: € +
+ Versand: € +

+

+ Alkohol: % +
+ Volumen: Liter +
+ € / Liter +

+
+
+ +
+ +
+
+ + +
id="post-"> + +
+ + ', '' ); ?> + + ID, 'large', array( 'class' => 'mx-auto d-block border rounded' ) ); ?> + + +
+ +
+ +
+ +
+ +
+
+ +
+ + + + '', + ) ); + ?> + +
+ + +
diff --git a/loop-templates/content-angebotenotitle.php b/loop-templates/content-angebotenotitle.php new file mode 100644 index 0000000..11f8b0e --- /dev/null +++ b/loop-templates/content-angebotenotitle.php @@ -0,0 +1,15 @@ + + +
id="post-"> + +
diff --git a/sass/theme/_offer.scss b/sass/theme/_offer.scss index 14a9510..6f94d2d 100644 --- a/sass/theme/_offer.scss +++ b/sass/theme/_offer.scss @@ -73,3 +73,17 @@ .page-link { font-size: 18px; } + +.offer-title { + color: rgba(0, 0, 0, 0.87); + text-decoration: underline; +} + +.icon-small { + font-size: inherit !important; +} + +/*.offer-page .card {*/ +.card-border-left { + border-left: 5px solid theme-color(primary); +} diff --git a/search.php b/search.php index 2c59501..1495828 100644 --- a/search.php +++ b/search.php @@ -51,7 +51,11 @@ $container = get_theme_mod( 'understrap_container_type' ); * If you want to overload this in a child theme then include a file * called content-search.php and that will be used instead. */ - get_template_part( 'loop-templates/content', 'search' ); + if ( "angebote" == get_post_type() ) { + get_template_part( 'loop-templates/content', 'angebotenotitle' ); + } else { + get_template_part( 'loop-templates/content', 'search' ); + } ?> diff --git a/single-angebote.php b/single-angebote.php new file mode 100644 index 0000000..1182b5d --- /dev/null +++ b/single-angebote.php @@ -0,0 +1,53 @@ + + +
+ +
+ +
+ + + + +
+ + + + + + + + + + + +
+ + + + +
+ +
+ +
+ + diff --git a/taxonomy-type.php b/taxonomy-type.php new file mode 100644 index 0000000..0d83afd --- /dev/null +++ b/taxonomy-type.php @@ -0,0 +1,88 @@ + + + + +
+ +
+ +
+ + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+ +
+ +
+ + -- cgit v1.2.3