multiple wordpress custom post meta values - wordpress

I'm pretty hopeless at PHP, so I figured I'd ask for a bit of help!
I'm using WordPress as a CMS for a music agency site at daviesmusic.com and have lots of extra information for the various artists via custom fields. So far, I've just retrieved the custom field values one by one, like so:
<?php
//audio title
$audio_title = get_post_meta($post->ID, 'audio_name', true);
if ($audio_title) :
?>
<p><?php echo $audio_title; ?></p>
This works fine, only I have lots of custom fields that all require different outputs. I have for instance a photo for the artist, an audio file, the title of the audio file and some notes about the audio file, like so:
<?php
//profile image
$profile_pic = get_post_meta($post->ID, 'profileimage', true);
if($profile_pic) :
?>
<img class="post-thumb-single" src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $profile_pic; ?>&a=t&h=278&w=278&zc=1" alt="<?php the_title(); ?>" />
<?php
endif;
//photographer
$photographer = get_post_meta($post->ID, 'photographer', true);
if($photographer) :
?>
<p id="photographer">Photographer: <b><?php echo $photographer; ?></b></p>
<?php
endif;
//audio title
$audio_title = get_post_meta($post->ID, 'audio_title', true);
if ($audio_title) :
?>
<h4 class="nsmb">Audio files</h4>
<p><?php echo $audio_title; ?></p>
<?php
//audio file
$audio = get_post_meta($post->ID, 'audiofile', true);
if ($audio) :
?>
<p class="audio" id="audioplayer_1">Sorry, there was a problem loading the file.</p>
<script>AudioPlayer.embed("audioplayer_1", {soundFile: "<?php echo $audio; ?>"});</script>
<?php
$audio_credits_1 = get_post_meta($post->ID, 'audio_credits_1', true);
if ($audio_credits_1) :
?>
<p class="audio_cred"><?php echo $audio_credits_1; ?></p>
<?php
endif; endif; endif;
?>
Lots of code. I'm sure there must be a more efficient way to retrieve the values! Any ideas?!
Cheers

Take this example from here:
$custom_fields = get_post_custom(72);
$my_custom_field = $custom_fields['my_custom_field'];
foreach ( $my_custom_field as $key => $value ){
echo $key . " => " . $value . "<br />";
}
This will cycle through all specified custom values for the post (id #72 in this example, you can get yours using $post->ID as you have in your code). You can use $key to print the field name, and $value to print the value of that field. You can get more granular with how they're displayed using an if elseif else statement or a switch.

Related

Get vars of prev and next post type

I am trying to make a script which will work in single.php template on footer post. I want to have info about previous custom_post_type and next custom_post_type.
I've written this:
<?php
// get info about prev_post
$prev_post = get_previous_post();
$k_id = $prev_post->post_author;
$p_link = get_page_link($prev_post->ID);
$p_title = $prev_post->post_title;
$p_date = mysql2date('d F Y', $prev_post->post_date, false);
$p_author = get_the_author_meta( 'display_name', $k_id );
?>
<a class="p_title" href="<?php echo $p_link; ?>"><?php echo $p_title; ?></a>
<p class="p_post"><?php echo $p_date; ?> - <?php echo $p_author; ?></p>
Everything works well, but permalink is broken. The custom_post_type url is:
http://mypage.com/customposttype/title_of_entry
but link leads to:
http://mypage.com/title_of_entry
How I can fix it?

Displaying a custom post type title within another loop

I have a Custom Post Type set up called Venues. I'm also using a plugin called Event Organiser, and I want to display the title and link to the venue within one of the Event Organiser templates.
The code is:
<?php if( $eo_event_loop->have_posts() ): ?>
<ul <?php echo $id; ?> class="<?php echo esc_attr($classes);?>" >
<?php while( $eo_event_loop->have_posts() ): $eo_event_loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = ( eo_is_all_day() ? $date_format : $date_format.' '.$time_format );
?>
<li class="<?php echo esc_attr(implode(' ',$eo_event_classes)); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_title(); ?></a> at VENUE NAME HERE <?php echo __('on','eventorganiser') . ' '.eo_get_the_start($format); ?>
</li>
<?php endwhile; ?>
</ul>
Where VENUE NAME HERE is, I want the title and link to my custom post type. Might be just something really simple I'm missing, but any help much appreciated.
I used Advanced Custom Fields plugin and used 'Relationship' field so that I could choose a Venue for each event. Then used this code to bring through the Venue link:
<?php $venue = get_field('location_venue'); ?>
<?php foreach( $venue as $venue ): ?>
<?php echo get_the_title( $venue->ID ); ?>
<?php endforeach; ?>
More info available here:
http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/

Wordpress: Show posts based on custom field

I have a slider script for my homepage (wp nivo slider), but only want to show slides (posts) if todays date is before post_end_date (custom field). This is so that I do not have to manually remove posts that are no longer relevant.
This is the code that loads the posts. Any help would be greatly appreciated.
Custom post field: post_end_date
<?php
$category = get_option('wpns_category');
$n_slices = get_option('wpns_slices');
?>
<?php query_posts( 'cat='.$category.'&posts_per_page=$n_slices' ); if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php if ( '' != get_the_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif ?>
<?php endwhile; endif;?>
<?php wp_reset_query();?>
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
in this function $postid is that id of wich is coming in loop and key is that name of custom field last in $single you can write true of false
get the custom fild by this function after that try the if condition for you custom postfeild values
hope this will help you

Getting post id in WP_Query

I am trying to execute the query in WordPress to retrieve the last post from featured category, then check for its custom field and show an image stored in it.
Well, my code is not working for some reason.Can you spot any error there? Or point me in the right direction?
<?php $featured = new WP_Query('showposts=1&category_name=featured'); ?>
<?php if($featured->have_posts()) : ?>
<?php while($featured->have_posts()) : $featured->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'image3', true); ?>"></a>
<?php endwhile; ?>
<?php endif; ?>
Many thanks!
I hope this will do what you expecting
$args['category_name']='featured'; // posts from particular category
$args['orderby']='date'; //to get latest post
$args['order']='DESC';
$args['numberposts']='1';// to get only one post from post list
$featured = new WP_Query($args);

Wordpress: query all images in a posts media library

Howdy! I am looking for a way to list all of the image files in a posts' media library.
What I mean by this is if a file has been uploaded while creating or editing a post, is the file associated with the post in some way, and can I create a list from this data.
I think that the next_image_link() / previous_image_link(); template tag is as close as I have found.
I think that this should be close:
$query = 'SELECT * FROM `wp_posts`
WHERE `post_parent` = \''.$_GET['post_id'].'\'
AND `post_mime_type` = \'image/jpeg\'
ORDER BY `menu_order` ASC';
thanks.
In wordpress terminology, every image you uploaded to particular post is called attachment.
To list all attachment, you can use get_children() function:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=10' );
$counter=0;
foreach( (array) $images as $attachment_id => $attachment )
{
$counter++;
echo "<a href='".wp_get_attachment_link( $attachment_id ) . "'>image $counter</a><br />";
}
The algorithm is something like that.
If you are looking for a plugin to manage image gallery, you can use attachments plugin,
http://wordpress.org/plugins/attachments/
It keeps the gallery separate and does not put the image gallery shortcodes in post content, thus providing you with full hold over image display in your post/page/custom post. You can also change the order of your images by just drag-n-drop
here is a sample code of how to retrieve your gallery images,
<?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
<?php if( $attachments->exist() ) : ?>
<h3>Attachments</h3>
<p>Total Attachments: <?php echo $attachments->total(); ?></p>
<ul>
<?php while( $attachments->get() ) : ?>
<li>
ID: <?php echo $attachments->id(); ?><br />
Type: <?php echo $attachments->type(); ?><br />
Subtype: <?php echo $attachments->subtype(); ?><br />
URL: <?php echo $attachments->url(); ?><br />
Image: <?php echo $attachments->image( 'thumbnail' ); ?><br />
Source: <?php echo $attachments->src( 'full' ); ?><br />
Size: <?php echo $attachments->filesize(); ?><br />
Title Field: <?php echo $attachments->field( 'title' ); ?><br />
Caption Field: <?php echo $attachments->field( 'caption' ); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

Resources