Run php functions on selected Wordpress pages
I am trying to customise a Wordpress theme. I have a function in themes/functions.php that I would like to run on some pages.
I need to be to:
- Detect the page ID to determine whether the function should execute
- Determine which hook to attach the function to开发者_开发百科 (preferably something like page load.
Cheers
The file functions.php
is for theme specific functions called inside your theme. If this is a theme specific function then the function call should be in the header (or wherever you want the output of the function to appear) via <?php my_function() ?>
.
Hooks are for plugins, not template specific code.
If you are inside The Loop, then you can call <?php the_ID(); ?> as WarrenB said. If you are outside of the loop, then <?php echo $post->ID?> will print the page ID.
To the questions you posed, given you want to select on id #9, run your loop like this:
<?php query_posts('page_id=9'); if (have_posts()) : while (have_posts()) : the_post(); // Do whatever on post id #9 ?> <?php endwhile; else: ?> // Do whatever on all the other posts <?php endif; ?>
If this isn't the answer you're looking for, please add more information to your question.
精彩评论