是否有可能从子弹中获取页面链接?
6 个回答
- 投票数
-
- 2010-12-07
您是在说Pages对吗?没有帖子.
这是您要寻找的东西
-
get_permalink( get_page_by_path( 'map' ) )
-
get_permalink( get_page_by_title( 'Map' ) )
-
home_url( '/map/' )
You're talking about Pages right? Not Posts.
Is this what you looking for:
get_permalink( get_page_by_path( 'map' ) )
get_permalink( get_page_by_title( 'Map' ) )
home_url( '/map/' )
-
您是说`get_permalink(get_page_by_path('contact')-> ID));`吗?Did you mean `get_permalink(get_page_by_path('contact')->ID));`?
- 4
- 2010-12-07
- Sampson
-
嗯,不是吗?身份证号是什么?hmmm nope? What's with the ID?
- 1
- 2010-12-08
- zeo
-
[`get_page_by_path()`](http://codex.wordpress.org/Function_Reference/get_page_by_path)返回所有页面信息的数组.[`get_permalink()`](http://codex.wordpress.org/Function_Reference/get_permalink)将页面ID作为第一个参数.我以为我必须显式传递ID值.[`get_page_by_path()`](http://codex.wordpress.org/Function_Reference/get_page_by_path) returns an array of all page information. [`get_permalink()`](http://codex.wordpress.org/Function_Reference/get_permalink) takes a page ID as the first argument. I thought I'd have to explicitly pass the ID value.
- 3
- 2010-12-08
- Sampson
-
@Jonathan:并不总是记录在案,但是许多WP函数都接受数字ID和完整的post对象作为参数.@Jonathan: It's not always documented, but many WP functions accept both numeric ID's and full post objects as the argument.
- 10
- 2010-12-08
- Jan Fabry
-
似乎在处理子页面时使用get_page_by_path()可能会非常复杂...It seems that get_page_by_path() can be quite complicated to use when dealing with child pages...
- 1
- 2011-11-14
- Kaaviar
-
同意Kaaviar,以下功能会更好.Agreed with Kaaviar, the function below is better.
- 0
- 2012-08-10
- Rezen
-
我将1用于子页面没有问题.我真的没有看到它的复杂性.I use 1 for my child pages with no issue. I really don't see the complication with it.
- 0
- 2014-12-19
- Jeremy
-
home_url('/map/')和我一起工作home_url( '/map/' ) worked with me
- 0
- 2017-04-15
- Shady Mohamed Sherif
-
错误的答案,请拒绝涅ni.路径与子弹不同.示例:一个页面的父页面名为abc,页面本身的链接为123.现在的路径是" abc/123",子弹是" 123".请删除此错误答案.`get_page_by_path('123')`无效.wrong answer, downvote to nirvana please. The path is not the same like the slug. Example: a page has a parent page called `abc` the page itself has a slug of `123`. The path now is `abc/123` the slug is `123`. Please remove this wrong answer. `get_page_by_path( '123' )` wont work.
- 1
- 2017-09-01
- Toskan
-
在我的情况下,我必须使用第一个,并且运行良好,因为第二个(get_page_by_title)返回的是具有类似(但不相同)子段的另一个帖子(非页面)链接.我不知道为什么会这样.In my case I had to use the first one and it worked fine, because the second one (get_page_by_title) was returning another post (not page) link with similar (but not the same) slug. I don't know why this was happening.
- 1
- 2019-06-10
- Adriana Hernández
-
- 2011-02-28
我认为这样会更好:
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
.(第3173行)rgds
I think this could be better:
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; }
following the pattern of "original"
get_page_by_title
of wordpress. (line 3173)rgds
-
为什么会更好?你可以解释吗?Why would that be better? Can you explain?
- 11
- 2012-04-11
- julien_c
-
最后的评论-我认为sql需要再有一个条件:`functionget_page_by_slug($page_slug,$ output=OBJECT,$post_type='page'){全局$ wpdb;$page=$ wpdb->get_var($ wpdb->prepare("在$ wpdb->posts中选择ID,post_name=%s ANDpost_type=%s ANDpost_status='publish'",$page_slug,$post_type)));如果($page)返回get_page($page,$ output);返回null;}`Last comment - I think that sql needs to have one more condition: `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; }`
-
为什么?它不会仅仅为了获取ID而生成完整的post对象.Why? It doesn't generate a complete post object just to get the ID.
- 0
- 2013-01-09
- s_ha_dum
-
@webcitron我认为这只是因为遵循Wordpress的原始模式通过"标题"发布帖子,只是更改为" slug". (请查看[link](http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/post.php#L3173))@webcitron I think just because is following original pattern of Wordpress getting post by 'title', just changing for 'slug'. (check the [link](http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/post.php#L3173))
- 0
- 2013-05-13
- Matheus Eduardo
-
这是一个很好的答案.这绕过了流氓插件掩盖页面或错误过滤页面的可能性.如果您从发布表中返回ID,则可以从中创建一个\ WP_Post实例,该实例可以直接在所有检查其他值的wordpress函数中解析.\ WP_Post还提供直接找到有关帖子的最相关数据的方法.This is a good answer. This bypasses the possiblity of a rogue plugin masking your page or incorrectly filtering it. If you return the id from the post table, then you can create an instance of `\WP_Post` from it, and that resolves directly in all of the wordpress functions that check for other values. `\WP_Post` also provides methods directly to find most related data about the post.
- 0
- 2018-03-23
- mopsyd
-
- 2013-01-31
这是汤姆·麦克法兰(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
).This is a method published by Tom McFarlin on his blog:
/** * 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; }
It works with custom post types and built-in post types (such as
post
andpage
). -
- 2017-09-01
接受的答案是错误的,因为分层页面无法像这样工作.简而言之,该段并不总是页面或帖子的路径.例如.您的页面上有一个孩子,等等.路径将为
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>
the accepted answer is wrong because hierarchical pages don't work like that. Simply put, the slug is not always the path of the page or post. E.g. your page has a child etc. the path will be
parent-slug/child-slug
andget_page_by_path
will fail to findchild-slug
this way. The proper solution is this: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>
-
- 2018-02-26
尝试一下:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>
get_page_by_path( 'path' )
返回页面/帖子对象,然后get_page_link()
可以使用它,因为它接受帖子/页面对象并返回永久链接.Try This:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>
get_page_by_path( 'path' )
returns page/post object which can be then used byget_page_link()
as it accepts post/page object and returns permalink.-
请** [编辑]您的答案**,并添加说明:**为什么**可以解决问题?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 2
- 2018-02-26
- fuxia
-
- 2014-11-11
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
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
Use this function by
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
是否可以仅从子弹中获取页面的永久链接?我知道您可以使用
从ID中获取页面的永久链接.get_page_link()
:我很好奇是否有任何办法可以处理页面上的所有内容-