Hot answers tagged conditional-content
25
Here you are: get_post_type() and then if ( 'book' == get_post_type() ) ... as per Conditional Tags > A Post Type in Codex.
16
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() ...
15
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 here:
...
7
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
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 ...
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
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
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()
...
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
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
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
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
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
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
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
Just wanted to share a solution I worked on which allowed me to be a bit more versatile with my implementation. Rather than having the function check for a variety of shortcodes, I modified it to seek out a single shortcode that references a script/style function that needs to be enqueued. This will work for both methods in other classes (such as plugins ...
3
Handy little function
function is_child_of($parent) {
global $post;
if ( $post->post_parent == $parent){
return true
}else{
$curent_parent = $post->post_parent;
$dec = false;
while ($curent_parent > 0){
$p = get_post($curent_parent);
$curent_parent = $p->post_parent;
...
3
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', ...
3
Get the Image does what you need and better. It's NOT overwhelming with lots of unnecessary features, and does what it says. Try it to see if it does what you need.
How does Get the Image plugin pull images?
Looks for an image by custom field (one of your choosing).
If no image is added by custom field, check for an image using ...
3
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( ...
3
Here you go. Because the DL, DT, DD, and UL are all within the foreach loop, that is why they are being duplicated. Here is the proper code:
function get_spec_1(){
$desc1 = get_post_meta( get_the_ID(), 'eco_mb_tp_1', true );
if( !empty( $desc1 ) ){
// Open the stuff
$code = "
<dl>
<dt ...
3
If you're using it on single templates, the easiest & most efficient way would be to switch based on the value of get_post_type().
switch(get_post_type()) {
case 'news' :
// some statement for news type
break;
case 'sports' :
// some statement for sports type
break;
default:
// something to do when it's none ...
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
While s_ha_dum's answer is not only not wrong, but exactly the answer you'd expect to get, I'd highly advise against using it, whether you are on 3.4+ or not.
User agent sniffing sucks. Whether done manually or via a core function. Period.
For one, new UA strings pop up like wildfire. For another, a good number of browsers pretend to be something else ...
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 ) )
{
...
2
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.
2
You can do:
if( 'video' == $post->post_type )
$play_button = '/images/btn-play.png';
elseif( 'slideshow' == $post->post_type )
$play_button = '/images/btn-ss.png';
Then render the button with:
<img src="<?php echo get_bloginfo('template_directory') . $play_button; ?>" class="play" />
Hope that helps!
2
Can I create a loop with multiple post types and specify different $args for each post type?
Simply put no... you can't have a single query instance with three differing sets of arguments. The reason why is pretty simple, the query class will only take one array of arguments per instance.
If you want differing sets of results, then you need additional ...
Only top voted, non community-wiki answers of a minimum length are eligible