Hot answers tagged

85

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 and more conditional tags can be viewed ...


74

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


31

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


20

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){ ...


19

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() && ...


15

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() { ...


15

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 ( ...


12

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 ...


8

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


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

To be honest, the function in_the_loop() is what you are looking for: add_filter( 'the_content', 'custom_content' ); function custom_content( $content ) { if ( in_the_loop() ) { // My custom content that I add to the_content() } return $content; } What in_the_loop does is to check if global for $wp_query (that is the main query ...


6

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 ...


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() ) { ...


6

If for any reason you already have access to the global variable $post, you can simply use if ($post->post_type == "your desired post type") { }


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

This is merely an addition to @Otto's answer. Just to make it a little bit better understandable. Basically what @Otto is saying, you have to reverse the logic, that means: if you can reliably determine the main query, then you can add - and remove - your hooking into the the_content filter. For example the main query can reliably be recognized at the ...


5

Just a few notes on your code You should dequeue and deregister a script to remove it completely from the $wp_scripts global You should not be using wp_print_scripts, this is the wrong hook. You should be using wp_enqueue_scripts Don't wrap your action in a conditional. Your conditional tag might either be set to early or to late and might cause unexpected ...


4

Regarding the posts_orderby, posts_where, posts_join and posts_clauses hooks, the current \WP_Query object is available through the second input argument. These are the relevant parts from the \WP_Query class: $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); $where = apply_filters_ref_array( 'posts_where', array( ...


4

You would rather want to use the post ID inside the loop to target your pages. is_page() simply checks whether the current page is actually a page or a specific page if a value is passed Example if ( $post->ID === 7 ) { // Do something for page id 7 } else { // Do something for other pages }


4

if ( !has_term('term', 'taxonomy', $post->ID )) { Use the NOT (!) operator


4

I'd suggest you use post_class for this. The following goes into your functions.php: function category_name_post_class( $classes ) { global $post; $categories = get_the_category( $post->ID ); foreach( $categories as $category) $classes[] = $category->category_nicename; return $classes; } add_filter('post_class', ...


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

You're calling it on the wrong hook, and you're calling the wrong conditional function to test. When the after_setup_theme hook is fired, WordPress hasn't finished processing wether you're on a page or a homepage yet. You should also never pass in hardcoded post IDs. In this case is_home works, as might is_front_page. If you really must reference a ...


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, ...


4

You'll want get_queried_object(). This is a very generic function - and simply returns the queried object- so a single post, this would be a post object. For instance, the return object may be of the form: Object ( [term_id] => 299 [name] => test [slug] => test [term_group] => 0 [term_taxonomy_id] => 317 [taxonomy] ...


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

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 .)



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