Tag Info

Hot answers tagged

48

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


46

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


15

You can use the function is_active_widget . E.g.: function check_widget() { if( is_active_widget( '', '', 'search' ) ) { // check if search widget is used wp_enqueue_script('my-script'); } } add_action( 'init', 'check_widget' ); To load the script in the page where the widget is loaded only, you will have to add the is_active_widget() ...


11

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


10

I think this should work: $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') ); if ( $attachments ) { // do conditional stuff here }


9

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


8

You can use the is_page( 'landing-page-template-one' ) conditional around your page specific styles / scripts as part of your over-all enqueue statements. function my_enqueue_stuff() { if ( is_page( 'landing-page-template-one' ) ) { /** Call landing-page-template-one enqueue */ } else { /** Call regular enqueue */ } } add_action( ...


7

No, its not possible. $content_width is a theme-wide constant, and its set in functions.php before any of the query conditionals are set. $content_width is used to determine the intermediate image sizes in image_send_to_editor. The "large" image size will be set to the value of $content_width. If you need to modify those sizes on a per-category basis, you ...


7

From quick look at code this conditional only seems to be processed for styles and not scripts.


6

Hi @NetConstructor: First thing, assuming your logic worked you can use the ternary operator to simplify your example: <li id="kids-<?php echo is_term('Kids','age_groups') ? 'on' : 'off'; ?>">Kids Programs</li> The issue seems to be that is_term() is used to check if a term exists, not if it is associated with a particular post. I ...


6

How about a few simple lines With jQuery? jQuery(document).ready( function ($) { if ($(".entry-content:first-child").has('img').length) //this check for the img tag $(".entry-content:first-child").after("<div>MY CUSTOM CODE</div>"); else $(".entry-content:first-child").before("<div>MY CUSTOM CODE</div>"); }); ...


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

This gets asked a lot so lets try and fully explain it. We can simply wrap it in an if statement and echo the value, for example, <?php if ( get_post_meta($post->ID, 'genre', true) ) : ?> <?php echo get_post_meta($post->ID, 'genre, true) ?> ?> <?php endif; ?> But that is ugly, and why do 2 queries when you can do ...


5

if( get_post_type() == 'post_type_slug' ) { //do some stuff } else { //do other stuff } Should do it, alternately you can use templates to handle that, which wordpress has setup pretty well by default. See the codex page on template hierarchy, specifically I find the visual overview to be quite useful.


5

I think it is better to check for role rather than specific capability for such purpose, this should work: <?php if ( current_user_can('administrator') ): ?> See Roles and Capabilities in Codex.


5

This is the work around I had to put in place since WP doesnt support what I was trying to do functions.php add_action('init', 'sort_out_jquery_pngfix_frontend'); function sort_out_jquery_pngfix_frontend() { if(!is_admin()) { wp_deregister_script('jquery'); wp_register_script('jquery', ...


4

functions.php is processed way before you can know which page is being loaded. Instead of assigning value to variable put your code into function and use that function in page.php template.


4

It is a long shot, but you might try registering the script, then adding in the conditional, and then enqueueing the script: // Register the script wp_register_script( 'dd_belatedpng', get_stylesheet_directory_uri() . '/js/dd_belatedpng.js', array(), NULL, true ); // Attempt to add in the IE conditional tags $wp_scripts->add_data('dd_belatedpng', ...


4

Testing for sub-Pages section of Conditional Tags article in Codex has fitting code example that uses get_post_ancestors() to retrieve parent tree and loops through it with check.


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

Wrap the content in a conditional: if( is_archive() ){ // Do stuff } More on conditionals here: http://codex.wordpress.org/Conditional_Tags


4

I would recommend setting up multiple sidebars. Then you can call one sidebar for regular pages and a different sidebar for archive pages. This gives you complete control over which elements appear in the sidebar on each type of page. Here's a good tutorial on creating multiple, widget-ized sidebars. You can manage each in the Appearance >> Widgets ...


4

Yes, that's good. You can be a little safer using <?php if(current_user_can('manage_plugins') ); ?>


4

WordPress never makes any HTTP requests for gravatars, it just generates URLs to them. You don't really need to override function completely. You can filter get_avatar hook and return different URL if email matches user with custom avatar set.


4

You could try putting this code in your functions.php function remove_contact_nav( $nav_menu, $args ){ if( is_page_template('template-contact.php') || is_page( 'contact' ) ) { $nav_menu = null; } return $nav_menu; } add_filter( 'wp_nav_menu', 'remove_contact_nav', 11, 2 ); The if condition need to be modified as per your template ...


4

As an alternative to @m0r7if3r's solution, the add_meta_boxes hook optionally passes two variables, the post type and post object. You can use this to conditionally add your metabox. New posts have the post status of 'auto-draft'. add_action( 'add_meta_boxes', 'myplugin_add_custom_box',10,2); function myplugin_add_custom_box($post_type, $post) { ...


4

If you plan to do a lot of WP development you should bookmark this page: http://codex.wordpress.org/Conditional_Tags The other answer works but the conditional relies upon your page slug (myurl.com/this-is-the-slug) never changing. A more reliable method (IMO), and one that fits this case, would be to use the is_page_template('example-template.php') ...


4

To catch the first paragraph (<p>) you can use a regex. That's not optimal, so be warned. :) Then you test the match for an image and insert the extra content depending on the test result. I use two functions here, one for each step: The first finds the first paragraph, the second changes the first match. // Late priority parameter to let shortcodes ...


3

Try this: if ($my_query->post_count < $min_posts) { // Do Something. } Good luck! Hope that helps.


3

Just check for the global $numpages variable: <?php global $numpages; if ( is_singular() && $numpages > 1 ) { // This is a single post // and has more than one page; // Do something. } ?>



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