WordPress gets page ID method based on page name

Source: Internet
Author: User
WordPress gets page ID method based on page name

When making a corporate theme, we usually need to show the company profile on the homepage, and the company profile is a single page. To get the content of this page is not difficult, the code is as follows:

<?php
$recent = new Wp_query (' page_id=1 ');
while ($recent->have_posts ()): $recent->the_post ();?>
<?php the_content ();?>
<?php Endwhile;?>

This will output the contents of the page with ID 1. However, when the user uses the topic, his company profile page ID is not necessarily 1, so you need to modify the page ID in the source code. Some humanized point of theme provides the theme of background management functions, then the user in the background input corresponding ID number is OK. But for beginners, you may not know how to get the page ID you want.

So let's make the theme a little more personal.

Let the user fill out the page name can output the content of the corresponding page, at this time, we need to according to the user provided by the page name to get the ID of the page to output the user want content. WordPress Gets the page ID code from the page name as follows:

function hui_get_page_id ($pageName) {
Global $wpdb;
$pages = Get_pages ();
for ($i = 0; $i < count ($pages); $i + +) {
if ($pageName = = $pages [$i]->post_title) $page _slug = $pages [$i]->post_name;//get a page alias based on the page name provided by the user
}
if ($page _slug) {
Get page ID based on page alias
$page _id = $wpdb->get_var ("SELECT ID from $wpdb->posts WHERE post_name = '". $page _slug. "' and post_status = ' publish ' and Post_type = ' page '");
return $page _id;
} else {
return false;
}
}

Put this code in the theme file function.php,

Function Call Example:

<?php hui_get_page_id (' Contact us ');?>

The above code will get the page "Contact Us" id.



WordPress latest, thermal review and random log (article) function of the three-body of the latest log, thermal assessment log, random log these three functions are very similar in function, nothing more than a sort of different, the latest log is in accordance with the time of publication, the thermal evaluation of the journal is ranked according to the number of comments, random log is a random sort. So I simply merged these three functions into one, one ... WordPress: Using the file cache sidebar The following code allows you to cache most of the content you want to cache in addition to caching sidebar. A few notes: 1. You can modify the cache time, how to modify, look at the following code the only comment. 2. Once the cache is used, make any changes to sidebar in the cache time ... Practical WordPress Outside the chain jump code practical WordPress outside the chain jump code to share. From Zhanggo Blog, a memo. PHP version <?php//prevent WordPress from being subjected to malicious URL requests. from:http://blog.wpjam.com/m/block-bad-queries/if (strlen $_server[' Request_uri ') > ... wordpress jump directly to the external link code Function: Click the title of the article to jump directly to the external URL link. The implementation process is very simple, using the wordpress powerful custom field ~ First, open the theme file functions.php, paste the following code into the inside: function Lxtx_post_link () {global $post; $t ... To solve the WordPress "to perform the requested operation, WordPress needs access to your Web server permissions" problem solving WordPress "to perform the requested operation, WordPress needs access to your Web server permissions" Problem method one, authorized directory Chown-r www-data/home/wwwroot/html chown-r apache/home/wwwroot/html chmod-r 775/home/wwwroo ... Perfect for all pages of the site to add canonical tags (wordpress) perfect for WordPress site to add canonical tag code to share. What is canonical label rel= "Canonical" This label has been launched for a long time, canonical is Google, Yahoo, Microsoft and other search engines together to launch a label, its main role is to solve the network ... WordPress Perfect anti-spam reviewTips WordPress Perfect anti-spam comment tips. Using Ajax comments to submit and empty or delete wp-comments-post.php has always been a perfect way to prevent spam comments, but this method has a disadvantage, that is, upgrade or reload WordPress must be processed on the file, a certain way ... WordPress-Free Plug-in generation complete site map (sitemap.xml) of the PHP code WordPress-Free plug-in to generate complete site map (sitemap.xml) of the PHP code, you can generate a home page, articles, single pages, categories and tags sitemap, You can also delete a wordpress plugin. <?php require ('./wp-blog-header.php '); Header ("C .....") Article Category: network resources This article comes from:Arctic Cold Spell>>WordPress gets page ID method based on page namePermanent Link: http://blog.epinv.com/post/881.html

The author of this article: Arctic coldArctic Cold SpellA total of 248 articles were shared

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.