1

I'm working on a responsive design desktop/mobile but I have a doubt regarding PHP calls, as the hosting provider didn't like high cpu usage.

In the loop query I have something like this:

<?php
  $feature = new WP_Query();
  $feature->query('category_name=featured&posts_per_page=6');
  while ( $feature->have_posts() ) : $feature->the_post();
?>

<div class="desktop 3-col">
  <?php the_title(); ?>
  <?php the_permalink(); ?>
  <?php the_excerpt(); ?>
  <?php the_date(); ?>
</div>

<div class="mobile 2-col">
  <?php the_title(); ?>
  <?php the_permalink(); ?>
  <?php the_excerpt(); ?>
  <?php the_date(); ?>
</div>

<?php endwhile; ?>

Only one of the desktop/mobile divs displays at a time, the other gets a "display:none;" tag, but the sourcecode is still there. So, I don't know if that is like double running the PHP calls?

Would be better something like this? So I just do one PHP call to the database, store the info into variables and echo the results?

<?php
  $feature = new WP_Query();
  $feature->query('category_name=featured&posts_per_page=6');
  while ( $feature->have_posts() ) : $feature->the_post();

  $title = get_the_title();
  $link = get_the_permalink();
  $exc = get_the_excerpt();
?>

<div class="desktop 3-col">
  <?php echo title + ' ' + link + ' ' + exc; ?>
</div>

<div class="mobile 2-col">
  <?php echo title + ' ' + link + ' ' + exc; ?>
</div>

<?php endwhile; ?>

I had been searching for info about how the loop works, but it focus just in the front end, so I don't know it it works as I'm assuming..

The query is just an example, as the real page has something around 6 queries which renders 60 posts.. And making PHP calls for all that can be a lot? (for one page on a full site)

  • You've already called (and in your case, this is the proper way to call) the_post()$feature->the_post();. So get rid of those <?php the_post(); ?>. – Sally CJ Jul 28 '18 at 22:53
  • 2
    You should really use the same DIV to handle both solutions based on @media queries, using defined break points determined by what you consider a mobile device and not. – Ideatron Jul 28 '18 at 23:45
  • @SallyCJ My bad, I edited the OP =) Thing is, I use uikit framework css, and it uses classes "uk-visible@m / uk-hidden@m" and seen a lot of WP code repeated as in my first block code didn't seems optimal. It's not just a matter of responsive, but structure. Desktop post use a large cover image, while mobile just a tiny thumbnail for example – DrM Jul 29 '18 at 2:55
  • 2
    You're only actually making the query once. Outputting the values twice will be a little bit slower, but not the same as making the query twice. The extra amount of HTML that the user has to download to render the page would be a bigger concern to me, but wouldn't affect CPU usage on the server. – Jacob Peattie Jul 29 '18 at 3:01
  • Would be better something like this? - yes, it would be, since the content are identical or that the div only differ in their class - i.e. desktop vs mobile. You can also use output buffering, like this - which is a simplified example: ob_start(); the_content(); $content = ob_get_clean(); then do <div class="desktop"><?php echo $content; ?></div> and <div class="mobile"><?php echo $content; ?></div>. But a more proper way, I think, would be using CSS to adjust the display on desktop and mobile devices, without having to duplicate the content - i.e. echo it twice. – Sally CJ Jul 29 '18 at 4:02
0

Like Jacob Peattie has said above, the query is actually only made once. The extra processing really is negligible, as would be the load time of the page. So don't worry about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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