在循环中获取固定链接

时间:2015-05-19 作者:MikeiLL

我有一个(Roots/Sage-based) 主题带有home.php 模板覆盖,并在该页面上显示每篇文章的摘录和特色图片。

<?php while (have_posts()) : the_post(); ?>  
  <div class=".container-fluid">
    <?php if (get_post_type() == \'instruments\') {?>
    <div class="col-md-3">
        <h1><?php the_ID(); ?></h1>
        <a href="<?php get_post_permalink(the_permalink()) ?>"><?php the_post_thumbnail( \'medium\' ); ?></a>
        <?php get_template_part(\'templates/content\', get_post_type() != \'post\' ? get_post_type() : get_post_format()); ?>
    </div>
  </div>
<?php } ?>

<?php endwhile; ?>
看起来相对简单,并且正在工作,但我只是通过尝试和错误才想到通过以下方式生成链接:

 get_post_permalink(the_permalink())
既然有那么多错误的方法来做php(和wp),我希望得到一些反馈。

2 个回复
最合适的回答,由SO网友:MikeiLL 整理而成

按照建议,前往function reference 从那里到Source File (位于wp_includes/link-template.php) 其中有四个函数,每个函数返回相似的结果。

    <?php echo get_post_permalink() ?>

http://newdep.localhost/instruments/jester/

    <?php echo post_permalink() ?>

http://newdep.localhost/instruments/jester/

    <?php the_permalink() ?>
/乐器/小丑/

    <?php echo get_the_permalink() ?>

http://newdep.localhost/instruments/jester/

在这种情况下,由于这是一种自定义的post类型,文档描述为为其设计的功能是get_post_permalink(), 其中get_*() 函数返回结果,而不是显示结果,需要echo\'D

/**
 * Retrieve the permalink for a post with a custom post type.
 *
 * @since 3.0.0
 *
 * @param int $id Optional. Post ID.
 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
 * @return string The post permalink.
 */
function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
    global $wp_rewrite;

    $post = get_post($id);

    if ( is_wp_error( $post ) )
        return $post;

    $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);

    $slug = $post->post_name;

    $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( \'draft\', \'pending\', \'auto-draft\', \'future\' ) );

    $post_type = get_post_type_object($post->post_type);

    if ( $post_type->hierarchical ) {
        $slug = get_page_uri( $id );
    }

    if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
        if ( ! $leavename ) {
            $post_link = str_replace("%$post->post_type%", $slug, $post_link);
        }
        $post_link = home_url( user_trailingslashit($post_link) );
    } else {
        if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
            $post_link = add_query_arg($post_type->query_var, $slug, \'\');
        else
            $post_link = add_query_arg(array(\'post_type\' => $post->post_type, \'p\' => $post->ID), \'\');
        $post_link = home_url($post_link);
    }

    /**
     * Filter the permalink for a post with a custom post type.
     *
     * @since 3.0.0
     *
     * @param string  $post_link The post\'s permalink.
     * @param WP_Post $post      The post in question.
     * @param bool    $leavename Whether to keep the post name.
     * @param bool    $sample    Is it a sample permalink.
     */
    return apply_filters( \'post_type_link\', $post_link, $post, $leavename, $sample );
}

SO网友:Tarei SuperChur King

正如Nilambar所提到的,正确的函数是<?php the_permalink(); ?>.

但是,如果您使用的是试错法,请尝试WordPress Codex,尤其是the_permalink 有关更多信息,请参阅示例和相关函数。

结束

相关推荐

Loop for sticky posts

我用过Justin Tadlock\'s 关于如何创建仅包含粘性帖子的循环的教程。代码大致如下所示:$sticky = get_option( \'sticky_posts\' ); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 2 ); query_posts( array( \'post__in\' => $sticky, \'caller_get_posts\' => 1 ) ); 根据教程,我