{"id":3972,"date":"2020-06-10T16:24:00","date_gmt":"2020-06-10T14:24:00","guid":{"rendered":"https:\/\/plural.software\/?p=3972"},"modified":"2020-06-10T17:28:06","modified_gmt":"2020-06-10T15:28:06","slug":"custom-post-type-in-wordpress","status":"publish","type":"post","link":"https:\/\/plural.software\/en\/custom-post-type-in-wordpress\/","title":{"rendered":"Custom Post Type in WordPress"},"content":{"rendered":"\n<p>Einer von vielen Vorz\u00fcgen einer WordPress Webseite besteht darin, dass sich die Webseite beliebig mit Inhalt erweitern l\u00e4sst. Eine der besten Funktionalit\u00e4ten ist auf jeden Fall die Erweiterung via sogenanntem Custom Post Type. <\/p>\n\n\n\n<h2>Neuer Custom Post Type registrieren<\/h2>\n\n\n\n<p>Ein Custom Post Type erweitert eine WordPress Webseite um ein weiteres Gef\u00e4ss um neue Beitr\u00e4ge darin zu erfassen. Beispielsweise kann man damit Referenzen, B\u00fccher oder Filme sammeln um diese dann zentralisiert auf der Webseite ausgeben. In diesem Beispiel wird die Seite um ein FAQ Gef\u00e4ss erweitert.<\/p>\n\n\n\n<p>Um den neuen Custom Post Type zu registrieren wird folgender Code in die functions.php des aktiven Themes eingef\u00fcgt:<\/p>\n\n\n\n<pre><code class=\"php\">\/*\n* Creating a function to create our CPT\n*\/\nfunction faq_custom_post_type() {\n \n    \/\/ Set UI labels for Custom Post Type\n    $labels = array(\n        'name' => _x( 'FAQ', 'FAQ', 'twentytwenty' ),\n        'singular_name' => _x( 'FAQ', 'FAQ', 'twentytwenty' ),\n        'menu_name' => __( 'FAQ', 'twentytwenty' ),\n        'all_items' => __( 'Alle FAQs', 'twentytwenty' ),\n        'view_item' => __( 'FAQ anzeigen', 'twentytwenty' ),\n        'add_new_item' => __( 'Neues FAQ', 'twentytwenty' ),\n        'add_new' => __( 'Neues FAQ', 'twentytwenty' ),\n        'edit_item' => __( 'FAQ bearbeiten', 'twentytwenty' ),\n        'update_item' => __( 'FAQ aktualisieren', 'twentytwenty' ),\n        'search_items' => __( 'Suche FAQ', 'twentytwenty' ),\n        'not_found' => __( 'Nicht gefunden', 'twentytwenty' ),\n        'not_found_in_trash' => __( 'Nicht gefunden im Papierkorb', 'twentytwenty' )\n    );\n     \n    \/\/ Set other options for Custom Post Type\n    $args = array(\n        'label' => __( 'faqs', 'twentytwenty' ),\n        'description' => __( 'FAQs', 'twentytwenty' ),\n        'labels' => $labels,\n        \/\/ Features this CPT supports in Post Editor\n        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),\n        \/\/ You can associate this CPT with a taxonomy or custom taxonomy. \n        'taxonomies' => array( 'category' ),\n        \/\/ A hierarchical CPT is like Pages and can have parent and child items. A non-hierarchical CPT is like Posts.\n        'hierarchical' => false,\n        'public' => true,\n        'show_ui' => true,\n        'show_in_menu' => true,\n        'show_in_nav_menus' => true,\n        'show_in_admin_bar' => true,\n        'menu_position' => 5,\n        'can_export' => true,\n        'has_archive' => true,\n        'exclude_from_search' => false,\n        'publicly_queryable' => true,\n        'capability_type' => 'post',\n        'show_in_rest' => true,\n    );\n     \n    \/\/ Registering your Custom Post Type\n    register_post_type( 'faq', $args );\n}\n \n\/* Hook into the 'init' action so that the function\n* Containing our post type registration is not \n* unnecessarily executed. \n*\/\nadd_action( 'init', 'faq_custom_post_type', 0 );<\/code><\/pre>\n\n\n\n<p>Nach Einf\u00fcgen des Codes k\u00f6nnen s\u00e4mtliche Parameter im Code angepasst werden um beispielsweise Name, Taxanomy oder Anzeigeicon im Backend zu ver\u00e4ndern. Nun k\u00f6nnen im WordPress Backend neue Beitr\u00e4ge erfasst werden, welche unter dem neuen &#8216;post_type&#8217; abgespeichert werden.<\/p>\n\n\n\n<h2>Beitr\u00e4ge des neuen CPT ausgeben<\/h2>\n\n\n\n<p>Um die angelegten Beitr\u00e4ge des neu erstellten Custom Post Type ausgeben zu k\u00f6nnen wird eine Abfrage an die Datenbank gestellt, welche nur die Beitr\u00e4ge mit dem passenden Post Type einliest.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><pre><code class=\"php-template\">&lt;?php $args = array( 'post_type' => 'faq', 'posts_per_page' => 10 );\n$the_query = new WP_Query( $args ); \n\/\/ Fetch 10 posts with post_type faq\nif ( $the_query-&gt;have_posts() ) : while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post(); ?&gt;\n    \/\/ Output all 10 posts with whileloop\n    &lt;h2>&lt;?php the_title(); ?>&lt;\/h2&gt;\n    &lt;div class=\"entry-content\"&gt;\n        &lt;?php the_content(); ?&gt; \n    &lt;\/div&gt;\n&lt;?php wp_reset_postdata(); ?&gt;\n&lt;?php else: ?&gt;\n    &lt;p&gt;&lt;?php _e( 'Sorry, no posts matched your criteria.' ); ?&gt;&lt;\/p&gt;<\/code><\/pre><\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Einer von vielen Vorz\u00fcgen einer Wordpress Webseite; Custom Post Types.<\/p>\n","protected":false},"author":1,"featured_media":2892,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[40],"tags":[59,60,62,61,64,63,50],"_links":{"self":[{"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/posts\/3972"}],"collection":[{"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/comments?post=3972"}],"version-history":[{"count":32,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/posts\/3972\/revisions"}],"predecessor-version":[{"id":4008,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/posts\/3972\/revisions\/4008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/media\/2892"}],"wp:attachment":[{"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/media?parent=3972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/categories?post=3972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plural.software\/en\/wp-json\/wp\/v2\/tags?post=3972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}