如何使用页面头
7 个回答
- 投票数
-
- 2013-06-13
使用
get_page_by_path($page_path)
:$page = get_page_by_path( 'about' ); echo get_the_title( $page );
这将返回常规的post对象.
文档:
https://developer.wordpress.org/reference/functions/get_page_by_path/
https://developer.wordpress.org/reference/functions/get_the_title/Use
get_page_by_path($page_path)
:$page = get_page_by_path( 'about' ); echo get_the_title( $page );
This will return a regular post object.
Documentation:
https://developer.wordpress.org/reference/functions/get_page_by_path/
https://developer.wordpress.org/reference/functions/get_the_title/-
如果我想要的是子页面的ID,该怎么办?What if it's a child page's id I want?
- 0
- 2013-06-13
- freaky
-
@freaky该函数仅接收页面信息,而不接收父信息.由于是唯一的,因此您始终只能得到一页.@freaky The function takes just the page slug, not the parent slug. Since slugs are unique, you will always get just one page.
- 2
- 2013-06-13
- fuxia
-
谢谢,它正在工作,对于子页面,我必须像点击" $page=get_page_by_path('about/child');"一样进行导航.Thank you it is working and for child page I had to navigate like hits `$page = get_page_by_path( 'about/child' );`
- 3
- 2013-06-13
- freaky
-
为了澄清起见,`get_page_by_path`内部使用了`post_name`字段,而不是`post_slug`.Just to clarify, `get_page_by_path` uses the `post_name` field internally, not `post_slug`.
- 0
- 2018-04-09
- colefner
-
为了清楚起见,这使用页面路径而不是页面名称,对吗?然后一个名为"关于我们"的页面的参数应该是"关于我们",对吗?没有开头或结尾的斜线?Just to be clear, this uses the page path and not the page name, correct? Then a page named "About us" the argument should be "about-us", correct? with no beginning or trailing slashes?
- 0
- 2018-07-25
- user658182
-
是的,@ user658182Yes, @user658182
- 0
- 2018-07-25
- fuxia
-
- 2015-03-13
我一直在用这个..
function get_id_by_slug($page_slug) { $page = get_page_by_path($page_slug); if ($page) { return $page->ID; } else { return null; } }
希望这会对某人有所帮助.
I've been using this ..
function get_id_by_slug($page_slug) { $page = get_page_by_path($page_slug); if ($page) { return $page->ID; } else { return null; } }
Hope this will help someone.
-
为什么将其包装在函数中?`get_page_by_path`已经返回null.Why wrapping it in a function? `get_page_by_path` already returns null …
- 0
- 2019-03-01
- GDY
-
因为OP问题要返回ID,而不是页面对象.Because the OP question wants to return ID, not the page object.
- 0
- 2019-10-16
- user1158023
-
- 2013-06-13
已经在此论坛上对其进行了询问和回答.我从那里粘贴相同的代码. 使用此功能来检索页面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 AND post_status = 'publish'", $page_slug, $post_type ) ); if ( $page ) return get_post($page, $output); return null; }
It has been already asked and answered on this forum. I am pasting the same code from there. Use this function to retrieve page 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 AND post_status = 'publish'", $page_slug, $post_type ) ); if ( $page ) return get_post($page, $output); return null; }
-
- 2016-10-08
当尝试在同一页面中多次使用代码时,选择的答案出现问题.在每种情况下,它都会同时显示我所有页面的内容.因此,我重新思考并根据 WordPress Codex 的文档提出了一种更简单的方法:
<?php $query = new WP_Query( array( 'pagename' => 'about-me' ) ); while ( $query->have_posts() ) { $query->the_post(); echo '<h2>'. get_the_title() .'</h2>'; the_content(); } wp_reset_postdata(); ?>
也许对那里的某人还是有帮助的; D
I had problems with the chosen answer when trying to use the code several times in the same page. It kept on displaying all of my pages content at the same time in every instance. So I went back to thinking and came up with this simpler approach based on the WordPress Codex's documentation:
<?php $query = new WP_Query( array( 'pagename' => 'about-me' ) ); while ( $query->have_posts() ) { $query->the_post(); echo '<h2>'. get_the_title() .'</h2>'; the_content(); } wp_reset_postdata(); ?>
Maybe it can still be helpful for somebody out there ;D
-
- 2019-04-05
许多答案似乎过于复杂,或者没有描述如何专门获取页面ID.
$page = get_page_by_path("your-page-slug"); if ($page) { $page_id = $page->ID; echo $page_id; }
在上面的描述中,我们已将post对象分配给$page-获得post对象后,您可以获得此处描述的任何信息: https://codex.wordpress.org/Class_Reference/WP_Post
$page->ID $page->post_status $page->post_title
还有很多
A lot of answers here that seem overly complex, or don't describe how to get the page ID specifically.
$page = get_page_by_path("your-page-slug"); if ($page) { $page_id = $page->ID; echo $page_id; }
In the above description we've assigned the post object to $page - Once you have the post object you can get any of the info described here: https://codex.wordpress.org/Class_Reference/WP_Post
$page->ID $page->post_status $page->post_title
and a lot more
-
- 2020-08-31
自WordPress v1.0.0起有一个功能 url_to_postid :)使用此功能最容易实现此任务.
当页面是顶层页面时,只需要输入子弹.
例如
url_to_postid('slug');
当页面处于较低的层次结构级别(即它具有父级)时,您必须在父子级之前加上斜杠,如下所示:
url_to_postid('parent-slug/child-slug');
There is a function url_to_postid since WordPress v1.0.0 :) This task is easiest to achieve by using this function.
When page is top-level page, only slug has to be given.
e.g.
url_to_postid('slug');
When the page is in lower hierarchy level (i.e. it has parent) you have to prepend parent slug divided by slash like so:
url_to_postid('parent-slug/child-slug');
-
- 2018-05-31
<?php function get_page_ID_by_slug( $slug ) { $page = get_page_by_path( $slug ); if ( $page ) { return (int) $page->ID; } else { return null; } } ?>
我希望这个建议对某人有帮助.
<?php function get_page_ID_by_slug( $slug ) { $page = get_page_by_path( $slug ); if ( $page ) { return (int) $page->ID; } else { return null; } } ?>
I hope this suggestion is helpful for someone.
我对wordpress来说还很陌生,我想知道是否可以通过
page id
获得slug
.有可能请让我知道.