[英]get_permalink returns to attachment url in wordpress
$parent_id = $post->post_parent;
echo get_permalink($parent_id);
我有一個自定義類型,在該類型中,我使用默認的wordpress媒體發布照片庫,並在單擊任何圖像時打開image.php(附件URL)。在這里,我試圖添加“返回圖庫”鏈接,但始終返回到實際的附件URL並刷新頁面。
您有什么主意,如何獲得正確的父帖子固定鏈接以返回到帖子中的所有圖像?
我正在嘗試在image.php中創建鏈接
<?php while ( have_posts() ) : the_post();
global $post;
$parent_id = $post->post_parent;
?>
<div style="background: #292929;padding-top:3px;text-align:center;">
<a href="<?php echo wp_get_attachment_url($post->ID); ?>">
<?php echo wp_get_attachment_image( get_the_ID(), 'large' ); ?>
</a>
</div>
<?php //the_content(); ?>
<nav id="image-navigation" class="navigation image-navigation">
<div class="nav-links">
<?php previous_image_link( false, '<div class="previous-image"><i class="fa fa-arrow-left" aria-hidden="true"></i> ' . __( 'Previous Image', 'albano' ) . '</div>' ); ?>
<?php //echo back_to_gallery(); ?>
<?php echo get_permalink($post->post_parent); ?>
<?php next_image_link( false, '<div class="next-image">' . __( 'Next Image', 'albano' ) . ' <i class="fa fa-arrow-right" aria-hidden="true"></i></div>' ); ?>
</div><!-- .nav-links -->
</nav><!-- #image-navigation -->
<div class="clearfix"></div>
<?php endwhile; // end of the loop. ?>
輸出是當前頁面的URL,而不是父帖子。 <?php echo get_permalink($post->post_parent); ?>
解決方案很簡單:
在自定義帖子類型注冊功能中添加了新標簽。
'parent' => __('Parent galleries','albano'),
'parent_item_colon' => __( 'Parent:', 'albano' ),
我使用了以下功能:
function back_to_gallery() {
if ( !is_attachment() && is_singular('gallery') )
return false;
$current_attachment = get_queried_object();
$permalink = get_permalink( $current_attachment->post_parent );
$parent_title = get_post( $current_attachment->post_parent )->post_title;
$link = '<a class="back-to-gallery" href="' . $permalink . '" title="' . $parent_title . '">' . __('Back to gallery', 'albano') . '</a>';
return $link;
}
該函數將使用以下輸出返回父帖子的URL: echo back_to_gallery();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.