WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] How to get post language during a mixed language Loop? (10 posts)

  1. RavanH
    Member
    Posted 2 years ago #

    Hi Chouby,

    It's been a while but the issue has come back since get_option('rss_language') was dropped in WP 3.4 (as expected). We have talked about it before on http://wordpress.org/support/topic/plugin-polylang-static-home-page-404-issue?replies=54 but to recap, the problem is this:

    I've got a Google News XML feed (plugin) that lists all the latest post URLs (of the last 48 hours) in all languages. This is how Google prefers it, all languages in the same XML. But... each item/post must have the proper language tag assigned. Like this example:

    <url>
      <loc>http://4visions.nl/2012/06/wordpress-platform-upgrade/</loc>
      <news:news>
        <news:publication>
          <news:name>4Visions</news:name>
          <news:language>en</news:language>
        </news:publication>
        <news:publication_date>2012-06-19T14:08:49+00:00</news:publication_date>
        <news:title>WordPress platform upgrade</news:title>
        <news:keywords>upgrade</news:keywords>
        <news:genres>Blog</news:genres>
      </news:news>
    </url>
    <url>
      <loc>http://4visions.nl/2012/06/upgrade-van-het-wordpress-platform/</loc>
      <news:news>
        <news:publication>
          <news:name>4Visions</news:name>
          <news:language>nl</news:language>
        </news:publication>
        <news:publication_date>2012-06-19T13:28:03+00:00</news:publication_date>
        <news:title>Upgrade van het WordPress platform</news:title>
        <news:keywords>Platform</news:keywords>
        <news:genres>Blog</news:genres>
      </news:news>
    </url>
    ...

    (notice the <news:language> tags)

    So what I want is to get each posts language while going though The Loop. But whatever I try, I get the sites default language... Before in WP 3.3, I used get_option('rss_language'); which after a mod to Polylang on your part, worked perfectly. Now, for WP 3.4 where get_option('rss_language') was dropped, I switched to reset(explode('-', get_bloginfo_rss('language'))) but this value does not get changed during the Loop so only the default language is used in each language tag.

    I tried using $polylang->get_post_language($post->ID) and get_the_terms($post->ID, 'language' ) but those do not return anything.

    Is there any way (don't mind adding a function for this, as long as it is possible) to get to a posts language without falling back on the global language during the Loop?

    Thanks for your thoughts!

    .Allard

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Hello !

    I downloaded the version 3.9.2 of your plugin. I tested:

    global $polylang;
    echo $polylang->get_post_language(get_the_ID())->slug;

    and it works for me. (I had to clean the cache however to see the result).
    I do not understand why the function get_post_language does not return anything for you.

  3. RavanH
    Member
    Posted 2 years ago #

    I'm running this combo on a multi site install. Could that explain it?

    It's indeed strange that get_the_terms($post->ID, 'language' ) would not return anything when clearly everything else in Polylang works on multi-site...

  4. RavanH
    Member
    Posted 2 years ago #

    Hang on. After testing $polylang->get_post_language() again it turns out to be indeed a browser cache clearing thing. But that revealed something else going on:

    At fisrt I got a mixed language list (where the polylang global did not work) but after visiting /sitemap-news.xml?lang=en which limited the list to only english entries (as expected) and clearing the cache I returned to view the normal /sitemap-news.xml without the query parameter and now only the english entries where visible in the list. Same thing happens whith the other languages.

    Google News likes all languages to be inside the same list.

    Would it be possible to get posts of ALL languages in one query?

  5. RavanH
    Member
    Posted 2 years ago #

    Come to think of it, the main /sitemap.xml without 'lang' request parameter ALWAYS shows a mixed language list. Even when first visiting /sitemap.xml?lang=fr and then coming back (and refresh) still shows all languages.

    I really wonder why this is different with the /sitemap-news.xml ... They have different templates but basically they do the same: exceute a query and run a wordpress loop.

  6. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    It's really strange what you get because for me:
    /sitemap.xml : all posts in all languages
    /sitemap.xml?lang=fr : all posts in French
    /sitemap-news.xml : all new posts in all languages
    /sitemap-news.xml?lang=fr : all new posts in French.
    I guess it is the result you expect.
    I am using latest versions of both plugins and browser cache is deactivated to make the tests. I tested several combinations of options for Polylang

  7. RavanH
    Member
    Posted 2 years ago #

    Note that you need to have recent posts (not older than 2 days) or else the sitemap-news.xml request gets redirected to sitemap.xml!

    If you have recent posts: Did you visit the sitemaps in the following order, each time refreshing with Ctrl+R or equivalent ?

    /sitemap.xml : all posts in all languages
    /sitemap.xml?lang=fr : all posts in French
    /sitemap.xml : all posts in all languages
    /sitemap-news.xml : all new posts in all languages
    /sitemap-news.xml?lang=fr : all new posts in French
    /sitemap-news.xml : all new posts in French (!)

    All are as expected except that last one... I suppose it can not be an option in Polylang or else it would behave the same in both the sitemaps.

    Could it be something in the wordpress query or how the plugin template feed-sitemap-news.php is built?

  8. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    I made tests in the same order as you and I do not reproduce the issue. Of course I have at least one new post in each language to test the sitemap-news.xml. I do not understand what can produce such issue for you. Did you test with another browser ?

  9. RavanH
    Member
    Posted 2 years ago #

    The only real difference (as far as I can tell) between the two templates was the fact that one started with setting some response headers and a few lines of code before running the loop while the other started with a if(!have_posts()) to determine which response headers would be needed before actually starting the response. Could that have any affect on the language used in the query itself? Maybe in combination with the theme (a Thematic Framework child theme) or even the fact that it is a Mullti-site setup running on an Nginx server?

    In any case, I've reworked the complete template following the basic structure of the other template where I did not see this issue and it seems to work. I get a mixed list every time now... Plus getting the language taxonomy works perfectly so this is going into the dev version of the XML Sitemap Feed plugin :)

    Thanks for your help!

  10. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Good news :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic