user1002272
user1002272

Reputation:

Creating a separate page to pass custom fields from parent post

I am having a content sharing site where i share themes and gadgets for windows 7.

I am uploading the file using a custom field and storing its value in a custom field named durl.

Now I want to create a separate page called "Download" and when a user visits the posts page and when he clicks the download button, he must be forwarded to a new page where the download must start.

The new page "Download" must get the custom field value "$durl" from the post.

Is it possible? I have done it in codeigniter but no idea how to achieve this using wordpress.

Please guide me on that.

regards, Rias

Upvotes: 0

Views: 611

Answers (2)

Ahmed Fouad
Ahmed Fouad

Reputation: 3073

Here is another way you can do it.

Create a Downloads template by copying page.php and have this in your new template:

<?php
/*
Template Name: Downloads
*/
?>

Fill the template with header, footer, etc like page.php. Create a new page in WordPress and assign the template "Downloads" to it.

In your original post (single.php) for example, create the download link dynamically like this:

<a href="/download/?fileID=<?php echo $post->ID; ?>">Download File</a>

The user will be redirected to Downloads page which you just created. You need to put this code in your downloads template / page:

<?php

if (isset($_GET['fileID']) && is_numeric($_GET['fileID'])) { // to verify that fileID is passed
      // we now have the post ID in downloads page and can create download link
      $file = get_post_meta($_GET['fileID'], 'durl', true);
}
?>

<a href="<?php echo $file; ?>">Download now</a>

And it should print the custom field value in downloads page (which is the download link related to your post)

The idea of this code is to pass the post ID in $_GET and use the post ID to get the download link via its custom field.

Upvotes: 1

Marty
Marty

Reputation: 4657

simply create a new page template called Downloads. Copy and Paste, all the code from page.php within your theme files, then just above were it says:

<?php get_header();?>

Paste this in section of code, to create your new template file..

<?php
/*
  TEMPLATE NAME: Downloads
*/
?>

Go to pages within the admin area, and if you havent already create a page called downloads, then assign the new template to this page,

within your new template page code,

place this section of code, were you want the download link to appear

<?php 
 if(isset($_POST['durl'])){ 
    $durl = trim($_POST['durl']);
?>
<div id="download_box">
    <a href="<?php echo $durl;?>">click here to download</a>
</div>
<?php } ?>

hopefully this helps.

Marty

Upvotes: 0

Related Questions