Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to add elements to individual pages?
Tagged: elements to individual pages
- This topic has 16 replies, 2 voices, and was last updated 4 years, 7 months ago by [email protected].
-
AuthorPosts
-
May 22, 2015 at 5:08 pm #153325[email protected]Member
Hey everyone.
I am just beginning to learn about hooks and is() things. I have a need to add specific elements to only specific pages before the main content wrap so that is is full with. probably do action(gensis_after_nav, golf_slider) function golf_slider is a slider that is just for one page of the site.
Does anyone know how I can do the is (page) and have it be limited to one specific page?
May 22, 2015 at 5:23 pm #153328[email protected]Memberthis is what i have so far.
it didnt work though
add_action('genesis_before_loop','golfcourseslider');function golfcourseslider() {
if (is_page('golf-course-homes')) {
add_action(genesis_after_header,golf_course_slider);
}
}function golf_course_slider(){
layerslider(4);
}May 22, 2015 at 5:28 pm #153330GrahamMemberThe is_page() function accepts a $page parameter
Ref: https://codex.wordpress.org/Function_Reference/is_page
This means that you are able to target a single page by its ID, slug, page title etc
e.g.
if ( is_page ( 'your-page-slug' ) ) { // do stuff }
or, by page ID. e.g page id 10
if ( is_page ( 10 ) ) { // // do stuff }
If you wished to target a group of pages, then you could do so by using an array. For example
if ( is_page( array( 42, 54, 6 ) ) ) { // do stuff }
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 5:30 pm #153332GrahamMemberPerhaps a typo in your code above
add_action(genesis_after_header,golf_course_slider);
should read
add_action( 'genesis_after_header', 'golf_course_slider' );
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 5:38 pm #153335[email protected]Membertried this too
add_action('genesis_before_loop','golfcourseslider'); function golfcourseslider() { if (is_page('golf-course-homes')) { add_action(genesis_after_header,golf_course_slider); } } function golf_course_slider(){ echo do_shortcode('[layerslider id="4"]'); }
May 22, 2015 at 5:57 pm #153342GrahamMemberThe single quotation marks are necessary
add_action( 'genesis_after_header', 'golf_course_slider' );
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 6:13 pm #153346[email protected]Memberoh doh! I am a PHP infant. Thanks so much for pointing this out.
May 22, 2015 at 6:16 pm #153347[email protected]Memberok added the single quotation marks and now my code looks like this.
add_action('genesis_before_loop','golfcourseslider');
function golfcourseslider() {
if (is_page('golf-course-homes')) {
add_action('genesis_after_header,golf_course_slider');
}
}function golf_course_slider(){
echo do_shortcode('[layerslider id="4"]');
}I honestly dont think that I would like to use the shortcode for the layer slider. the code is also given for php "<?php layerslider(4) ?>"
The revised code still did not work however. The slug is golf-course-homes. not sure if it has to be in quotes or anything.
Is there a specific place within the functions.php file where this section of code needs to be placed?
May 22, 2015 at 6:17 pm #153349[email protected]MemberOh my goodness!! I am so hasty! I ust put quotes around the whole thing! hahaha. wow sorry for that. Lets close those quotes properly and see if it works.
add_action('genesis_before_loop','golfcourseslider');
function golfcourseslider() {
if (is_page('golf-course-homes')) {
add_action('genesis_after_header','golf_course_slider');
}
}function golf_course_slider(){
echo do_shortcode('[layerslider id="4"]');
}that is what I have now. Still not working.
May 22, 2015 at 6:32 pm #153353GrahamMemberTry this
add_action('genesis_meta','child_maybe_show_slider'); function child_maybe_show_slider() { if (is_page('golf-course-homes')) { add_action( 'genesis_after_header', 'golf_course_slider' ); } } function golf_course_slider(){ echo do_shortcode( '[layerslider id="4"]' ); }
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 6:40 pm #153360[email protected]Memberwell that worked!
any reason why that worked and mine didnt?May 22, 2015 at 7:02 pm #153361GrahamMemberI used an earlier hook. By the time genesis_before_loop came along, it was too late. genesis_after_header had already happened
Glad it worked 🙂 Are you able to mark as resolved?
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 7:19 pm #153363[email protected]MemberYeah I can mark as resolved. In the interest of not creating another thread. Can I use two conditional tags in one function?
ex. is_page(!Home,!golf-course-homes)
May 22, 2015 at 7:28 pm #153364GrahamMemberGenerally, for the sake of others, it's better to start another thread 🙂 .. yet ...
Functions are defined, you can only use them in the way that they are built. If the function were built in a way that accepts two parameters, then yes you could ( the syntax isn't right in the above, but no matter in this example ).
In this case, the is_page() function only accepts one parameter.. the $page parameter - as documented in the WordPress codex. The parameter for this function has to be of a certain type and format.
Ref: https://codex.wordpress.org/Function_Reference/is_page
It's all fun and games... happy Googling !
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 7:46 pm #153369GrahamMemberA way of approaching what i think you are looking for might be something like this
function child_maybe_do stuff() { if ( !is_page('golf-course-homes') || !is_home() ) { // do stuff } }
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 22, 2015 at 7:55 pm #153370GrahamMembercurses... wont let me edit.
Meant this
function child_maybe_do stuff() { if ( !is_page('golf-course-homes') && !is_home() ) { // do stuff } }
My JustGiving page: https://www.justgiving.com/helping-graham-give
-
AuthorPosts
- The topic ‘How to add elements to individual pages?’ is closed to new replies.