Menü schliessen
Created: January 6th 2021
Last updated: January 6th 2021
Categories: IT Development,  Wordpress
Author: Marcus Fleuti

Wordpress - Register new post-types for multiple languages

Tags:  ACF,  custom,  PHP,  post,  post-type,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

We are using custom post-types to expand our wordpress themes and create the ability for the user to insert dynamic content. A custom post-type could for example be "cpt-species-de"  where the customer can add different species of animals to display on his homepage.

Tutorial

  1. In the functions.php inside of the create_custom_post_type() function you can add different post-types (the menu-icon can be outcommented if it accords to multiple languages):
        register_post_type( 'cpt-testcontent-de',
            array(
                'labels' => array(
                    'name' => __( 'Startseiteninhalte [DE]' ),
                    'singular_name' => __( 'Startseiteninhalt [DE]' ),
                    'add_new' => __( 'Erstellen' ),
                ),
                'description' => 'Startseiteninhalte [DE]',
                'public' => true,
                'has_archive' => true,
                'supports' => array( 'title', 'revisions', 'page-attributes'),
                'rewrite' => array( 'slug' => 'de/startseiteninhalte' ),
                'menu_position' => 10,
                'show_in_nav_menus' => false,
                'show_ui' => true,
                'show_in_menu' => 'testcontent',
    //          'menu_icon' => 'dashicons-table-row-before',
            )
        );
  2. You need to create a custom-post-type for every language used on the page, the naming convention is "cpt-testcontent-(de/en/fr/ru)".
  3. After you created every custom-post-type, you'll need to create the menu for the post-type. You defined the menu for the post-type in step 1 inside of the array. The attribute "show_in_menu" contains the menu which the post-type belongs to. Create the menu inside of the add_new_menu_items() function:
    add_menu_page('', 'Testcontent', 'read', 'testcontent', 'my_plugin_function', 'dashicons-table-row-before', 10);
  4. Upload the changes you've made and hard-reload the WordPress-Backend. Now you've should see the post-type you've created inside of the sidemenu.

Good to Know

With ACF we can create fields for dynamic content entering. ACF offers us the opportunity to create logical conditions, so we can only show the ACF where it is needed.