WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to link to attachments but through its parent page.

/directors/piranha-bar/ shows all of that post's attachments on single.php

and i want /directors/piranha-bar/video/name-or-ID/ to still use single.php but have the variable $video captured.

How can i firstly make the links on the attachments into this format? Then get wordpress to recognise this url, get the variable and go to single.php?

hope that's clear! best, Dc

share|improve this question
There's definitely a way to do this but first can you tell us what happens if you go to the video url currently? Does it find the piranha bar page? – sanchothefat Mar 8 '12 at 11:47
hello sancho, its says Not found. Loading template file 404.php. – daniel Crabbe Mar 8 '12 at 13:28
Ok, will do a few tests and get back to you – sanchothefat Mar 8 '12 at 15:00

Showing links to attachments in the desired format

Loop through the attachments and generate the link you want:

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo '<a href="' . get_permalink() . 'video/' . $attachment->ID . '">' . apply_filters('the_title', $attachment->post_title) . '</a>';

Getting the video variable and using single.php

Sorry it took me a while, there's a very simple way to do this in WordPress using url endpoints.

Paste the following code inside your functions.php:

add_action( 'init', 'video_endpoint' );
function video_endpoint() {
    add_rewrite_endpoint( 'video', EP_PAGES | EP_PERMALINK );

Make sure you refresh your permalinks by going to the admin permalinks page and hitting 'save changes'.

Once you've done that you can access the video parameter in your templates using:

echo get_query_var( 'video' );

NOTE: Above where you see EP_PAGES this is the scope of where the endpoint will work. Some possible values are EP_ALL, EP_ROOT, EP_PERMALINK, EP_CATEGORIES, EP_TAGS and so on.

share|improve this answer
thanks sancho - tried all that but still get not found. Not sure how this will affect the solution but turns out we'll need to add a showreel variable too making it, showreel/ID/video/ID/, thanks for your help so far... – daniel Crabbe Mar 9 '12 at 9:49
Did you definitely flush the rewrite rules? I tested it myself and it works. You may need tweak the endpoint mask a bit though - I've edited my answer to reflect that. – sanchothefat Mar 9 '12 at 12:04
Can you explain how the content is being managed here, eg. how are you splitting things up into showreels and videos in the back end? – sanchothefat Mar 9 '12 at 12:06
yeh flushed them a lot! Directors and showreels are custom post_types. I basically want the extra variable(s) to define what loads/plays first on the directors page. That uses single-directors.php. The site isn't in the root of the domain either - sandbox.modernactivity.co.uk/independent_02 but not sure if that makes a difference as .htaccess is in /independent_02/ – daniel Crabbe Mar 9 '12 at 12:42
mmm - array(EP_PAGES) instead EP_PAGES has made a difference but only works for one variable. Adding one for showreel too gives becomes 222/video/111... – daniel Crabbe Mar 9 '12 at 12:49

in the end i used something like this -

function wpse13483_init() {

    add_rewrite_rule( 'directors/([^/]*)/showreels/([^/]*)/video/([^/]*)/?', 'index.php?post_type=directors&name=$matches[1]&showreel=$matches[2]&video=$matches[3]', 'top' );
    add_rewrite_rule( 'directors/([^/]*)/showreels/([^/]*)/', 'index.php?post_type=directors&name=$matches[1]&showreel=$matches[2]', 'top' );
    add_rewrite_rule( 'directors/([^/]*)/photography/?', 'index.php?post_type=directors&name=$matches[1]&photography=1', 'top' );

add_action( 'init', 'wpse13483_init' );

function wpa52794_query_vars( $vars) {
    $vars[] = 'video';
    $vars[] = 'showreel';
    $vars[] = 'photography';

    return $vars;
add_filter( 'query_vars', 'wpa52794_query_vars' );

then to get the var on the page -

$show_photograpy = get_query_var( 'photography');
$selected_playlist = get_query_var( 'showreel');

Which works well. Only annoying thing is the url structure and links are ignored by xml sitemap plugins so the links with the variables only exist when i create the structure manually..


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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