[resolved] Query posts by category (5 posts)

  1. ruimolar
    Posted 3 years ago #


    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!


  2. Chouby
    Plugin Author

    Posted 3 years ago #

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

  3. ruimolar
    Posted 3 years 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:

    if ( have_posts() ) : while( have_posts() ) : the_post();
    <div class="span3">
         <div class="pic">
              <?php if (( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() )) {
                         } else { ?>
                              <img src="<?php echo get_template_directory_uri(); ?>/img/no-thumbnail.jpg" width="260" height="260">
                         <?php } ?>
         <div class="id">
              <h3><?php the_title(); ?></h3>
              <?php the_content(); ?>
    <?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
    Plugin Author

    Posted 3 years 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

    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
    Posted 3 years ago #

    Everything's working beautifully now. Thank you!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Polylang
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic