get_permalink not rendering correctly
-
I’m using a custom post type of “story” and pulling a random one to the home page (front-page.php).
My WP_Query() is pulling all the other information correctly except for get_permalink(). This is just printing “http://domain.com/story/%story%/” instead of say “http://domain.com/story/vanna-white/”.
Strangely it IS displaying properly in the admin on the actual post. I can click “View Story” next to it and display the page correctly.
-
Hey Justin,
Does it give you the same output if you switch to using “the_permalink()” approach?
Thanks for the reply! It does… prints exactly the same issue
Could you share the code you’re using to display the content on Front Page as well as the code used to create the custom post type?
Custom post creation:
/** * Custom Post Type: Stories * */ add_action('init', 'rr_story_register', 0); // Register custom post type function rr_story_register() { $labels = array( 'name' => _x('Client Stories', 'post type general name'), 'singular_name' => _x('Story', 'post type singular name'), 'all_items' => 'All Stories', 'add_new' => _x('Add New', 'story'), 'add_new_item' => __('Add New Story'), 'edit_item' => __('Edit Story'), 'new_item' => __('New Story'), 'view_item' => __('View Story'), 'search_items' => __('Search Stories'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( "slug" => "story"), 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => 9, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions') ); register_post_type( 'story' , $args ); //flush_rewrite_rules( ); }and the Front Page display:
if ( ! function_exists( 'display_random_story_post' ) ) : /** * Displays latest blog post on the home page. * First post will show excerpt/phone, rest will just be a list. * * @return null */ function display_random_story_post() { // Setup the Query $args = array ( 'post_type' => 'story', 'posts_per_page' => 1, 'orderby' => 'rand' ); $rand_story = new WP_Query( $args ); // Setup var $display = null; //foreach ( $rand_story as $story ) : setup_postdata( $story ); if($rand_story->have_posts()) : while($rand_story->have_posts()) : $rand_story->the_post(); $client_name = get_post_meta( get_the_ID(), 'rr_story_name', true); $client_location = get_post_meta( get_the_ID(), 'rr_story_location', true); $page_headline = get_post_meta( get_the_ID(), 'rr_story_headline', true); $use_alt_headline = 1; if ($page_headline == '') { $use_alt_headline = 0; $page_headline = get_the_title(); } $display = '<article id="post-' .get_the_ID(). '" class="latest-post">' . "\r\n"; $display .= '<header>' . "\r\n"; $display .= '<h2><a href="' .get_permalink(). '">' .$page_headline. '</a></h2>' . "\r\n"; if( $use_alt_headline == 1 && $client_location !== '') { $display .= '<div class="entry-meta"><p>' .$client_name. ', ' .$client_location. '</p></div>'; } elseif( $use_alt_headline == 1 ) { $display .= '<div class="entry-meta"><p>' .$client_name. '</p></div>'; } else { $display .= '<div class="entry-meta"><p>' .$client_location. '</p></div>'; } $display .= '</header>' . "\r\n"; $display .= return_excerpt(280); $display .= '</article><!-- END #post-' .get_the_ID(). ' -->' . "\r\n"; endwhile; // End post display loop else: // If there are no posts, say so $display .= '<p>We're sorry, there are no stories to display.</p>'; endif; // End loop to get blog posts echo $display; wp_reset_postdata(); } // END display_random_story_post() endif;What happens when you change the query to pull
'post_type' => 'post'or'post_type' => 'page'? Does it still cause the same url issue?Hmm, looks like those pull correctly. Out of curiosity I changed to one of the two other custom post types I’m using… the “events” one worked ok, but not another using the basic method of custom post permalinks.
The one that’s working is “Events” and it set to rewrite => false, but then I have a custom function I’d found someplace, that’s making it use ID. I’m guessing this is disrupting the rest of the custom posts’ rewriting…
add_action('init', 'rr_event_rewrite'); function rr_event_rewrite() { global $wp_rewrite; $queryarg = 'post_type=event&p='; $wp_rewrite->add_rewrite_tag('%event_id%', '([^/]+)',$queryarg); $wp_rewrite->add_permastruct('event', '/event/%event_id%', false); //$wp_rewrite->flush_rules(); } add_filter('post_type_link', 'rr_event_permalink', 1, 3); function rr_event_permalink($post_link, $id = 0, $leavename) { global $wp_rewrite; $post = get_post($id); if ( is_wp_error( $post ) ) return $post; $newlink = $wp_rewrite->get_extra_permastruct($post->post_type); $newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink); $newlink = home_url(user_trailingslashit($newlink)); return $newlink; }Don’t know enough about $wp_rewrite to trouble shoot, but I’m guessing it doesn’t play well with others.
Thanks so much for all the help up to this point, I really appreciate it!
I’m just bailing and using a plugin
(http://wordpress.org/support/plugin/wp-permastructure)
Working now well enough now, thanks though!
The topic ‘get_permalink not rendering correctly’ is closed to new replies.