Im trying to simply just output some HTML onto these 3 specific page-ids that I am referring too in the code below. Does anyone have any ideas as to why this php isn't working? (its at the bottom of my functions.php file in the child theme.
if( is_page('96') || is_page('98') || is_page('61') ) { ?>
<div class="test"><h2>test content</h2></div>
<?php }
Any help would be appreciated as to why this 'test content' is not appearing on these pages.
Thanks!
Try the below code that will work properly.
if( is_page(96) || is_page(98) || is_page(61) ) : ?>
<div class="test"><h2>test content</h2></div>
<?php endif;
===================== or ==========================
$id = get_the_ID();
if( ($id == 96) || ($id == 98) || ($id == 61) ) : ?>
<div class="test"><h2>test content</h2></div>
<?php endif;
You can use
get_the_ID(); or $post->ID;. you will get the ID of the current post of the loop
like this
$ids = [96, 78];
if(in_array(get_the_ID(), $ids)) echo '<div class="test"><h2>test content</h2></div>';
Please take a look at the docs :
For is_page function
To get the ID from the loop
Related
I'm currently adding this to the header.php page to add a script to a single wordpress page:
<?php if( $post->post_name == "my-page" ) { ?>
//script here
<?php } ?>
Works great. how do I add another page to this? I tried:
<?php if( $post->post_name == array("my-page", "my-page-two") ) { ?>
//script here
<?php } ?>
but it didn't work.
I'm trying to alter the experience of the user by changing the page templates based on URL UTM variables.
Example, if the utm_source=google I want to show the person the page-google.php.
Here's the code I have so far.
<?php if ( isset( $_GET['utm_source'] ) && $_GET['utm_source'] == 'test' )
{
get_page_template(test);
}
else {
get_page_template(gallery);
}
?>
I'm not sure 1) if this is the right way 2) where it should be placed in wordpress.
Thanks for your help!
in page.php
<?php if ( isset( $_GET['utm_source'] ) && $_GET['utm_source'] == 'test' ) {
get_template_part('test');
}
else {
get_template_part('gallery');
}
?>
this is if test.php is in your sites theme directory
Quick overview of what i am trying to do. I have a template layout that has 3 columns the left and right column are widget areas. What i would like to do is test for if both widget areas are active, one of widget areas are active, or if none are active (ie. have widget content).
Examples code:
<?php if ( is_active_sidebar( 'sidebar_1' && 'sidebar_2') ) { ?>
<div class="bothSidebarsActive"></div>
<?php } elseif ( is_active_sidebar( 'sidebar_1' || 'sidebar_2' ) { ?>
<div class="OneTheSidebarsActive"></div>
<?php } else { ?>
<div class="noneOfTheSidebarsAreActive"></div>
<?php }; ?>
I have had limited success getting the IF or ELSEIF to return true, but never all three states. Anybody know what i am doing wrong ?
Thank you for any and all assistance.
elseif ( 'employees_sidebar' || 'contact_sidebar' ) looks like your issue to me. You should be using the is_active_sidebar function here also. Like this:
<?php } elseif ( is_active_sidebar('employees_sidebar') || is_active_sidebar('contact_sidebar') ) { ?>
Ok thanks to - BULK for the assistance the code now works, here it is in case anybody else comes across this same issue.
functional code:
<?php if ( is_active_sidebar ( 'employees_sidebar' ) && is_active_sidebar ( 'contact_sidebar' ) ) { ?>
<div class="bothSidebarsActive"></div>
<?php } elseif ( is_active_sidebar ( 'employees_sidebar' ) || is_active_sidebar ( 'contact_sidebar' ) ) { ?>
<div class="OneSidebarActive"></div>
<?php } else { ?>
<div class="noneOfTheSidebarsAreActive"></div>
<?php } ?>
I have added a true/false checkbox using Advanced Custom Fields for Wordpress. I want to be able to select an option which amends the page template.
I am adding this option to the Product Category in WooCommerce / Wordpress. I have included this bit of logic in the code.
I have the following code but it does not work. I suspect it is because it is not within the loop. However the code I want to insert includes the loop. Any ideas/guidance on the code is much appreciated
<?php if (is_product_category() && get_field('field_name') == true) { ?>
<div class="custom-sidebar-right">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php } elseif (is_product_category() && get_field('field_name') == false ) { // Added close brace
<div> Empty Test </div>
}
Ok, I re-read the docs for ACF and found the following (http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/)
So I applied with some logic and now it works. Thanks for var_dump pointer as that helped me fix this.
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$is_field_name = get_field('field_name', $taxonomy . '_' . $term_id);
if (is_product_category() && $is_field_name == false) { ?>
missing bracket in the if statement ,
change
if (is_product_category() && get_field('field_name') == true)
with
if (is_product_category() && get_field('field_name') == true) )
I am trying to write a conditional query for a custom post type. I need it to be relevant to a specific post though. So for example, I have the current code which works for ALL projects:
<?php if ('project' == get_post_type() ) { ?>
// Get content
<?php } ?>
However, I need to be able to specify a certain project with ID 75. Is this possible?
Any help is greatly appreciated.
Cheers,
You can use $post global variable for specific custom post.
<?php if($post->post_type == 'project' && $post->ID == '75') : ?>
//Get Content
<?php endif; ?>
if($post->post_type == 'type your post type here' ) :
//Get Content
endif;
this is working...
you can use the get_the_ID() function inside the LOOP:
http://codex.wordpress.org/Function_Reference/get_the_ID
<?php if ('project' == get_post_type() && get_the_ID() == 75) { ?>
// Get content
<?php } ?>