[wp-edu] Need help with code to select right image

Cara Peckens cpeckens at jhu.edu
Wed Sep 25 11:57:20 UTC 2013


Kim – It sounds like you'd be better off using Wordpress' built-in "post format" functionality.  http://codex.wordpress.org/Post_Formats

To turn it on add this line to your functions.php file: add_theme_support( 'post-formats', array( 'video', 'audio' ) );

Then you can use conditionals to display each post format type differently in the single.php file by either doing the layout there or calling other template files i.e.


if ( has_post_format( 'video' )) {
  echo 'this is the video format'; //this is how I want it displayed ORget_template_part( 'content', 'video' ); //This would call the content-video.php file

}

From: <Charles>, Kimberly <kbaxa at uic.edu<mailto:kbaxa at uic.edu>>
Reply-To: "Low-traffic list discussing WordPress in education." <wp-edu at lists.automattic.com<mailto:wp-edu at lists.automattic.com>>
Date: Tuesday, September 24, 2013 4:32 PM
To: "Low-traffic list discussing WordPress in education." <wp-edu at lists.automattic.com<mailto:wp-edu at lists.automattic.com>>
Subject: [wp-edu] Need help with code to select right image

Hello --

I need help with some custom code for my WordPress site. I have a site that has 2 multimedia post types:
$media_type = 'audio';
$media_type = 'video';
and the site should pull the youtube thumbnail for the 'video' type and the post thumbnail for the 'audio' type, however, there isn't any if/else statement to distinguish between the two types for what image to use (this was built by someone else and I'm trying to fix it).

I'm not sure (a.) if I should have the function inside functions.php do this or if (b.) it should be at the page template level.

This is what I have in functions.php -- which tells WP to get one thing for 'video' but doesn't tell it what to do for 'audio'

function abc_media_thumbnail() { ?>
<div class="thumb">
 <!-- RUN TIME, CAST, etc... -->
 <a>">
   <?php $media_type = 'video';
       get_youtube_thumbnail(get_field('media_url'));
       $runtime = get_field('length');
   ?>
 </a>

 <?php if (!empty($runtime)): ?>
 <div class="runtime"><?php echo $runtime; ?></div>
 <?php endif; ?>
 <div class='media-info'>
   <p class='title'><a>"> <?php the_title(); ?></a></p>
   <p class="date grayed"><?php the_time('F j, Y'); ?></p>
 </div>
</div>
<?php
}

This is what the page template has:

<?php get_header();
$url = get_bloginfo('url'); ?>

<div id="content">
  <?php if ( have_posts() ) while ( have_posts() ): the_post(); ?>
 <?php
 $media_terms = get_the_terms($post->ID, 'media-type');

 if (is_array($media_terms)) {
 $media_type = array_pop($media_terms);
 }
 ?>

 <?php if (isset($media_type)): ?>
   <div class="labels">
     <a>slug; ?>"><?php echo $media_type->name; ?></a>
   </div>
   <div id="media" class="clearfix">
     <?php
       if ($media_type->slug == 'video') {
         abc_video_post();
       } else {
         abc_audio_post();
       }
     ?>
   </div>
 <?php endif; ?>
  <?php endwhile;
    wp_reset_postdata();
  ?>

  <?php if (isset($media_type)): ?>
 <h2 class="recent">Recently Featured</h2>
 <?php
   $args = array(
     'posts_per_page' => 4,
     'post__not_in' => array($post->ID),
     'tax_query' => array(
       array(
         'taxonomy' => 'media-type',
         'field' => 'slug',
         'terms' => $media_type->slug
       )
     )
   );
   $recent_posts = new WP_Query( $args );
   if ( $recent_posts->have_posts() ) while ( $recent_posts->have_posts() ): $recent_posts->the_post();
     abc_media_thumbnail();
   endwhile; ?>
  <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Any help is much appreciated!
Kim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.automattic.com/pipermail/wp-edu/attachments/20130925/aa431331/attachment.html>


More information about the wp-edu mailing list