Tag Info

Hot answers tagged

41

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


33

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


12

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


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


9

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


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


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

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

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


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

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

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

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


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

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


3

So, first off, if you want to block access to wp-admin, why hook into something that fires on every page load? Hook into admin_init instead. And, as @MattSmath mentioned, edit isn't a capability. edit_posts is. Also, admin_init only fires on admin pages, so you can remove is_admin() from your check. Your revised function: <?php ...


3

I don't see why it would be impossible to do what you want to do. I think you just need to do a bit of rearranging. Put your custom query code first, then put your containing HTML markup inside the if ( $my_query->have_posts() ), then put your related-post markup inside the while ( $my_query->have_posts() ), then put your no-related-post markup inside ...


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


3

As in this question: How to execute conditional script when on new customize.php (Theme Customize) screen global $wp_customize; if ( isset( $wp_customize ) ) { // do stuff }


3

You can combine conditions: if ( ! empty ( $GLOBALS['post'] ) && is_single() && in_category( 'gaming', $GLOBALS['post'] ) ) { // do something } See PHP manual: Logical Operators and Exclude custom function content from certain pages.


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

Use Conditional Tags to show content only if a certain condition is met. In your case, you're probably looking to use is_front_page(). <aside> <ul> <?php if ( function_exists( 'dynamic_sidebar' ) ) { if ( is_front_page() ) { if ( ! dynamic_sidebar( 'frontpage-widget-area' ) ) { ...


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

Is your home page blog index or static page? is_home() is meant to check if we are on blog index (latest posts), while is_front_page() checks if home is static page set in Settings > Reading.



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