• Resolved berryblue031

    (@berryblue031)


    The theme I am using in header.php queries for all posts in a specific category to show a special slideshow. How can i get this query to consider the language of the post and only return items that match the language of the current page?

    $cat = get_option(THEME_NAME.'_slider_category');
    $args= array('cat'=>$cat,'post_status' => 'publish','posts_per_page' => 1000);
    query_posts($args);
    $pageURL = get_page_url();
    $i = 1;
    while (have_posts()) : the_post();
      $img = "";
      if (has_post_thumbnail())
      { //do some stuff }

    https://www.ads-software.com/extend/plugins/polylang/

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Chouby

    (@chouby)

    Could you tell me where I can find this theme ?

    I would also like to know this!

    I am trying to query a custom post type and i only want to return posts of a certain language.

    There must be an easy way to do this that i have overlooked.

    Please help!

    Plugin Author Chouby

    (@chouby)

    @tuneintokyo,
    In your case it’s in fact quite easy. Just query your custom post type together with the language you want like that:

    yoursite.com/?post_type=your_custom_post_type&lang=your_language_code

    where your_language_code is the 2 characters code you chose for the language.

    For berryblue031, it would be a bit more complex as each category is normally affected one unique language.

    for custom post type try something like this (for english lang):

    <?php
    $loop = new WP_Query(array('post_type' => 'YOUR_CUSTOM_TYPE_SLUG', 'lang' => 'en', 'paged' => get_query_var('paged'), 'posts_per_page' => 10));
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    	// html here
    
    <?php endwhile; ?>

    Brilliant! I knew it should be easy.
    @chouby, @yoyurec, thanks so much for your help!

    Thread Starter berryblue031

    (@berryblue031)

    For berryblue031, it would be a bit more complex as each category is normally affected one unique language.

    I can easily make a category for each language. But the theme only allows me to input one category via it’s control panel.

    So using that approach given a category id
    $cat = get_option(THEME_NAME.'_slider_category'); //this returns an integer
    How can I get the id of the related category in the language of the current page?

    If there is no ‘nice’ way of doing it I guess I will just have to hack into your cookie variable and hardcode the category id based on it’s value. Though I would rather not have to hack it… I already had to make some hacks because the theme I am stuck with depends on a specific permalink format that your language drop down doesn’t respect and my php / wordpress skills aren’t good enough to modify the theme so I went with the easier route.

    Ironically I am a professional C++ developer and some how that meant I have the skills and should make a website for my mother-in-laws bed and breakfast :P..

    Plugin Author Chouby

    (@chouby)

    To manually know the id of a given category is easy. Go in your category admin panel, edit the category and look at the address your browser sent you. It’s something like:
    yoursite/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=5&post_type=post
    The number after the tag_ID is the category id you are looking for.

    Then to get the language of the current page, no need to play with the cookie. You can use get_locale() which returns something like en_US or get_bloginfo('language') which returns something like en-US.

    Finally you can hardcode the category id depending on the language page.

    However, if the theme is downloadable somewhere, I would be interested to get the link. I would like to see if I can adapt the plugin to such cases.

    Thread Starter berryblue031

    (@berryblue031)

    The theme is available to download you can get it here, it includes documentation about setting the permalinks
    https://www.themeskingdom.com/en/page/Widely/229/theme

    The first problem I had with it, is it requires a staic frontpage and uses is_front_page() in header.php to do special stuff on the front page,but when using your plugin there are multiple front pages (one for each language) and is_front_page was returning false on these pages – to get around this I ended up pulling all the code out of the themes front-page.php and putting it in a template called _front.php, then changing the is_front_page() calls to is_page_template(‘_front.php’), ideally is_front_page() would have returned true for all language versions of the front page.

    And the second problem was with your language drop down, when I used it the pages didn’t work at all when referenced with /language/da, and the dirty hack I had to do for that was change the all my front pages to be called “en-home.php” , “da-home.php” etc, and then “modify” your Polylang_Widget href to use my naming scheme instead which is really ugly

    // $url = home_url('?lang=');
    $url = home_url('');
    ...
    if (this.value == '$default')
       location.href = '$home_url';						else
    {
       //location.href ='$url'+this.value;
       location.href = '$home_url' + '/' + this.value +'-home/';
    }

    Thread Starter berryblue031

    (@berryblue031)

    Back to topic of the category is it really necessary to hard-code the category id (I was hoping to avoid that). I was kinda hoping there would be a function called getAlternateLanguage($cat_id, get_locale()); that would then return the id of the category in the proper language? I have linked the categories together as language alternatives of each other.

    Plugin Author Chouby

    (@chouby)

    I will take your suggestions into account for v0.5. However you gave me an idea and you may prefer using this alternative for now :

    // returns the id of the translation of a term (category, post tag or custom taxonomy)
    $translate_cat_id = get_metadata('term', $cat_id, '_lang-'.$slug, true);

    where $slug is the two characters code you chose for the language. It should normally be accessible with:

    $slug = substr(get_locale(), 0, 2);

    Thread Starter berryblue031

    (@berryblue031)

    Ahhh that is perfect! Thank you.
    It’s really great how fast you are at responding on the forum ??

    Plugin Author Chouby

    (@chouby)

    To come back on the two problems you mentioned above:

    Unfortunately, the way it is coded in WordPress, I believe there is no way to filter is_front_page() so that it returns true for all front pages in all languages, when using a static front page. I will have to create an alternative function in the coming Polylang API.

    I know that the few javascript lines I wrote for the dropdown list in the language switcher widget are far from enough. I did that very quickly to answer a user’s request. It’s on my roadmap to improve this, but I did not decide a time schedule yet. Certainly not for the 0.5 for which the list of new features is already quite long.

    Thread Starter berryblue031

    (@berryblue031)

    You know I would have actually preferred to use the list version of the widget instead of the dropdown, but I didn’t know how to make it layout horizontally. Is it possible?

    Plugin Author Chouby

    (@chouby)

    I will have to include this in the documentation. You are the third to ask. Look here.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Plugin: Polylang] How to query for posts in the page language?’ is closed to new replies.