Wordpress meta value empty no display - wordpress

I am using a directory theme and I am using the following code:
<?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); echo $listing_contact; ?>
<?php if( empty( $listing_contact) ) : ?>no contact<?php endif; ?>
However, the 'no contact' value is not displaying once the $listing_contact is empty. Can somebody help me out to get this working?

Your code works, even though I would advise you to rewrite it:
<?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); ?>
<?php if ( empty( $listing_contact ) ) : ?>
No contact
<?php else : ?>
<?php echo $listing_contact;
<?php endif; ?>
Maybe check that you get the right ID in get_the_ID() or that your actually looking for empty() in your conditional.

The below will check and see if $listing_contact has any value if it doesn't display anything if it does display the contents of $listing_contact.
<?php
$listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true );
if(!($listing_contact == null || $listing_contact == '')){
echo $listing_contact;
}else{
echo "no contact";
}
?>
Would you please try above code?

Related

Get a Value in the Author Page from a Custom Taxonomy

I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.
I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
I have also tried the following code:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
still giving me an Array.
The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string
any Ideas how to make this Correct.
Thank you!
Ahmad
are you trying to get the custom taxonomy from the current user? try like this:
get_the_terms( get_the_ID(), 'nationality' )
and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:
get_the_terms( get_the_ID(), 'nationality' )[0]
I have the right Code here :
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>

WordPress last_modified what was changed

Is it possible to find out if only the date/time of a post has been changed or something in the content? The the_modified_date() function covers both and I can't find a way to do a specific check.
The Background: I have a list of posts which will be published in the future and I want to have some feedback for the users if the date or time of an upcoming post changes.
This is my code so far:
<?php
$loop_ls = new WP_Query( $args_ls );
if($loop_ls->have_posts()) : while($loop_ls->have_posts()) : $loop_ls->the_post();
// set date/time variables
$live_date = get_the_date('j.n.');
$live_time = get_the_date('H:i');
?>
<?php if ( get_post_status() == 'future' ) : ?>
<div class="live live--upcoming">
<p><?php _e('Am', 'textdomain') ?> <span class="post-date"><?php echo $live_date; ?></span> <?php _e('um', 'textdomain') ?> <span class="post-time"><?php echo $live_time; ?></span> <?php _e('Uhr', 'textdomain') ?> <?php _e('zum Thema:', 'textdomain') ?>
<br/><span class="post-title"><?php echo get_the_title(); ?></span>
</p>
</div>
<?php elseif ( get_post_status() == 'publish' ) : ?>
<div class="live live--now">
<p><?php _e('Jetzt:', 'textdomain') ?> <span class="post-title"><?php echo get_the_title(); ?></span></p>
<a class="btn btn--default" href="<?php echo get_the_permalink(); ?>"><?php _e('zum LIVE Video', 'textdomain') ?></a>
</div>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
OK, I figured out a way to save a creation date for posts published in the future (code below). But I still have the problem, that I can't check if only a posts date/time is modified (not the content).
function wx_add_creation_date_to_live_videos( $post ) {
$post_id = get_the_ID();
$post_type = get_post_type($post_id);
if( $post_type == 'video' && has_term('live', 'video_category') ) {
// If this is a revision, do nothing.
if ( wp_is_post_revision( $post_id ) )
return;
$creation_date = get_the_modified_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'creation_date', $creation_date );
}
}
add_action( 'draft_to_future', 'wx_add_creation_date_to_live_videos', 10, 3 );
EDIT:
sometimes you just think too complicated. To check a change on post date/time I changed the code above to this
$orig_publish_date = get_the_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'orig_publish_date', $orig_publish_date );
And now I can compare the values of the orig_publish_date with the current publish_date. And that's it.

Iterate Wordpress custom field?

I have a custom field in an Apus theme of type multi checkbox and I need to display the selected values.
I am using this code:
<?php $keywords = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'keywords', true ); ?>
<?php if ( ! empty( $keywords ) ) : ?>
<li><span><?php echo esc_html__( 'Keywords:', 'apushome' ); ?></span> <?php echo esc_attr( $keywords ); ?></li>
<?php endif; ?>
What this code is showing is
Keywords: Array
How can I loop the field's values to show them on the page?
Best regards
Americo
you may also use
echo implode(",",$keywords);
maybe this would help if this is indexed array, otherwise use
foreach ($keywords as $val){
echo $val. "\n";
}

If Data not exists - show (custom text)

I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>

Proper way to get page content

I have to get specific page content (like page(12))
I used that :
<?php $id=47; $post = get_page($id); echo $post->post_content; ?>
Work nice execpt for compatibility with translations, it returns both French and English text
But the loop is fine, return only the good language version
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id="post">
<?php the_content(); ?>
</div> <!-- .post -->
So the question.... HOW to get a specific page content inside the loop...
I've answered my own question. Call apply_filter and there you go.
<?php
$id=47;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
get page content by page name:
<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>
I used this:
<?php echo get_post_field('post_content', $post->ID); ?>
and this even more concise:
<?= get_post_field('post_content', $post->ID) ?>
A simple, fast way to get the content by id :
echo get_post_field('post_content', $id);
And if you want to get the content formatted :
echo apply_filters('the_content', get_post_field('post_content', $id));
Works with pages, posts & custom posts.
Just only copy and paste this code it will get your page content.
<?php
$pageid = get_the_id();
$content_post = get_post($pageid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
The wp_trim_words function can limit the characters too by changing the $num_words variable. For anyone who might find useful.
<?php
$id=58;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
$customExcerpt = wp_trim_words( $content, $num_words = 26, $more = '' );
echo $customExcerpt;
?>
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'prev_text' >' Previous','post_type' => 'page', 'posts_per_page' => 5, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
//get all pages
the_ID();
the_title();
//if you want specific page of content then write
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID
{
echo get_the_ID();
the_title();
the_content();
}
endwhile;
//if you want specific page of content then write in loop
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID
{
echo get_the_ID();
the_title();
the_content();
}
One liner:
<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?>

Resources