Tag Info

Hot answers tagged

47

Here you are: get_post_type() and then if ( 'book' == get_post_type() ) ... as per Conditional Tags > A Post Type in Codex.


41

if ( is_singular( 'book' ) ) { // conditional content/code } The above is TRUE when viewing a post of the Custom Post Type: book. if ( is_singular( array( 'newspaper', 'book' ) ) ) { // conditional content/code } The above is true when viewing a post of the Custom Post Types: newspaper or book. These are more conditional tags can be viewed ...


23

Use the global $pagenow; if ( in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) run_my_funky_plugin();


14

The best way to approach this situation is via JavaScript. That way, whenever the selected value changes, you can instantly hide/show the related metabox. Use wp_enqueue_script() in functions.php to load a custom JavaScript file in the admin area: add_action('admin_enqueue_scripts', 'my_admin_script'); function my_admin_script() { ...


11

here is a function that i have: /** * is_edit_page * function to check if the current page is a post edit page * * @author Ohad Raz <admin@bainternet.info> * * @param string $new_edit what page to check for accepts new - new post page ,edit - edit post page, null for either * @return boolean */ function is_edit_page($new_edit = null){ ...


9

For anyone else looking for this, add this to your functions.php and you can have the functionality, inside or outside of the loop: function is_post_type($type){ global $wp_query; if($type == get_post_type($wp_query->post->ID)) return true; return false; } so you can now use the following: if (is_single() && ...


7

To test if a post is any custom post type, fetch the list of all not built-in post types and test if the post’s type is in that list. As a function: /** * Check if a post is a custom post type. * @param mixed $post Post object or ID * @return boolean */ function is_custom_post_type( $post = NULL ) { $all_custom_post_types = get_post_types( array ( ...


7

Do not create functions on the fly. This is hard to read and to debug. Implement separation of concerns instead, and separate the registration of callbacks from their execution (business logic). Setting the conditional checks logically in front of the callback registration is dead simple now. Wait for the action template_redirect to instantiate that ...


7

WordPress coding standard for PHP does not state anything about this, also there is no other standard, so it's up to developer choose one way or another. I have to say that the 2 methods have a different approach, while the first contain a conditional logic, the second is conditional function declaration, it means that if you try to call the function you ...


6

According to the WordPress conditional docs it should be: if(is_singular( 'video' ) ){ // do conditional stuff } else{ //do other stuff } Also if you want a blank page or no content to show when the CPT is not video you don't really need the else so you could omit it. You could ALSO do something like: if(!is_singular( 'video' ) ){ // do ...


6

This could be one of a few different issues. 1. Wrong Hook / Not Hooked You need to make sure that wp_enqueue_script is hooked to wp_enqueue_scripts. Otherwise, the call can get fired too late or (possibly in this case?) too early? Try this (also with your php cleaned up a bit: function wpse53364() { if( !is_admin() && is_single() ) { ...


5

Check for the type of the page: function get_author_bio ($content=''){ if ( ! is_single() ) // not a blog post, stop immediately { return $content; } global $post; // continue with regular work The easiest way to learn these check functions is a look at the function get_body_class(). Here are the most important: is_rtl() ...


5

Basically, you put the second block into the first. And wait until the last moment: add_action( 'pre_get_posts', 'wpse_71899_start_filter' ); function wpse_71899_start_filter() { // wrong page if ( ! is_page( 'Home' ) ) return; // stop here. add_filter('posts_orderby', 'edit_posts_orderby'); ...


5

The problem is that you're wrapping your add_action() calls inside of the conditionals, but at the point at which those add_action() calls are executed, neither the query nor the query conditionals are available yet. Solution: put the conditionals inside the callbacks: function faq_start_shortcode( $atts, $content = null ) { if ( is_single() || ...


5

This is because the_title() prints the title. You want get_the_title() to 'get' the title. Similarly single_post_title prints the title (by default). This can be prevented by using the second argument, e,g,: single_post_title('',false)


5

Did you check what part of is_front_page() is causing it to return false? I could reproduce the problem by following the setup from the trac ticket. In my case inside this function the call to is_page() was returning false. I guess this is due to using $wp_query->set() for page_id and is_page is only causing the query_vars to be changed, you don't ...


4

I just want to add that in general, one should be careful using conditional tags like: if( is_*() ) { // stuff } in the global scope of functions.php, because it will run before any filter or action is fired with do_action() or apply_filters().


4

Your conditional logic makes sense as well but you're probably looking for this: if ( 'page' == get_option('show_on_front') ) { // do something } Hint: You can append options.php to the WordpPress admin url like this: http://www.example.com/wp-admin/options.php to see all options. ( that's where I found the answer for you .)


4

Syntax is wrong you need to phrase it like so: condition ? TRUE : FALSE so change your code to : echo is_home() ? '1' : '0';


4

The Codex page for is_single should be updated, what you're seeing is correct behavior. According to the main Conditional Tags Codex page: is_single() When any single Post (or attachment, or custom Post Type) page is being displayed. (False for Pages) You can exclude attachments by also checking ! is_attachment(). Also, you may want to move that ...


4

Use has_category() to check if a post belongs to a certain category. function email_friends( $post_ID ) { if ( has_category( 'uncategorized', $post_ID ) { $friends = 'bob@example.org, susie@example.org'; wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: ' . ...


4

If you have a lot of rules that you need to conditionally apply (there's no hard and fast rule, but, to make one up, maybe 10-15+ lines?) then @aahan's answer is the way to go. If you only have a few though, just work off body class. In your header.php file (that's the common name at least) make sure the <body> has: <body <?php body_class(); ...


4

Take a look at the is_admin() conditional tag: function wpse106895_dummy_func() { if ( ! is_admin() ) { // do your thing } } add_action( 'some-hook', 'wpse106895_dummy_func' ); is_admin() returns true, if the URL being accessed is in the dashboard / wp-admin. Hence it's negation (via the not operator) is true when in the frontend. Update, ...


3

Make better use of the parameters: <?php the_tags( '<div class="tags"><span>Tags</span>', // before ' ', // separator '</div>' // after ); ?> Now you will never get any useless markup, because WordPress will not print anything when there are no tags for the current post.


3

You'd need something like this (to be added in functions.php): add_action('wp_enqueue_scripts','wpse57014_register_script'); function wpse57014_register_script(){ //Register and enqueue stylesheet only on this page if ( is_page( 'mypage' ) ) { wp_register_style( 'wpse57014_mypage_stylesheet', get_template_directory_uri().'/mypage-style.css' ...


3

Drawing on Sagive SEO's post this also seems to work without using counters. echo '<ul>'; while ( have_posts() ) { echo '<li><div class="slide">'; the_post(); the_content(); // If there is 1 more post, advance current post and add its content. if ( $wp_query->current_post + 1 < $wp_query->post_count ) { ...


3

You can define a global variable in your theme functions.php global $mywidgets; $mywidgets = false; in your template part add global $mywidgets; $mywidgets = true; and in your functions you can check if its set to true: function my_function(){ global $mywidgets; $mywidgets = true; if ($mywidgets){ // YES Your template part is ...


3

Install my plugin T5 WP Load Order. You get a long detailed list with all available hooks, files, constants, classes, functions and global variables – ordered by first appearance. Whenever you’re in doubt create such a list for the page you are working on, and look up what you need.


3

You might want to try is_front_page() instead of is_home(). is_home() returns true when your most recent posts are being displayed on your home page; is_front_page() returns true if you've set it to be a static page in Settings > Reading. See this article for more info: http://codex.wordpress.org/Conditional_Tags#The_Conditions_For_... Hope this helps!


3

Try this if statement instead: if ( is_category() && ( is_category( 59 ) || cat_is_ancestor_of( 59, get_queried_object_id() ) ) ) Update: To work for single posts: if ( is_single() && $terms = get_the_category( get_queried_object_id() ) ) { foreach ( $terms as $term ) { if ( $term->term_id == 59 || cat_is_ancestor_of( 59, ...



Only top voted, non community-wiki answers of a minimum length are eligible