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) )
Related
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
Within ACF, I have a repeater called "slider" with a radio button field. This appears on the homepage of the site.
I'd like to output the radio button field within header.php. Here's what I'vd tried:
<?php
if( have_rows('slider',$post->ID) ):
while ( have_rows('slider',$post->ID) ) : the_row();
if(get_sub_field('logo_type',$post->ID) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
This is coming up empty even when I try var_dump(get_sub_field('logo_type',$post->ID));
I've also tried:
<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type',$postid) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
What am I doing wrong here?
Did you try without this $post->ID
<?php
if( have_rows('slider') ):
while ( have_rows('slider') ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
I don't know if this will solve your problem but get_sub_field second parameter should not be the post id but the format value. So you would leave it empty in this case.
<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
I also recommend debugging what ID you're getting from $postid.
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo $postid;
?>
I think you need to add your custom field along with menu. i.e. create field group with header and assign menu is equal to your header menu name and then call that field using following code
$menu = wp_get_nav_menu_object('menuid');//replace with your menu id.
the_field('logo_type',$menu);
you can access it anywhere and you can see this field in Apperance->Menus->Menu-name->acf-field-name
So I have a customised wordpress search page that groups the result into the custom post type header.
This works but it still shows the post type if there are no posts in it.
What can I do to check to see if the section has posts, and if not, hide the section.
Note: if( !empty ( $hasposts ) ) currently returns FULL for each check as the post types have posts, but they are not part of the search results.
<?php
if( have_posts() ){
//Define post types:
$types = array('post', 'promo_offers', 'product_manuals', 'support_posts','product');
foreach( $types as $type ){
// RETURN EMPTY IF THE RESLUTS FOR THE POST TYPE IS EMPTY
$hasposts = have_posts($type);
if( !empty ( $hasposts ) ) { echo '<div>FULL!</div>';} else {echo '<div>EMPTY!</div>';}
//BELOW CODE WORKS AS INTENDED
echo 'your container opens here for ' . $type;
echo '<ul>';
while( have_posts() ){
the_post();
if( $type == get_post_type() ){
echo '<li>';
the_title();
echo '</li>';
}
}
echo '</ul>';
rewind_posts();
echo 'your container closes here for ' . $type;
}
}
?>
So my understanding is that have_posts() doesn't take parameters, and actually I'm curious about what happens when you call
$hasposts = have_posts($type);
Your function is looping through all posts multiple times, but only displaying those posts that have the type of $type. The point is that have_posts() returns true every time because it's referring to all posts, not just of the specified type.
So I think that what you need is to make a separate query for each type, like so:
foreach( $types as $type ) {
query_posts("post_type=$type");
while (have_posts() ) {
//now this should only happen if the specific type has posts in it...
}
}
In my WordPress template (that I am developing), I have the header.php and a template part named header-head.php (called in the header.php). At the index.php, I have other template parte named content.php (called in the index.php). I am trying to get a information of the first post of the taxonomy (named feeling) that I created and return the value in the header-head.php.
The content.php code:
$terms = get_the_terms($post->ID, 'feeling');
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'feeling' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
$term_return = 'Feeling ' . $term->name . '';
}
if ( has_term( '', 'feeling' ) AND $count == 0 ) {
$latest_feeling = "<p class='feeling-text'>" . $term_return . "</p>";
$GLOBALS['feeling_post'] = $latest_feeling;
}
The header-head.php code:
$feeling_return = $GLOBALS['feeling_post'];
echo $feeling_return;
You may have noticed the variable $count, it count the number of posts to check for the first post. I did it in the index.php file and then got the value in the content.php file using $GLOBALS, but now I am not getting...
The index.php loop code:
<?php if ( have_posts() ) : ?>
<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $GLOBALS['count_post'] = $count++; ?>
<?php
/* Include the Post-Format-specific template for the content.
*/
get_template_part( 'partials/content', get_post_format() );
?>
<?php endwhile; ?>
<?php sensibus_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
Can someone help me? What am I doing wrong?
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 } ?>