How to get mp3 or (file) url to current post to be attached to custom button

Please how do I get mp3 file url so that i can attach it to a custom button i made. I was able to get the post title but cant do that for file url.
I want when I click on download button the download starts immediately not taking me to the post.
See my current code and check screenshot too or site tophits.ng

Screenshot_4

Please forgive me if i fail on how to ask question. see my current code below

<?php query_posts('cat=1302&showposts=1'); while (have_posts()) : the_post(); ?>
  <?php the_post_thumbnail(); ?>

<div style="margin-left:80px;"><h3><font style="color: white;"><u><?php the_title(); ?></u></font><br><br>



	
		<button style="float:left; background-color:black; padding: 3px;  color:white; border-radius:5px;"><a style="float:right; background-color:black; color: white;" href="<?php the_permalink(); ?>">VIEW</a></button>
		

	<button style="float:right; background-color:black; padding: 3px;  border-radius:5px;"><a style="float:right; background-color:black; color: white; padding: 3px;  border-radius:5px;"href="<?php the_permalink(); ?>">DOWNLOAD</a></button>
	
	</h3> <script src="https://tophits.ng/wp-content/cache/min/1/e0d56963d9eb9a71278f066774b009c7.js" data-minify="1" defer=""></script><noscript><link data-asynced='1' as='style' onload='this.rel="stylesheet"'  rel="preload" href="https://tophits.ng/wp-content/cache/min/1/31e4b8c01154801046d0c85b7f0cd74e.css" data-minify="1" /><noscript><link rel="stylesheet" href="https://tophits.ng/wp-content/cache/min/1/31e4b8c01154801046d0c85b7f0cd74e.css" data-minify="1" /></noscript></div></li></ul></ul>
				<?php endwhile; ?><?php wp_reset_query(); ?>
				</div>
				
				

<a href="{url to your mp3}">Link</a>

i know exaclty that code you are showing me. What I am asking is a code that can aumatically generate the attachment to the custom download link. I have a code that did similar thing but not working as expected. Check below to see


				 <?php if ( have_posts() ) : while (have_posts()) : the_post();
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID );
                $attachments = get_posts( $args );
                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                      ?>
                         <div class="col-lg-4">
                            <p class="center"> <?php  the_title(); ?></p>
                          <a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>">
                          <div id="post-<?php the_ID(); ?>" class="download-block bold">
                        DOWNLOAD
                    </div>
                </a>
            </div>
                      <?php
                    }
                }
    endwhile; endif; ?>

This outputs all the post link which is not what I want. Check my site tophits.ng to see what I mean. I want when clicked on the “view” the post opens and when clicked on “Download” the file attached to the post downloads immediately. thanks

i don’t see the problem

image

Not really what i am saying. See below screenshot or check homepage tophits.ng Screenshot_5

then you have to change wp_get_attachment_url() to work as expected.

How do i achieve that? when I replace href="<?php the_title(); ?>" with <?php echo wp_get_attachment_url it doesnt work. can you check for me? This is the current code i use how do i modify it

		
		<div class="block_posts" style="background-color: #696969;">	<h2 style="font-size: 15px;
    background: #f5f5f5;
    padding: 5px;
    border-bottom: 1px solid #ddd;"><p style="color: white; text-align: center; border-radius:20px; background-color: green;"><big>MUSIC OF THE WEEK</big></p></h2>	
				<ul class="rpul"><ul><li> 






<?php query_posts('cat=1302&showposts=1'); while (have_posts()) : the_post(); ?>
  <?php the_post_thumbnail(); ?>

<div style="margin-left:80px;"><h3><font style="color: white;"><u><?php the_title(); ?></u></font><br><br>



	
		<button style="float:left; background-color:black; padding: 3px;  color:white; border-radius:5px;"><a style="float:right; background-color:black; color: white;" href="<?php the_permalink(); ?>">VIEW</a></button>
		

	<button style="float:right; background-color:black; padding: 3px;  border-radius:5px;"><a style="float:right; background-color:black; color: white; padding: 3px;  border-radius:5px;"href="<?php the_permalink(); ?>">DOWNLOAD</a></button>
	
	</h3> <script src="https://tophits.ng/wp-content/cache/min/1/e0d56963d9eb9a71278f066774b009c7.js" data-minify="1" defer=""></script><noscript><link data-asynced='1' as='style' onload='this.rel="stylesheet"'  rel="preload" href="https://tophits.ng/wp-content/cache/min/1/31e4b8c01154801046d0c85b7f0cd74e.css" data-minify="1" /><noscript><link rel="stylesheet" href="https://tophits.ng/wp-content/cache/min/1/31e4b8c01154801046d0c85b7f0cd74e.css" data-minify="1" /></noscript></div></li></ul></ul>
				<?php endwhile; ?><?php wp_reset_query(); ?>
				</div>
				
				

you have to find the file that contains function wp_get_attachment_url and look at what it does with the ID you provide, then check if that relates to the attachement you expect, and change the algorithm so it gets the attachements URL.

I started editing this theme from popular carrington mobile theme and I love the design I have come up with but right now this only function is dragging me back. Like I said there was a code I used before which output all the post links with download. It works fine but my problem is that it calls all the post whereas I want only one post being structured that way. see the code i am talking about below ```

<?php if ( have_posts() ) : while (have_posts()) : the_post(); $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { ?>
                     <div class="col-lg-4">
                        <p class="center"> <?php  the_title(); ?></p>
                      <a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>">
                      <div id="post-<?php the_ID(); ?>" class="download-block bold">
                    DOWNLOAD
                </div>
            </a>
        </div>
                  <?php
                }
            }
endwhile; endif; ?>``` I showed you before now. I am trying to see if there is a way I can get it work same way. Let me put the code and show you screenshot for a better understanding.

Screenshot_6 Thats the screenshot. I want the post to be only one if it can be done in a similar way. Thanks

if you already have a working code, you have to determine which post is special and use an appropriate condition that you can build up a plain if-then-else from.

Thanks for your reply. I have currently removed the codes to concentrate on other development on my site. I wanted that for mobile view but I now changed to a premium theme I bought some time in the past. My problem now is that page speed score in search console is making me shed tears. Check my site if you can help www.TopHits.ng

WordPress will always take a hit. You can only make it so fast, because of how it functions.

Sponsor our Newsletter | Privacy Policy | Terms of Service