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 } ?>
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
I have some fields (Wordpress) and sometimes one of them is empty. I want to display the content of 'the_excerpt' when the 'short_description' is not filled in.
here is what I came with:
if (empty(the_field('short_description'))) {
the_excerpt();
} else {
the_field('short_description');
}
Unfortunately it displays both short_description and the except after that. What is wrong here? Do I miss something? For me the code looks good.
To Check if value exists first use get_field() function instead of the_field()
Please have a look on example which shows how to check if a value exists before displaying it.
<?php if( get_field('short_description') ): ?>
<?php the_field('short_description'); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<?php endif; ?>
Or you can user in another way like :
$isValue = get_field( "short_description" );
if( $isValue ) {
echo $isValue ;
} else {
the_excerpt();
}
WooCommerce: Show Custom Short Description When Empty
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty',
21 );
function bbloomer_echo_short_desc_if_empty() {
global $post;
if ( empty ( $post->post_excerpt ) ) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'This is the default, global, short description.<br>It will
show if <b>no short description has been entered!</b>';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
This is a Right
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
I'm making website about movies. Taxonomy is using to for a cast. For example:
Cool yeah? :D But i want to show the description on this page. I'm talking about this:
How to make it? Here is a code of taxonomy.php:
<?php get_header(); ?>
<div class="module">
<?php get_template_part('inc/parts/sidebar'); ?>
<div class="content">
<header><h1><?php printf( __( '%s', 'mundothemes' ), '' . single_tag_title( '', false ) . '' ); ?></h1></header>
<div class="<?php if(is_tax('dtquality')) { echo 'slider'; } else { echo 'items'; } ?>">
<?php if (have_posts()) :while (have_posts()) : the_post(); ?>
<?php if(is_tax('dtquality')) { get_template_part('inc/parts/item_b'); } else { get_template_part('inc/parts/item'); } ?>
<?php endwhile; endif; ?>
</div>
<?php if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } ?>
</div>
</div>
<?php get_footer(); ?>
That should work with <?php echo term_description(); ?>
see also https://codex.wordpress.org/Function_Reference/term_description, where you can also read about the two optional parameters $term_idand $taxonomy
You can use the method term_description
echo term_description($term_id, "your-taxonomy");
Every taxonomy has a unique id, like "course_teachers". so we grap it like :
get_the_terms($post->ID , 'course_teachers');
In this example we are graping the first term of ower taxonomy and store it inside a variable. like :
$course_teacher = get_the_terms($post->ID , 'course_teachers')[0];
now we need to grap the description :
$teacher_description = term_description( $course_teacher, 'course_teachers' );
Finally, print the description on the web page :
echo $teacher_description;
Note: if you get an error like (variable $post not exist). don't worry, just add code below on top of your code :
global $post;
If you are using a "loop". you need to write the $teacher_description variable inside the loop.
Grap other data's :
-> taxonomy name
$teacher_name = $course_teacher->name;
-> taxonomy archive URL
$teacher_archive_url = get_term_link( $course_teacher, 'course_teachers' );
I'm not a backend developer and it's was all of my knowledge. I hope this will help someone.
123 is term id.
$data = get_term(123)->description;
print_R($data);
Display category description:
<?php
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
https://developer.wordpress.org/reference/functions/the_archive_description/
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) )