Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a "single page" WordPress based site that has content in multiple languages (using the plug-in WPML) and I need to retrieve it in a more efficient manner to avoid slow page load time.

An example of the code that I'm currently using -

$query = new WP_Query();
$query->query( array( 'post_type' => 'any', 'orderby' => 'id', 'order' => 'ASC' ) );

while ( $query->have_posts() ) {
    $query->the_post();

    // Gather the content for the first section of the page
    if( $post->ID == 100 ) {
        $firstSection .= get_content();
    }

    // Gather next content section etc...

}

Essentially, what I'm doing right now is looping through all posts looking for a specific ID, for which I then retrieve content tied to a certain part of the page. As you may understand, this gets quite messy after a while, having to create an if statement for each section content for each language. I have around 20 statements inside the while loop currently and I'm sure there is a better way to retrieve the data. Any ideas?

share|improve this question
add comment

2 Answers

WPML has a global to check for the language, though it might be outside the scope of WPSE because this is a commercial plugin.

It works like:

if (ICL_LANGUAGE_CODE == 'en'){
//do something like a custom query...
}else{
// do something else
share|improve this answer
    
In reply to both of you - Perhaps a setup like this: 1. Check the current language 2. Query only the posts from that language (how?) 3. Loop through the query, now there's no need for "double if statements" (2 languages). –  Staffan Estberg Aug 22 '12 at 13:06
1  
Put the query in the conditional language check, which is the example above.. –  Wyck Aug 22 '12 at 13:49
    
Done. Clueless as of how to fetch all posts that are of a specific language via a query though (and with WPML specifically). –  Staffan Estberg Aug 22 '12 at 14:28
1  
There is a query parameter for this documented on the WPML site, but since this is a commercial plugin we tend to close these questions here on stack, it's best if you ask over on their site. –  Wyck Aug 22 '12 at 15:57
    
Ah, I understand. I have put up a request at their support but it's not the fastest forum. Thanks for the input! –  Staffan Estberg Aug 22 '12 at 18:21
add comment

Have a decent look at wp_query.

http://codex.wordpress.org/Class_Reference/WP_Query

you can just filter on your post_id at hand. and get only that post. crazy to go like you are doing :)

$query = new WP_Query( 'p=100' );

also

post_in

More on you question: Using Switch Case will be better than your if statements. You could nest them. first switch case your language, then loop your pages.

share|improve this answer
    
Hm, not sure I understand - why would I want to make a query for every post when I can query them all and loop that query one single time? –  Staffan Estberg Aug 22 '12 at 10:18
    
then I do not quite understand your question I think. –  woony Aug 22 '12 at 11:02
    
I have a number of posts that I all need to retrieve because the site is a "single page" solution. I figure I can make a query that selects all, then loop through that query and assign a particular ID's content to a variable, which I print in between my HTML (in the example above, $firstSection). Now I manage to solve this, but I'd like to find out if there is a more efficient way of doing this than involving a severe amount of if statements. –  Staffan Estberg Aug 22 '12 at 11:15
    
using a switch might be a bit more efficient. php.net/manual/en/control-structures.switch.php also stackoverflow.com/questions/427760/… –  woony Aug 22 '12 at 11:20
    
Yes, that might improve the script a bit. I'm more considered how to first check for language, then for the post ID. Not needing to do a language check every time I make a statement, if possible. That would cut down the size significantly. –  Staffan Estberg Aug 22 '12 at 12:06
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.