Answers:
您是在说Pages对吗?没有帖子。
这是您要找的东西吗?
get_permalink( get_page_by_path( 'map' ) )
get_permalink( get_page_by_title( 'Map' ) )
home_url( '/map/' )
get_page_by_path()
返回所有页面信息的数组。get_permalink()
将页面ID作为第一个参数。我以为我必须显式传递ID值。
我认为这可能更好:
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, $post_type ) );
if ( $page )
return get_page($page, $output);
return null;
}
遵循get_page_by_title
wordpress的“原始”模式。(第3173行)
rgds
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { global $wpdb; $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) ); if ( $page ) return get_page($page, $output); return null; }
\WP_Post
从其中创建一个实例,该实例将直接在所有检查其他值的wordpress函数中解析。\WP_Post
还提供直接找到有关帖子的最相关数据的方法。
这是Tom McFarlin 在他的博客上发布的一种方法:
/**
* Returns the permalink for a page based on the incoming slug.
*
* @param string $slug The slug of the page to which we're going to link.
* @return string The permalink of the page
* @since 1.0
*/
function wpse_4999_get_permalink_by_slug( $slug, $post_type = '' ) {
// Initialize the permalink value
$permalink = null;
// Build the arguments for WP_Query
$args = array(
'name' => $slug,
'max_num_posts' => 1
);
// If the optional argument is set, add it to the arguments array
if( '' != $post_type ) {
$args = array_merge( $args, array( 'post_type' => $post_type ) );
}
// Run the query (and reset it)
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$query->the_post();
$permalink = get_permalink( get_the_ID() );
wp_reset_postdata();
}
return $permalink;
}
它适用于自定义帖子类型和内置帖子类型(例如post
和page
)。
接受的答案是错误的,因为层次页面无法像这样工作。简而言之,该信息并不总是页面或帖子的路径。例如,您的页面上有一个孩子,等等。路径将是parent-slug/child-slug
并且get_page_by_path
将无法找到child-slug
这种方式。正确的解决方案是这样的:
function mycoolprefix_post_by_slug($the_slug, $post_type = "page"){
$args = array(
'name' => $the_slug,
'post_type' => $post_type,
'post_status' => 'publish',
'numberposts' => 1
);
$my_page = get_posts($args)[0];
return $my_page;
}
<a href="<?php echo mycoolprefix_post_by_slug('map'); ?>">Map</a>
尝试这个:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>
get_page_by_path( 'path' )
返回页面/帖子对象,get_page_link()
当接受帖子/页面对象并返回固定链接时,可以使用该对象。
function theme_get_permalink_by_title( $title ) {
// Initialize the permalink value
$permalink = null;
// Try to get the page by the incoming title
$page = get_page_by_title( strtolower( $title ) );
// If the page exists, then let's get its permalink
if( null != $page ) {
$permalink = get_permalink( $page->ID );
} // end if
return $permalink;
} // end theme_get_permalink_by_title
通过使用此功能
if( null == theme_get_permalink_by_title( 'Register For This Site' ) ) {
// The permalink doesn't exist, so handle this however you best see fit.
} else {
// The page exists, so do what you need to do.
} // end if/else
get_permalink(get_page_by_path('contact')->ID));
吗