• Resolved morris373

    (@morris373)


    Hi
    I would like to add a discription to my pages that are in a different language so I know what each pages are.

    WordPress already does it to the Home page and Blog page when you set them up in Settings => Reading.

    I am setting up a bilingual website and it’s difficult to know what each page is without translating them all the time.

    Thank you

    Morris

    • This topic was modified 4 years, 3 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter morris373

    (@morris373)

    Hi
    Just looking at the code in Polylang and it refers to those pages as Post States.

    So Setting a page called Home as the Front page, in All Pages it shows:

    Home – Front Page and Blog – Posts page

    So how can I use the same Post State to add similar to the Contact page for example but in a different language so when I view all the pages I will be able to identify the Contact’s page in a different language with the english version next to it?

    Woocommerce does the same to there pages for like Cart, Shop page, Checkout page and My Account page.

    And it uses this code but can it be used for my other pages with some modification:

    add_filter( 'display_post_states', 'add_display_post_states', 10, 2 );

    Thanks

    Morris

    • This reply was modified 4 years, 3 months ago by morris373.
    Thread Starter morris373

    (@morris373)

    Hi
    I found this code but I would rather use the actual page title so how can this code be changed from the ID?

    The language below is English and Welsh.
    Post ID – 13, is Contact Us so that gives me Contact Us – Cysylltwch and
    Post ID – 11 is About so that gives me About – Am

    add_filter( 'display_post_states', 'my_post_states', 10, 2 );
    function my_post_states( $post_states, $post ) {
        if ( 13 === $post->ID ) {
            $post_states['cysylltwch'] = 'Cysylltwch';
        }
    
        if ( 11 === $post->ID ) {
            $post_states['am'] = 'Am';
        }
    
        return $post_states;
    }

    Cheers

    Morris

    Thread Starter morris373

    (@morris373)

    Hi
    Just found what I was looking for, so now I have this:

    add_filter( 'display_post_states', 'my_post_states', 10, 2 );
    function my_post_states( $post_states, $post ) {
        if ( 13 === $post->ID ) {
            //$post_states['cysylltwch'] = 'Cysylltwch';
        }
    
        if ( 'Contact Us' === $post->post_title ) {
            $post_states['cysylltwch'] = 'Cysylltwch';
        }
    
        if ( About === $post->post_title ) {
            $post_states['am'] = 'Am';
        }
    
        return $post_states;
    }

    Now, I need to do this to all the pages. Could there be an easier way to do this maybe via 2 arrays and a loop?

    Or maybe a plugin that already exists…I could then create a page in one language and then add the translated word, save the page and after that the post state is automatically added.

    That would be far quicker, it would just be like the Slug field or a Custom Field.

    Cheers

    Morris

    Thread Starter morris373

    (@morris373)

    Hi
    Sorry to mention any plugins but I started today not knowing how to describe what I wanted to do and I just used them as examples of how they did what I wanted to achieve.

    To modify the code now is probably a PHP question now as I found what creates the Post State i.e. ‘display_post_states’

    Hopefully this is useful for other WordPress users.

    Morris

    Thread Starter morris373

    (@morris373)

    Hi
    I finally solved my problem by looking for solutions on the Internet and using a Custom Field. It works on Pages and Posts.

    My final code is:

    /** Add Post State to Page Title */
    add_filter( 'display_post_states', 'my_post_states', 10, 2 );
    function my_post_states( $post_states, $post ) {
        $pages = get_pages(); 
        foreach ($pages as $page) {
            if (!is_front_page()) {
                if(get_field('translated_title')) {
                    if ( $page->post_title ) {
                        $post_states['translated_title'] = get_field('translated_title');
                    }
                return $post_states;
                }
            }
       }
    }
    

    I hope others will find it useful especial if building a Bilingual WordPress Website.

    Cheers

    Morris

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do you add a page description like Home – Front Page?’ is closed to new replies.