Community Forums › Forums › Archived Forums › General Discussion › How to check what layout is being loaded?
- This topic has 6 replies, 3 voices, and was last updated 10 years, 3 months ago by lmartins.
-
AuthorPosts
-
June 13, 2014 at 2:19 pm #109621nickhempseyMember
Hey guys,
I'm trying to conditionally load a script based on the layout of the page. Is there a function I can use to check which layout is active?
Thanks!
NickJune 13, 2014 at 5:36 pm #109646Brad DaltonParticipantYou could create a custom layout. http://wpsites.net/web-design/flexible-custom-layouts-for-studiopress-themes/
June 16, 2014 at 8:35 am #109979nickhempseyMemberThanks Brad,
I'm not looking to create a custom layout, although I will go that route if all else fails.
I just need to know whether or not the current layout is using the primary sidebar or not.
June 16, 2014 at 8:52 am #109983nickhempseyMemberI did some digging in genesis/lib/structure/layout.php and found the function genesis_site_layout(); It returns the active layout of the page. So I can do this:
$site_layout = genesis_site_layout(); //* Load custom code when content-sidebar is active if('content-sidebar' == $site_layout ){ //* Do something }
September 29, 2014 at 9:10 am #126157lmartinsMemberThe method @nickhempsey suggests does work but in my case, once I run the genesis_site_layout() function in my functions.php the post gets the default layout instead of the custom assigned layout.
I was trying to run a piece of code depending on the layout currently used:
$site_layout = genesis_site_layout(); if ( $site_layout == 'sidebar-content-sidebar' ) { add_action( 'genesis_before_entry', 'mw_add_attachments' ); } else { add_action( 'genesis_entry_footer', 'mw_add_attachments' ); } function mw_add_attachments(){ $attachments = new Attachments( 'attachments' ); /* pass the instance name */ if ($attachments->exist()) { ?> <ul class="files"> <?php while( $attachments->get() ) : ?> <li> <div class="files-thumbnail"> <?php echo $attachments->image( 'thumbnail' ); ?> </div> <div class="files-details"> <a href="<?php echo $attachments->url(); ?>" target="_blank" class="files-title"> <?php echo $attachments->field( 'title' ); ?> </a> <div class="files-meta"><?php echo $attachments->subtype(); ?> - <?php echo $attachments->filesize(); ?></div> <div class="files-caption"> <?php echo $attachments->field( 'caption' ); ?> </div> </div> </li> <?php endwhile; ?> </ul> <?php } }
September 29, 2014 at 9:24 am #126162nickhempseyMember@imartins , that code shouldn't set the layout, only return the active layout.
I'm wondering .. are you wrapping this code into a function or just firing it directly in functions.php?
If you have that code directly in functions.php I would suggest making a function that places it in genesis_meta instead.
add_action('genesis_meta', 'mw_conditional_attachments'); function mw_conditional_attachments() { $site_layout = genesis_site_layout(); if ( $site_layout == 'sidebar-content-sidebar' ) { add_action( 'genesis_before_entry', 'mw_add_attachments' ); } else { add_action( 'genesis_entry_footer', 'mw_add_attachments' ); } } function mw_add_attachments(){ $attachments = new Attachments( 'attachments' ); /* pass the instance name */ if ($attachments->exist()) { ?> <ul class="files"> <?php while( $attachments->get() ) : ?> <li> <div class="files-thumbnail"> <?php echo $attachments->image( 'thumbnail' ); ?> </div> <div class="files-details"> <a href="<?php echo $attachments->url(); ?>" target="_blank" class="files-title"> <?php echo $attachments->field( 'title' ); ?> </a> <div class="files-meta"><?php echo $attachments->subtype(); ?> - <?php echo $attachments->filesize(); ?></div> <div class="files-caption"> <?php echo $attachments->field( 'caption' ); ?> </div> </div> </li> <?php endwhile; ?> </ul> <?php } }
let me know if that helps at all.
September 29, 2014 at 9:29 am #126164lmartinsMemberOhh, brilliant. Is working now.
Im such a new with PHP but I must say im quite liking working with it and Genesis.
Thanks @nickhempsey !! -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.