简体   繁体   中英

Wordpress homepage ID

I want to point a function that only works on the home, the problem is that I can´t find the ID of the homepage. How do I know which ID it has?

The id "home" doesn´t works...

In your editor, when you edit your page, check the URL. It will look something like this:

http://www.domain.co.uk/wp-admin/post.php?post=154&action=edit

Where your ?post={ID IS HERE}

In this example, my home page (or the current page I am editing) is 154.

No need to find the id of home page. Use the conditional tag is home

<?php
if ( is_home() ) {
    // This is a homepage - write your function here.
} else {
    // This is not a homepage
}
?>

您需要首先创建主页模板,请参阅http://wsmithdesign.wordpress.com/2011/01/19/creating-a-custom-wordpress-home-page-template/然后您可以在主页模板中创建任何功能。

Please try this in function.php file :

  1. If you are working on javascript based function:
    \n        add_action('wp_head', 'functionForHomePage'); \n        function functionForHomePage(){  
    \n if(is_home()){ \n //function body \n } \n } \n

if above code doesn't work then try it:

<pre>
                    add_action('wp_head', 'functionForHomePage');
        function functionForHomePage(){  
             if(is_front_page()){
                //function body
              }
        }
</pre>
  1. if you are working on php function:

    \n        add_action('wp', 'functionForHomePage'); \n        function functionForHomePage(){   \n             if(is_home()){ \n                //function body \n              } \n        } \n

if above code doesn't work then try it:

<pre>
    add_action('wp', 'functionForHomePage');
    function functionForHomePage(){  
             if(is_front_page()){
                //function body
              }
        }

</pre>

actually I don't know how do you setup your wordpress site, so here I post as par available information as you mentioned.

Hope this is helpful for you. All the best ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM