Support » Plugins » Hacks » Help combining browser detection and pulling in different featured posts?

  • Hi – I’m trying to do something outside my php skill level, but I’m doing so purposely to try to stretch and learn. I’d like someone to either check/correct my (probably hideous) attempt at coding this, or suggest a more elegant solution than the one my tiny brain came up with.

    This is what I’d like the site to do:

    A client of mine is going to have video blog posts (a vlog), separate from regular blog posts. In the sidebar, a thumbnail of the most recent vlog post will appear as a link to the single/permanent url of the post. Using a plugin, that I can easily do.

    However, she wants to make sure iPhone users aren’t left out, so she plans to have two videos available, one in Flash and one in mp4.

    My plan, to make it as quick and easy for the user as possible, is for my client to make two blog posts for each video (they won’t be pulled into the regular blog). She can tag or categorize them to differentiate. Then I’d like the blog to detect the browser (I’m using the PHP Browser Detection plugin to do this) then pull into the sidebar either the link to the Flash version of the post or the mp4 version, depending on the results. The link would go straight to the correct version of the video without the user having to click a second link if they don’t have Flash.

    PHP Browser Detection creates some custom functions for you to use (like “<?php if ( is_iphone($version) ) { do stuff }; ?>”). Using this, I created the following which 1) I fully acknowledge could be a code abomination and 2) unsurprisingly, causes the sidebar (and the footer) not to appear at all.

    <!-- Right Sidebar Start -->
                <div id="sidebar_right">
    
                <div class="widget_stray_quotes">
                	<h2>See the latest vlog</h2>
    
    <!-- Check the browser to get the link to the correct vlog format -->
    
     <?php if (is_iphone()) {
    
    	//Get the link to the mp4 post ?>
    
    		<?php if (have_posts()) : 
    
    			(query_posts('tag=vlog_mp4&posts_per_page=1');?>
    
                <?php while (have_posts()) : the_post(); ?>
    			<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
    
                <?php endwhile; ?>
                	<?php endif; ?>
                     </div>
    
    			<?php }
    	else {
    
    			//Get the link to the Flash post ?>
    		<?php if (have_posts()) :
    				(query_posts('tag=vlog_flash&posts_per_page=1'));?>
    		 <?php while (have_posts()) : the_post(); ?>
    			<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
    
                <?php endwhile; ?>
                	<?php endif; ?>
    
                     </div>
    
    	<?php endif; ?>
    
    <!-- end vlog function -->
    
    </div>

    Anyone have an idea what’s not working/a better way to go?

Viewing 5 replies - 1 through 5 (of 5 total)
  • This doesn’t look like a plugin code, rather it seems like you’re inserting your code directly into the theme’s sidebar, am I correct?

    I’ll suggest you replace all the code inside the if/else with the text “iphone” and “flash” just to check that the basic logic/loop is working first.

    Off hand, I’d say the main problem is you getting confused with using braces { } and the colon : notation for defining code blocks.

    Off-topic: There’s no need to follow the convoluted WordPress style of using colons and intermixing HTML/PHP if you are already used to the more typical and readable style of using braces.

    Actually the main issue is that you are trying to use multiple loops and your loop structure is a bit off.

    <?php if (have_posts()) : 
    
            (query_posts('tag=vlog_mp4&posts_per_page=1');?>
    
         <?php while (have_posts()) : the_post(); ?>

    You want to do a query, then say “if have posts”, then “wile we have posts”…, do stuff.

    Adding multiple queries and custom loops can get confusing. The page itself has a main loop, so you need to create a new query object that would look like

    $recent = new WP_Query($args);

    then loop through that…

    Details here:
    http://codex.wordpress.org/The_Loop
    http://www.catswhocode.com/blog/multiple-wordpress-loops
    http://wptricks.net/how-to-setup-multiple-loops-in-wordpress/

    Okay, guys, between both of your advice – and the article at wptricks.net and a look at the get_posts documentation in the Codex – what I have is probably hella sloppy but now seems to work. This was it:

    <?php if (is_iphone()) { 
    
    	//Get the link to the mp4 post ?>
    
    	<?php $mp4_posts = get_posts('tag=vlog_mp4&posts_per_page=1');
        while ($mp4_posts->have_posts()) : $mp4_posts->the_post(); ?>
      	<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
      <?php endwhile; 
    
    } else {
    
    			//Get the link to the Flash post  ?>
    
    		<?php $flash_posts = get_posts('tag=vlog_flash&posts_per_page=1');
    		foreach($flash_posts as $post) :
    	    setup_postdata($post);?>
        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
     <?php endforeach;
    }
    
    ?>

    If it is indeed as Rube Goldberg-y as I suspect, suggestions for making it less so are always welcome. And thanks again. 🙂

    Okay, guys, between both of your advice – and the article at wptricks.net and a look at the get_posts documentation in the Codex – what I have is probably hella sloppy but now seems to work. This was it:

    <?php if (is_iphone()) { 
    
    	//Get the link to the mp4 post ?>
    
    	<?php $mp4_posts = get_posts('tag=vlog_mp4&posts_per_page=1');
        while ($mp4_posts->have_posts()) : $mp4_posts->the_post(); ?>
      	<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
      <?php endwhile; 
    
    } else {
    
    			//Get the link to the Flash post  ?>
    
    		<?php $flash_posts = get_posts('tag=vlog_flash&posts_per_page=1');
    		foreach($flash_posts as $post) :
    	    setup_postdata($post);?>
        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
     <?php endforeach;
    }
    
    ?>

    If it is indeed as Rube Goldberg-y as I suspect, suggestions for making it less so are always welcome. And thanks again. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Help combining browser detection and pulling in different featured posts?’ is closed to new replies.