• Resolved dunkkan

    (@dunkkan)


    Hello,

    I’ve been reading the codex (get_children function) and trying some combinations but with no luck. Does anyone knows how to retrieve the audio files of certain category (say “podcasts”) in the sidebar ?

    I tried this but it throws me an error related to the foreach argument :

    <?php
    			$attachments =& get_children( array(
    			'category_name' => 'podcasts',
    			'numberposts' => '8',
    			'post_type' => 'attachment',
    			'post_mime_type' => 'audio',
    			'post_status' => null,
    			'post_parent' => null, // any parent
    			'output' => 'object',
    		) );
    			 foreach($attachments as $attach):
    			    echo wp_get_attachment_link($attach);
    ?>
    <?php endforeach; ?>

    Thanks for any help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter dunkkan

    (@dunkkan)

    anyone ? Still fighting that 😛

    Thread Starter dunkkan

    (@dunkkan)

    Hello, I made some progress with this code found in the Codex :

    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => 1,
    	'post_status' => null,
    	'post_parent' => null, // any parent
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $post) {
    		setup_postdata($post);
    		the_title();
    		the_attachment_link($post->ID, false);
    	}
    }
    
    ?>

    With it, I got the link to the last uploaded attachment, anywhere in my template.

    But apparently it doesn’t allow to specify the desired category (I tried to set category and category_name in the array but then nothing appears), and it will take any last attachment :/

    Any ideas ? Thanks.

    Thread Starter dunkkan

    (@dunkkan)

    I got it working, thanks to this thread.

    We need to paste that in functions.php :

    <?php function postfile() {
    	if ( $files = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'order' => 'ASC',
    		'orderby' => 'ID',
    		'post_mime_type' => 'application',)))
    	{
    		foreach( $files as $file ) {
    			$attachmenturl=wp_get_attachment_url($file->ID);
    			$file_title = $file->post_title;
    			echo '<a href="'.$attachmenturl.'">'.$file_title.'</a>';
    		}
    	} else {
    		echo "No podcasts found";
    	}
    }
    ?>

    You can remove the php statements if you have them already there.

    Then, anywhere in the desired template, for instance in sidebar.php, we write a get_posts call :

    <?php
    	 $posts = get_posts('category_name=podcasts&numberposts=8');
    	 foreach($posts as $post):
    	    setup_postdata($post);
    	?>
    	<?php the_title(); ?> | <?php postfile(); ?>
    	<?php endforeach; ?>

    That will give something like the 8 last posts in the “podcasts” category, displayed as title + link to play the podcast.

    Hope that helps.

    Thread Starter dunkkan

    (@dunkkan)

    Sorry for the delay if someone tried that with no succes, there are obviously several issues.

    First you must specify the post_mime_type. For example, to audio/mpeg or video.

    But when I do it, nothing is shown.

    If I leave the space blank, the title of the eventual images of that post come together with the special attachements. How to avoid it ?

    If I clear the image’s title in the Media Manager, then WordPress takes the file name and it gets printed anyway.

    Thanks to anyone who can provide some light over this

    Thread Starter dunkkan

    (@dunkkan)

    Wops, sorry again, yes it works.

    I was just idioticly forgetting to put a title to my audio attachements. Anyway, you must specify what kind of stuff you’ll be retrieving, in that case audio/mpeg, which I forgot to mention earlier.

    Sorry again

    Does it work to insert Audio to Template/sidebar?

    Thread Starter dunkkan

    (@dunkkan)

    Hi MyRivne, yes it works. The only thing is you must UPLOAD via the Editor the audio stuff (and, to do that, you must probably change in one of the server files, called php.ini, the uploading power, because an mp3 is usually a lot bigger than normal images).

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘audio files in sidebar’ is closed to new replies.