admin管理员组文章数量:1328037
I have a widget that places links to pdf attachments in the sidebar. I want to use the pdf attachment text heading (that is used to display it in the parent post) as the title. I cannot get this to work. see below. $img_title is what I DON'T want, as this shows the title that is assigned through the media library, and $post_title is showing the name of the main page (on which I'm viewing the sidebar), which seems very wrong, and makes me wonder if there's some larger issue here. Any help would be appreciated. Here is what is in the actual post to show you what title I want to show up in sidebar.
a href="http://localhost/mytestsite/wp-content/uploads/2020/07/test-Copy-6.pdf">I want this to show as title</a
$attachments = new WP_Query(
array(
'post_type' => 'attachment',
'numberposts' => null,
'posts_per_page' =>$number,
'post_status' => 'any',
'author' =>$authorID,
'post_parent' =>$post->ID
),
$instance
);
if ( ! $attachments->have_posts()) {
return;
} ?>
<?php echo $args['before_widget']; ?>
<?php
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?>
<ul id="attachments">
<?php foreach ( $attachments->posts as $attachment ) {
setup_postdata($attachment);
$post_title = get_the_title($post_parent->ID); // retrieves title of main page
$img_title = apply_filters( 'the_title', $attachment->post_title ); //title from media librar
if ($attachment ->post_mime_type != 'image/jpeg') {
$filetipe = $attachment->post_mime_type;
$signs = array(".", ",", "-", "application", "/");
$filetype = str_replace($signs, "", $filetipe);
?>
<li class="<?php echo $filetype; ?>"><a href="<?php echo wp_get_attachment_url($attachment->ID);
?>" target="blank"><?php echo $img_title . $post_title; ?></a></li>
<?php } } ?>
</ul>
本文标签: loopdisplay post pdf attachments in side bar with link text
版权声明:本文标题:loop - display post pdf attachments in side bar with link text 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237773a2438391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论