Same Conditionals Not Working on Two Different Hooks
init is too early for conditional tags, use template_redirect instead. have a look at the action reference to see the order they’re executed.
init is too early for conditional tags, use template_redirect instead. have a look at the action reference to see the order they’re executed.
<?php if(get_post_type() == ‘page’): // Only pages ?> <div class=”recent_postMetaSingle”> <img src=”https://wordpress.stackexchange.com/questions/31727/<?php bloginfo(“template_directory’); ?>/images/eye.png” alt=”Views” title=”views” /> <?php print_page_views(get_the_ID(”)); ?> <p>This is a static page!</p></div> <?php elseif(get_post_type() == ‘post’): // If not page, Only posts ?> <div class=”recent_postMetaSingle”> <img src=”https://wordpress.stackexchange.com/questions/31727/<?php bloginfo(“template_directory’); ?>/images/eye.png” alt=”Views” title=”views” /> <?php print_page_views(get_the_ID(”)); ?> <img src=”https://wordpress.stackexchange.com/questions/31727/<?php bloginfo(“template_directory’); ?>/images/comments2.png” alt=”Comments” title=”comments” … Read more
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 loaded }else{ // … Read more
I’m pretty sure you shouldn’t use negation in your conditions… If you check ! is_tax… it will be true not only for other taxonomy pages, but also for singular pages, and any other… So it should look like this: <?php if ( is_tax(‘taxonomy-name’,’taxonomy-one’ ) ) {?> <div class=”test” style=”color: red”><?php the_field(‘field_name’); ?></div> <?php } elseif … Read more
You know the id of the form from the shortcode, you can do stuff just for that particular form. Here’s some sample code for that, just replace $myform_id with your form id: add_action( ‘wpcf7_mail_sent’, ‘wp190913_wpcf7’ ); /** * Do stuff for my contact form form. This function shouldn’t return aything * * @param WPCF7_ContactForm $contact_form … Read more
You’re after post_password_required(): <?php if ( ! ( $post->post_password && post_password_required() ) ) get_sidebar() ?>
As this is an important question with no real answer, I will provide what I found after hunting a solution down, though I don’t have an answer as to why it doesn’t work like we all think it should. You need to hook into WordPress somewhere, like this: // load our posts-only PHP add_action( “wp”, … Read more
If you want to use different meta tags for different pages I recommend you to use custom field values. If you prefer to do it with conditional functions, try this: add_action( ‘wp_head’,’carlos_head_meta_page’ ); function carlos_head_meta_page() { if ( is_page(‘4’) ) { $description = ‘tag1’; } else { $description = ‘tag2’; } ?> <meta name=”description” content=”<?php … Read more
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() ) … Read more
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’,’edit_posts_join_paged’); } function edit_posts_join_paged($join_paged_statement) { global $wpdb; $join_paged_statement = “LEFT JOIN “.$wpdb->prefix.”wpv_voting ON “.$wpdb->prefix.”wpv_voting.post_id = $wpdb->posts.ID”; return … Read more