I have a (Roots/Sage-based) theme with a home.php
template override and on that page am displaying excerpts and featured image for each post.
<?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="https://wordpress.stackexchange.com/questions/188719/<?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; ?>
Seems relatively simple and is working, but it was just by trial and error that I came up with generating the link via:
get_post_permalink(the_permalink())
And since there are so many wrong ways to do php (and wp), I’d love some feedback.
2 Answers
As suggested, went to the function reference and from there to the Source File (located in wp_includes/link-template.php
) in which there are four functions that each return similar results.
<?php echo get_post_permalink() ?>
http://newdep.localhost/instruments/jester/
<?php echo post_permalink() ?>
http://newdep.localhost/instruments/jester/
<?php the_permalink() ?>
/instruments/jester/
<?php echo get_the_permalink() ?>
http://newdep.localhost/instruments/jester/
In this case, since this is a custom post type, the function which the docs describe as being designed for it, is get_post_permalink()
, which, as most of of the get_*()
functions returns, rather than showing a result, needs to be 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 );
}