【问题标题】:get_permalink returns to attachment url in wordpressget_permalink 返回到 wordpress 中的附件 url
【发布时间】:2018-04-03 14:35:35
【问题描述】:
$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,而不是父帖子。 &lt;?php echo get_permalink($post-&gt;post_parent); ?&gt;

【问题讨论】:

  • 你能在你创建链接的地方发布 HTML + PHP 代码吗?
  • 实际上 WordPress 没有这样做,您的浏览器没有直接打开图像 URL。解决方案是您可以添加一些 css 功能,如弹出库等。
  • wordpress 做得很好,工作正常@Sunil Dora 唯一的问题是我不能回到所有图片所在的单个帖子。
  • @Alessandro 更新的是image.php的循环,所以我没有包含get_footer或header的所有代码
  • 谁能帮忙?

标签: php wordpress attachment


【解决方案1】:

解决方法很简单:

在自定义帖子类型注册功能中添加了新标签。

'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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 2016-05-10
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多