Tag Info

Hot answers tagged

44

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


36

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


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

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

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


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

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

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


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>"); }); ...


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


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


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


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

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.


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


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

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.


3

In WP 3.1, you have the handy has_term() function, found in wp-includes/category-template.php.


3

Hi @Denis Belousov: Not sure what exactly your code is doing wrong but here's code that does it right. I've decided to wrap the code in a class to be used for your plugin, and for humor I called it "Ugly Photo Plugin." Here's the full code: <?php /* Plugin Name: Ugly Photo Plugin */ class UglyPhotoPlugin { static function on_load() { ...


3

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


3

I would do it the other way around: Put the always visible text into a custom field per metabox, and protect the post content with a password. In your theme, always show the custom field, and let WordPress handle the password protection. Sample code for the theme: while ( have_posts() ) { the_post(); if ( post_password_required( $post ) ) { ...



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