admin管理员组文章数量:1289909
I am working on a website with some custom PHP.
The post uses "Custom Fields", where the custom field "pdf_name" is added to the post when there is a PDF document to attach to it. The value would be the URL to that PDF.
If there is no PDF, this custom field is not used and thus gives no (URL) value.
To use this, I go into "single.php" to add a function like so:
<?php
$pdf_name = get_post_meta($post->ID, 'pdf_name', true);
?>
Then I add the HTML:
<a class="pdfDownload" href="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo $pdf_name; ?>">PDF Download</a>
The HTML text link, "PDF Download", will appear on all post displays, whether or not there is a PDF linked to it. This results in a 404 page if someone clicks the text link that has no PDF URL returned to it.
Request:
I want to hide this PDF text link if the $pdf_name
function finds no PDF URL to use.
In other words, it finds and returns as no value.
or...
if it is easier, when there is no PDF URL value, replace the HREF HTML with a basic non-linked text like "No PDF Available".
Note: I am using a Child theme, so the theme single.php file is duplicated there.
Can anyone help with this?
Thanks
I am working on a website with some custom PHP.
The post uses "Custom Fields", where the custom field "pdf_name" is added to the post when there is a PDF document to attach to it. The value would be the URL to that PDF.
If there is no PDF, this custom field is not used and thus gives no (URL) value.
To use this, I go into "single.php" to add a function like so:
<?php
$pdf_name = get_post_meta($post->ID, 'pdf_name', true);
?>
Then I add the HTML:
<a class="pdfDownload" href="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo $pdf_name; ?>">PDF Download</a>
The HTML text link, "PDF Download", will appear on all post displays, whether or not there is a PDF linked to it. This results in a 404 page if someone clicks the text link that has no PDF URL returned to it.
Request:
I want to hide this PDF text link if the $pdf_name
function finds no PDF URL to use.
In other words, it finds and returns as no value.
or...
if it is easier, when there is no PDF URL value, replace the HREF HTML with a basic non-linked text like "No PDF Available".
Note: I am using a Child theme, so the theme single.php file is duplicated there.
Can anyone help with this?
Thanks
1 Answer
Reset to default 0If the custom field will be always empty when there is no pdf file, you can check if it is not empty before displaying the link:
<?php
$pdf_name = get_post_meta($post->ID, 'pdf_name', true);
if($pdf_name) {
?>
<a class="pdfDownload" href="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo $pdf_name; ?>">PDF Download</a>
<?php
}
?>
But, if there is a possibility that the custom field can hold a value even if there is no actual pdf file attached, then you can check if the pdf file actually exists by its path before displaying the download URL:
<?php
$pdf_name = get_post_meta($post->ID, 'pdf_name', true);
$uploads_dir = wp_upload_dir();
$pdf_path = $uploads_dir['basedir'] . '/' . $pdf_name;
if(is_file($pdf_path)) { ?>
<a class="pdfDownload" href="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo $pdf_name; ?>">PDF Download</a>
<?php
}
?>
本文标签: post metaif getpostmeta function returns emptyDo Not Display HTML
版权声明:本文标题:post meta - if get_post_meta function returns empty - Do Not Display HTML 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741446257a2379220.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论