WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Query posts by category (5 posts)

  1. ruimolar
    Member
    Posted 1 year ago #

    Hi,

    I'm a noob programmer and I've developed a theme for a friend. Everything works ok, except when I try to query posts by category. I have translated the categories but doesn't seem to be working. Is there a specific way to make it work with Polylang? Thanks! Awesome plugin by the way!

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

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Could you explain exactly the result you expect and how you try to do it?

  3. ruimolar
    Member
    Posted 1 year ago #

    Yes. Sorry, I should have been more specific.

    I've created a one-page only theme and I'm trying to display posts with different categories. I'm using the following code:

    <?php
    query_posts('category_name=team&order=asc');
    if ( have_posts() ) : while( have_posts() ) : the_post();
    ?>
    <div class="span3">
         <div class="pic">
              <?php if (( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() )) {
                         the_post_thumbnail('thumbnail');
                         } else { ?>
                              <img src="<?php echo get_template_directory_uri(); ?>/img/no-thumbnail.jpg" width="260" height="260">
                         <?php } ?>
         </div>
         <div class="id">
              <h3><?php the_title(); ?></h3>
              <?php the_content(); ?>
         </div>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e( 'No content was found', 'criativia' ); ?></p>
    <?php endif; ?>

    The problem is that the posts are not translated if I use query_posts();. If I comment the query line they are translated. Perhaps there is another way to make it work or probably I'm doing something wrong. Thanks in advance! x)

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    When you query the category 'team', you query the language associated to this category which is unique. If you want to query the category 'team' or its translation depending on the language, you have several solutions.

    1. Use the function 'pll_get_term' provided by Polylang (which will translates the category). For example:

    $cat = get_category_by_slug('team'); // get category object
    $cat = pll_get_term($cat->term_id); // get id of translation
    query_posts("cat=$cat&order=asc");

    2. Use a functionnality I am currently developing: Paste http://pastebin.com/utLLn1cu in a new php file which you will upload in your plugin directory and activate the newly created plugin. It should automatically do the category translation

  5. ruimolar
    Member
    Posted 1 year ago #

    Everything's working beautifully now. Thank you!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic