0

I am trying to emulate the appearance of multiple subfolders of files in the installations root directory, but these folder names will conflict with pages that need to be the same name.

In other words, I want to media files to appear as if they were in the same folder as the page as if it the page URL was a real folder.

Example:

http://example.com/courtcase/ would be the page for a court case and http://example.com/courtcase/testimony.pdf would be a document referenced in it

I COULD change the upload directory to the install root and then subfolders using a plugin that allows organizing media files this way. HOWEVER, calling on the page slug will cause the server to open the file directory for the actual folder rather than the page in the WP database.

So all I would need is to have the media files in another directory than the install to eliminate conflicts, and then have slugs for files just start at the install directory path.

Example: http://example.com/wp-content/uploads/courtcase/testimony.pdf would become http://example.com/courtcase/testimony.pdf

And of course I would need this done for all media files, not manually adding redictects.

{NOTE: This is a client requirement. Yes, it's crazy to be so concerned with URLS, but that's the demand for the project and they will not be swayed to accept any other result.}

  • Are you aware that an upload creates a post of type attachment with its own URL, comments, post meta, and template? Note that the WP rewrite URLs system isn't used for uploads, so there would be a non-trivial performance cost to do this, especially for large files which may trigger the execution time limit. Tell your client that what they ask is nontrivial and make sure they pay the extra for such an unusual ask – Tom J Nowell Apr 28 '18 at 23:16
0

if you change permalink to post name in setting and attach the testimony.pdf in your post, you will get http://example.com/your_post/testimony.pdf, to get url of the textimony u can get with this code

    <?php

$args = array(
    'post_type'   => 'attachment',
    'numberposts' => -1,
    'post_status' => 'any',
    'post_parent' => $post->ID,
    'exclude'     => get_post_thumbnail_id(),
);

$attachments = get_posts( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title', $attachment->post_title );
        the_attachment_link( $attachment->ID, false );
    }
}

?>

or put this code in single.php

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
//add in bootom of loops
 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           the_attachment_link( $attachment->ID, true );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

 endwhile; endif; ?>
</ul>

the code return http://wp.example.net/post_name/attachment_name. this is the reference https://codex.wordpress.org/Function_Reference/get_attachment_link

  • I cannot create a situation where it creates the URL based on the post inherently. However, I actually solved the problem with a combination of plugins, as I avoid hacking the theme files directly. Thanks, though. I may try this if for any reason my other fix doesn't hold. – Ken JP Stuczynski Apr 30 '18 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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