WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
Custom query question, and a big thank you (2 posts)

  1. Haitham
    Member
    Posted 11 months ago #

    First of all, THANK YOU SO MUCH for this plugin. Even though I have not used it on a live site yet, but I have been tinkering with it for the past several days and going over your documentation (PDF and online), and all plugin related support tickets (all 24 pages of them hehehe). Polylang seems solid and a true performer to say the least, but above all, your continuous and prompt support that I have personally felt in all the support threads are immense and should be applauded. So thank you!

    Before I start I must indicate that I am a designer and not a hardcore developer, and thus some of the terms you used in your documentation eluded me. I have:

    Languages

      Arabic (default)
      English
      All categories have been translated.

    In my home.php file, I have several custom queries that present articles in blocks from various categories. Here is a sample code block:

    function halkibsi_multimedia_multicolumn_loop() {
    	global $post;
    
    	$formats = get_post_format_slugs();
    
    	foreach ( (array) $formats as $i => $format ) {
    		$formats[$i] = 'post-format-' . $format;
    	}
    
    	$multimedia_posts = get_posts( array(
    		'post_type'			=> 'post',
    		'category'			=> 21, // Arabic Category ID
    		'lang'				=> 'ar', // use language slug in the query
            'posts_per_page'	=> 4,
    		'offset'			=> 0,
    		'orderby'			=> 'post_date',
    		'post_status'		=> 'publish',
    		'tax_query' => array(
    			array(
    			  'taxonomy' => 'post_format',
    			  'field'    => 'slug',
    			  'terms'    => $formats,
    			),
    		)
    	) );
    	//
    	$counter = 0;
    	foreach( $multimedia_posts as $post ) : setup_postdata($post);
    
    		if($counter % 2 == 0) : // half of the the showposts count in the query
    			echo '<div style="display: table-row;">';
    		endif;
    
    		// LOOP HTML OUTPUT CODE HERE
    
    		$counter++;
    		if($counter % 2 == 0) :  // half of the the showposts count in the query
    			echo '</div>';
    		endif;
    
    	endforeach;
    	wp_reset_postdata();
    	wp_reset_query();
    }

    Additional Information:

      Category "21" (in the query) is the Arabic category
      Category "37" (not in query) is the English translation

    Question:
    How do I re-write this function so that Polylang will automatically identify the current language and return values from the translated category, in this case "37"? Or I'm sure your have a better function than mine. I welcome any suggestions :)

    Thanks again!

    -----
    1. your permalinks settings
    custom permalinks --> /%category%/%post_id%/%postname%/
    -----
    2. if you are using a static front page
    Yes, but NOT through the admin Settings > Reading option, but by naming my file as home.php
    -----
    3. your Polylang settings
    screenshot --> https://dl.dropboxusercontent.com/u/6411826/screenshot.png

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

  2. Chouby
    Member
    Plugin Author

    Posted 11 months ago #

    Thank you very much for your positive feedback.

    When you use the parameter 'lang' => 'ar', you force the Arabic language in the query. To keep the current language, you should remove it.

    Then when you query one category (here 21), Polylang should normally automatically translate it to the corresponding English category if the English home page is queried.

    NB: In your case, I would verify first that there is no conflict between your category and your tax_query (independantly of Polylang) as a 'category' query is internally transformed in a 'tax_query' by WordPress. Maybe the obtained result is what you expect, maybe not.

Reply

You must log in to post.

About this Plugin

About this Topic