[wp-trac] [WordPress Trac] #21620: Add conditional tag to check if current page is any of the blog-related pages

WordPress Trac wp-trac at lists.automattic.com
Mon Aug 20 16:14:24 UTC 2012


#21620: Add conditional tag to check if current page is any of the blog-related
pages
------------------------------------+------------------------------
 Reporter:  grantnorwood            |       Owner:
     Type:  enhancement             |      Status:  new
 Priority:  normal                  |   Milestone:  Awaiting Review
Component:  General                 |     Version:
 Severity:  normal                  |  Resolution:
 Keywords:  dev-feedback has-patch  |
------------------------------------+------------------------------

Comment (by grantnorwood):

 Thanks Justin, I'm understanding better.  I get why is_single() and
 is_archive() won't work, and I'll look into how the others work with my
 custom post types, as that happens to be next on my dev list after the
 blog section.

 I'd like to provide a little context, in case that helps you decide
 whether it's a valuable addition to core or not.

 For the current site I'm working on, I would like to apply different
 themes to different pages, and groups of pages, using additional CSS
 classes on the body element.  This seems like something that many theme
 developers could use for conditional loading of styles/scripts/content,
 but it would be convenient to abstract the complexity of multiple is_xyz()
 calls for the various blog pages/templates.  Example below:

 {{{
 <?php

 //Theme color.  get_theme_color_class() uses my proposed is_blog_page()
 //function to determine which page is being viewed and returns a CSS class
 //as a string.  Also shown below.
 $theme_color_class = get_theme_color_class();

 ?>
 <body <?php body_class( $theme_color_class ); ?>>
 }}}
 {{{
 <?php

 /**
  * Get the CSS class name for the current page's theme color.  The theme
  * color is set using the "theme_color" custom field.
  */
 function get_theme_color_class() {

     global $post;
     $top_page_id = null;

     //Check for exceptions.
     if (is_blog_page()) {

         //Get the dummy blog page so we can get its custom fields.
         $top_page_id = get_page_by_path('blog')->ID;

     } /*else if () {

         //Some other exception ...

     }*/ else {

         //Check for ancestors.
         if ($post->post_parent) {

             $ancestors = get_post_ancestors($post->ID);
             $root = count($ancestors) - 1;
             $top_page_id = $ancestors[$root];

         } else {

             //This page is top-level.
             $top_page_id = $post->ID;

         }

     }

     if (!empty($top_page_id)) {
         $theme_color = get_field('theme_color', $top_page_id);

         if (!empty($theme_color)) {
             return $theme_color . '-theme';
         }
     }

     //No theme color specified for this page or its parents.
     return 'no-theme-color';

 }
 }}}

 ''Above is just for context, please ignore any other dumb mistakes or non-
 WP code formatting  :)''

 As you can see, get_theme_color_class() calls my proposed is_blog_page()
 to determine the current page, but is doing so outside the loop.  This
 seems to work well in the few use cases I've tried, and correctly sets a
 "red-theme" class on all blog-related pages (author, date, cat, tag,
 single), but this probably isn't reliable for all use cases.

 I understand your concern that with archives and custom post types, this
 might get hairy!  I'll continue to test with those for my own purposes,
 but also for the community if it's helpful.

 If you and/or others think there is value in a function like this, which
 simplifies detecting whether any of the post-related page templates are
 being viewed, then I'm happy to keep working on this ticket until it would
 be both useful and reliable.

 Maybe there are more use cases I should be thinking about for other WP
 developers?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21620#comment:4>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list