Hot answers tagged

92

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


80

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


33

Use the global $pagenow, which is a common global set by WordPress at runtime: if ( $GLOBALS['pagenow'] === 'wp-login.php' ) { // We're on the login page! } You can also check the type of login page, for example registration: if ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === '...


21

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


20

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


16

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


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


9

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


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

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") { }


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


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

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


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


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

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'); add_filter('posts_join_paged','...


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

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


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

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

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

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


4

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.


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

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



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