IF statement php displaying content if another is empty - php

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

Related

Using WordPress ACF repeater in header.php

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

Insert custom field value before content

I have a custom meta field that I would like to insert in the_content automatically so that my AMP plugin can render the custom field value in the same way as the_content.
Currently I am using this code to display it:
<?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?>
<?php if ( ! empty( $video_value ) ) {?>
<div class="video-container"><?php echo $video_value; ?></div>
<?php } else { ?>
<?php the_post_thumbnail(); ?>
<?php } ?>
But I would like the $video_value to be inserted automatically before the_content.
You can use the the_content filter to do this. You can read more about it on the WordPress developer site.
But the code could be looking something like this:
function my_custom_content_filter($content){
global $post;
$video_value = get_post_meta($post->ID, '_video', true);
if($video_value){
return '<div class="video-container">' . $video_value . '</div>' . $content;
}else{
return get_the_post_thumbnail($post->ID) . $content;
}
}
add_filter('the_content', 'my_custom_content_filter');
And you can add this code in you functions.php file.
Note This filter only works on the_content() and not get_the_content()
You can do something like this -
and add the conditions that you want -
You might need to access Global $post to get the meta value
function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true) {
$beforecontent = 'This line will go before the content - populate with whatever.';
$aftercontent = 'This will come after the content - makes sense right?';
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');
You can add this in functions.php

Show taxonomy description on wordpress

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/

Displaying post titles in a specific category in a shortcode (wordPress)

Trying to write my first short-code that displays all post-titles in a specific Category. But it is not displaying the actual results, just the short code.
Here is what I have so far in my child theme's functions.php file:
function posts_in_cat() {
echo '<ul>';
query_posts('cat=3'); while (have_posts()) : the_post();
echo ('<li>' . the_title() . '</li>');
endwhile;
wp_reset_query();
echo '</ul>';
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );
And then I am calling the short code, like so [display_posts_in_cat].
Any help would be greatly appreciated as I try and learn this.
EDIT: I have gotten it to display but the link itself is being echo-ed in front of the title in text. Also, it is not displaying more than 10 of the titles and I want it to display them all. Any ideas...?? Thanks.
First and foremost, avoid using query_posts() – it's inefficient. Check out this SO answer for the skinny.
Additionally, shortcodes shouldn't make use of echo statements. Shortcodes only return text. Put simply, WordPress has PHP internally that says "when this specific shortcode is entered into the editor, replace it with the text returned from this function." Using echo causes you to immediately print those statements to the screen, rather than returning to WordPress so that it can continue with its regular process. More on the WP Codex.
And, finally, shortcode functions require $atts as a parameter.
So, with all that said, here's how I would rewrite your function:
<?php
function posts_in_cat( $atts ) {
$atts = shortcode_atts( array(
'cat' => '',
), $atts );
if ( empty( $atts['cat'] ) ) {
// If category provided, exit early
return;
}
$args = array(
'category' => $atts['cat'],
// Disable pagination
'posts_per_page' => -1
);
$posts_list = get_posts( $args );
if ( empty( $posts_list) ) {
// If no posts, exit early
return;
}
$opening_tag = '<ul>';
$closing_tag = '</ul>';
$post_content = '';
foreach ( $posts_list as $post_cat ) {
$post_content .= '<li>' . esc_html( get_the_title( $post_cat->ID ) ) . '</li>';
}
return $opening_tag . $post_content . $closing_tag;
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );
I'm just adding all of the content you were echoing into a handful of variables, and then returning them, concatenated, at the end. Also, I added in an if statement to exit early if there aren't any posts in the category. That way you don't have an empty <ul> element cluttering up the page.
I've also escaped the outputs, which you can read about on the Codex.
Please try this:
function posts_in_cat() { ?>
<ul class="posts">
<?php query_posts('cat=3&showposts=50'); while (have_posts()) : the_post();
?>
<li><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<?php
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );

Can't Echo Custom Field In Widget

I'm trying to display a custom field in a widget I'm customizing but I can't get the custom field to display. I think it has something to do with the variable I'm using to get the post ID in the loop because when I change it to the standard the_title function, the widget works, so it has something to do with how I'm calling the custom field.
I know the key to the custom field is "wpcf-promo-title" but no matter what I've tried, the custom field (which holds a shortened version of the post title for the sidebar) just won't display. This code results in the thumbnails showing, but not the promo title. You can see this in action at http://www.cantstopshipping.com
Here's my code, including the query and the front end of the widget.
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
if ( ! $number = absint( $instance['number'] ) ) $number = 5;
if( ! $cats = $instance["cats"] ) $cats='';
// array to call recent posts.
$crpw_args=array(
'showposts' => $number,
'category__in'=> $cats,
);
$crp_widget = null;
$crp_widget = new WP_Query($crpw_args);
echo $before_widget;
// Widget title
echo $before_title;
echo $instance["title"];
echo $after_title;
// Post list in widget
echo "<ul>\n";
while ( $crp_widget->have_posts() )
{
$crp_widget->the_post();
?>
<li class="crpw-item">
<p style="float:left">
<?php the_post_thumbnail('sidebar-small'); ?>
</p>
<?php $promotitle = get_post_meta($post->ID, 'wpcf-promo-title', true); ?>
<p style="float:right; width:200px">
<?php echo $promotitle; ?>
</p>
<?php if ( $show_date ) : ?>
<span class="crpw-date"><?php echo "("; ?><?php echo get_the_date(); ?><?php echo ")"; ?></span>
<?php endif; ?>
</li>
<?php
}
wp_reset_query();
echo "<div class=\"fix\"></div>";
echo "</ul>\n";
echo $after_widget;
}
It looks like your global $post is missing.
But you could try get_the_ID() instead of $post->ID.
You should also consider getting rid of extract(), it's now considered a "bad" practice.
Another thing is that you should use wp_reset_postdata() to restore the global $post object. The wp_reset_query() call should be used with the query_posts() call.

Categories