wp_get_attachment_url not working - wordpress

I have a template file which shows the posts of a category called downloads. for each posts i have attached a pdf file. I have given a link to download the pdf file on the page. But when i click the download link it goes to the post page and from there i have to click to download the file. Is there any way to directly download without going to the post. ? I have tried using wp_get_attachment_url as the hyper-reference.but it is not working.The code that i have used is below:
<?php /*
Template Name: Downloads Template
*/
?>
<?php get_header(); ?>
<?php
$recent = new WP_Query("cat=7&orderby=title&order=ASC");
while($recent->have_posts()):$recent->the_post();
$desc_values = get_post_custom_values("description");
?>
<div id="download_featured_image" class="<?php the_ID(); ?> download_image_title_desc">
<a href="<?php the_permalink() ?>" rel="title">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<a href = "" > <?php if ( is_user_logged_in() ) {
echo "Download";
}?></a>
<a href=" http://localhost/wordpress/login.php"> <?php if( !(is_user_logged_in()) )
{
echo "Please signup/login to download this file";
}
?>
</a>
<div id="Download_post_description">
<?php
if( is_array( $desc_values ) )
{
foreach($desc_values as $key => $value );
echo "$value</n>";
}
?>
</div>
</div>
<?php endwhile ?>
<?php get_footer(); ?>
I want to give the link to the uploaded pdf in the href which i have left blank. Can someone help me?

Add the PDF attachment ID as a custom field value, for example attached_pdf_id.
Get the URL using wp_get_attachment_url()
-
<?php
if ( is_user_logged_in() ) {
$pdf_link = wp_get_attachment_url( get_post_meta( get_the_ID(), 'attached_pdf_id', true ) );
if ( $pdf_link ) {
?><a href = "<?php echo $pdf_link ?>" >Download</a><?php
} else {
?>Sorry, no link available. Please contact the webmaser.<?php
}
}
?>

Related

If Data not exists - show (custom text)

I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>

Rendering an image on a WooCommerce product page

Using the Advanced Custom Fields plugin, I am trying to render an image on a single product page in WooCommerce.
I found this snippet of code from someone who is doing the same thing, but is rendering a text field instead. How would I change this to render an image?
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
function woocommerce_template_top_category_desc (){
$terms = get_the_terms( $post->ID, 'wc-attibute-class' );
if ( !empty($terms)) {
$term = array_pop($terms);
$text= get_field('txt-field', $term);
if (!empty($text)) {
echo $text;
}
}
}
So far I have this, but it's not working. The add action is correct because I was using it for something else, but starting at $terms is where I get lost and is obviously not right.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
$terms = get_the_terms( $post->ID, '496' );
if ( !empty($terms)) {
$term = array_pop($terms);
$image = get_field('banner_feedback', $term);
if (!empty($image)) {
echo $image;
}
}
}
Figured it out.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
if( is_single( pageidhere ) ) {
echo '<br><img src="urlhere">';
}
}
Steps to add custom gallery below featured image in woocommerce single product page(you can modify suitably to add just a single image):
Create an ACF for Image Gallery.
Copy the template for product-image.php from /plugins/woocommerce/templates/single-product to themes/your-child-theme/woocommerce/single-product/
Add the following code to product-image.php just before the closing div of
"images"
Add the images you want in the gallery to your single product admin page in the ACF custom field.
`
<?php $images = get_field('product_image_gallery'); ?>
<div class="mycustom_image_gallery">
<?php if($images): ?>
<div class="popup-gallery">
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image['url']; ?>"
class="lightbox-link"
title="<?php echo $image['caption']; ?>"
data-description="<?php echo $image['description']; ?>">
<div class="image-wrap">
<img src="<?php echo $image['sizes']['thumbnail']; ?>">
</div>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
`

Wordpress is_page_template query

i use a Plug-in on my wordpress site which uses a query to show search results, thats the code of the query:
if ( $query->have_posts() )
{
?>
<?php echo $query->found_posts; ?> Ergebnisse gefunden<br />
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<?php if ( has_post_thumbnail() ) { echo '<p>'; the_post_thumbnail("small"); echo '</p>'; } ?>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Previous | Next
<?php
}
else
{
echo "No Results Found";
}
The search should only display results from pages who uses a specific page template (mytemplate.php)
I found the Docs http://codex.wordpress.org/Function_Reference/is_page_template who explain this, but i dont get it to work inside the above query.
any help would be nice :) thx
Please do as below.
if ( is_page_template( '{enter the name of page template with extension}' ) ) {
// Enter your if code here
} else {
// Enter your else code here.
}
For this code to work you must select the page template option while creating the page in WordPress Admin Interface.

Removing the featured image from the post but leaving it on the index page

I have the following blog post index page. It loads a featured image before the text of the post.
On the post itself - I would like for the featured image to not load. I believe this involves editing this content.php file (attached here) but I cannot figure out how to do it such that the image loads only on the index page and not on the post page. Any help would be appreciated.
Add the below code in place of the_post_thumbnail( 'serene-featured-image' ); at line no 24
Replace line no 24 with below code:
<?php
if ( ! is_single() ) :
the_post_thumbnail( 'serene-featured-image' );
endif;
?>
Comment out this code in your single.php (Deatil page of the Blog post)
<div class="main-image<?php if ( '' == get_the_post_thumbnail() && 'video' === get_post_format() ) echo ' et_video_only'; ?>">
<?php if ( 'video' === get_post_format() && false !== ( $first_video = et_get_first_video() ) ) : ?>
<div class="et-video-container">
<?php echo $first_video; ?>
</div>
<?php endif; ?>
<?php if ( ! is_single() || ! 'video' !== get_post_format() ) : ?>
<a href="<?php the_permalink(); ?>" class="et-main-image-link">
<?php endif; ?>
<?php the_post_thumbnail( 'serene-featured-image' ); ?>
<?php serene_post_meta_info(); ?>
<?php if ( isset( $first_video ) && $first_video ) : ?>
<span class="et-play-video"></span>
<?php endif; ?>
<?php if ( ! is_single() || ! 'video' !== get_post_format() ) : ?>
</a>
<?php endif; ?>
</div> <!-- .main-image -->
You have to find all the occurrences of function the_post_thumbnail and remove them.
Interesting files are:
single.php
page.php
content.php
content-gallery.php
content-audio.php
functions.php
Also check for get_the_post_thumbnail

random comments on page wordpress

my question is, how can i show random comments on a page on wordpress? in my website i got a wp page where people leaves lots of comments, i want them to show randomly and not assorted by date time, here's the code of the paginated comments, what should i do? thanks :)
<?php foreach ($comments as $comment) : ?>
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
<div class="paginated-comments-number" style="float: left; color: #999; width: 30px; text-align: left;"></div>
<?php
if ( function_exists('get_avatar') )
echo get_avatar( $comment, 48 );
?>
<cite><?php comment_author_link() ?>
</cite>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?><br />
<small class="commentmetadata"><?php comment_date('F jS, Y') ?> at <?php comment_time() ?> <?php edit_comment_link('edit',' ',''); ?></small>
<?php comment_text() ?>
</li>
<?php
/* Changes every other comment to a different class */
$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<p>
<!-- Start Paginated Comments Pages -->
<?php if ( Paginated_Comments_have_pages() ) : ?>
</p>
<p>
<?php endif; ?>
<!-- End Paginated Comments Pages -->
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
Here's a suggestion for a shortcode function that does what you're asking for:
add_shortcode( 'randomComment', 'randomComment_handler' );
function randomComment_handler($post_id) {
extract( shortcode_atts( array(
'post_id' => '0',
), $atts ) );
$out = "";
$comments = get_comments("post_id=$post_id&status=approve");
if ($comments) {
$ndx = mt_rand(0,sizeof($comments)) - 1;
$comment = $comments[$ndx];
$out = "<div class='randomComment'><div class='randomAuthor'>".$comment->comment_author."</div><div class='randomText'>".$comment->comment_content."</div></div>";
}
return $out;
}
This goes into your functions.php, and lets you put the shortcode on any page or post to show a random comment:
[randomComment post_id="1337"]
Just change the post_id according to the post you want to pull the random comment from.

Resources