[Resolved] How to get shared CSS category class between translated WPML pages

Home Forums Support How to get shared CSS category class between translated WPML pages

  • This topic has 6 replies, 2 voices, and was last updated 4 months ago by Tom.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1303711
    onalti

    Hello, it adds the category names as post class. I use the wpml translation plugin, and I want to define the translated article category names as the main language names. Is there a filter I can use for this?
    I hope I can explain my problem, thank you.

    <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?> <?php generate_do_microdata( ‘article’ ); ?>>

    <article id=”post-31379″ class=”post-31379 result type-result status-publish has-post-thumbnail hentry portfolio_category-fotos masonry-post generate-columns tablet-grid-50 mobile-grid-100 grid-parent grid-33 resize-featured-image masonry-brick” itemtype=”https://schema.org/&#8221; itemscope=”” style=”position: absolute; left: 0%; top: 56px;”>

    <article id=”post-31379″ class=”post-31379 result type-result status-publish has-post-thumbnail hentry portfolio_category-photo masonry-post generate-columns tablet-grid-50 mobile-grid-100 grid-parent grid-33 resize-featured-image masonry-brick” itemtype=”https://schema.org/&#8221; itemscope=”” style=”position: absolute; left: 0%; top: 56px;”>

    I want to use the same definition in translated categories. Let them all be in the mother tongue.

    #1303770
    onalti

    I found this for the wmpl translation plugin. but I couldn’t apply.

    I need to add the same CSS class to <body> element of all translatons of a page, but the CSS class added with body_class() is different for each translation because it is taken from the translated title of the page.

    For example, for about us page I get this markup:

    <body class=”about-us”> // English
    <body class=”sobre-nosotros”> // Spanish
    I want to get the same class for both languages:

    <body class=”about-us”> // English
    <body class=”about-us”> // Spanish
    Is there a way to add the same CSS class for all translations of the same page?

    function add_default_language_slug_class( $classes ) {
        global $post;
        if ( isset( $post ) ) {
            $default_language = wpml_get_default_language(); // will return languague code of your default language for example 'en'
            // get the post ID in default language
            $default_post_id = icl_object_id($post->ID, 'post', FALSE,$default_language);
            // get the post object
            $default_post_obj = get_post($default_post_id);
            // get the name
            $default_post_name = $default_post_obj->post_name;
            // add default language post name to body class
            $classes[] = $default_post_name;
        }
        return $classes;
    }
    add_filter( 'body_class', 'add_default_language_slug_class' );
    #1304183
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This is likely something you need to ask WPML support. I’m not familiar enough with their functions to know how to do it.

    It looks like you’re on the right track, you just need to right functions which they should be able to help with 🙂

    #1304193
    onalti

    Hi thank you for the answer.
    yes I will get support from WPML but generatepress
    Is there a general filter for post_class();

    #1305300
    Tom
    Lead Developer
    Lead Developer

    Yes, there’s a post_class filter as well as a body_class filter – both are core WP filters 🙂

    #1305454
    onalti

    thank you 🙂

    #1305609
    Tom
    Lead Developer
    Lead Developer
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.