Here are some conditional examples I have used for various tutorials.
(These are code pieces.)
add_filter( 'beans_layout', 'example_force_layout' );
function example_force_layout() {
if ( is_front_page() ) {
return 'c'; // Full width content
}
if ( is_page(contact) ) {
return 'c_sp'; // Content left - sidebar right
}
else {
return 'sp_c'; // Sidebar left - content right
}
}
/***** My modifications - This is just part of a code *****/
// Set default image for front page.
if ( is_front_page() ) {
$image_url = 'https://beans-tests.dev.cc/wp-content/uploads/
Blue-pattern-cool-bg.jpg';
}
// Set default image for contact.
if ( is_page('contact') ) {
$image_url = 'https://beans-tests.dev.cc/wp-content/uploads/
Green-mountains.jpg';
}
// Set a default image if no feature image is found.
if ( false === $image_url ) {
$image_url = 'https://beans-tests.dev.cc/wp-content/uploads/
1402533.jpg';
}
// Stop here if we are on a single view.
if ( is_singular() || is_admin() ) {
return $content;
}
// Return the excerpt() if it exists other truncate.
if ( has_excerpt() )
$content = '<p>' . get_the_excerpt() . '</p>';
else
$content = '<p>' . wp_trim_words( get_the_content(), 40, '...' ) .
'</p>';
There are some variations of the above code.
Switch out is_singular() || is_admin() with !is_archive()
if ( is_singular() && is_page() ) {
remove_all_actions( 'beans_post_header' );
}
if ( is_single() or is_page() ) {
beans_remove_action( 'beans_post_image' );
}
if( !beans_is_active_widget_area('hero') === is_front_page () )
return;
There are many times that I need to use conditional logic for various code.
Below I am sharing some of the various conditions I have used.
Remember to check the resources at the bottom for additional information.
Resources:
getbeans.io/code-snippets/force-a-layout
codex.wordpress.org/Conditional_Tags
https://developer.wordpress.org/themes/references/list-of-conditional-tags
isitwp.com/ultimate-guide-wordpress-conditional-tags/
developer.wordpress.org/themes/basics/conditional-tags/
developer.wordpress.org/themes/basics/template-hierarchy/
code.tutsplus.com/articles/php-for-wordpress-mastering-conditional-statements-and-tags–wp-22725
No comment yet, add your voice below!