I want to display a custom field we have [using the Estatik wordpress real estate plugin] if the database field has a value and hide that if it is empty. I can get the field to display and hide if empty or filled out, but, I cannot display the text: "Year built" when the year is filled out:
<?php if( es_the_property_field('year-built1594411179f5f08c8ab6ff14') ): ?>
<p>Year built: <?php es_the_property_field('year-built1594411179f5f08c8ab6ff14'); ?></p>
<?php endif; ?>
What can i change the code above to achieve:
If the year is filled out, I'd like it to display:
Year built: 1994
If the year is left empty, i'd like the paragraph to not print at all
thanks in advance!
For most data in WordPress, there are the_[…]() and get_the_[…]() style functions. Generally the the_ functions will output the data, and the get_the_ functions will return. Most plugins and themes have similar functionality. I downloaded the demo Estatik plugin and noticed that it does have that functionality as well.
In your if statement, you're actually outputting the year field there, and it's not returning a truthy value, so it stops there (otherwise it would show 1994Year Built: 1994).
The returning function is es_get_the_property_field( $field, $post_id = 0 );
So, coupling that with a little bit of cleanup (utilizing the before/after arguments of es_the_property_field, you would end up with something like this:
<?php if( es_get_the_property_field('year-built1594411179f5f08c8ab6ff14') ){
es_the_property_field('year-built1594411179f5f08c8ab6ff14', '<p>Year built: ', '</p>' );
} ?>
HOWEVER:
Here's the whole markup for the es_the_property_field() function:
/**
* Render property field.
*
* #param $field
* #param string $before
* #param string $after
*/
function es_the_property_field( $field, $before = '', $after = '' ) {
$result = es_get_the_property_field( $field );
echo ! empty( $result ) ? $before . $result . $after : null;
}
This means you could actually drop your if statement altogether, because that function checks to see if it's empty for you. So you could simplify it even further to just:
<?php es_the_property_field( 'year-built1594411179f5f08c8ab6ff14', '<p>Year Built: ', '</p>' ); ?>
Note: I don't have any personal experience with this plugin, this is all just based on what I found in the estatik/functions.php file.
I am not familiar with the Estatik theme but I can tell you that you could test the content like this.
<?php
$year_built = es_property_field('year-built1594411179f5f08c8ab6ff14');
if( $year_built != "" || $year_built != false ): ?>
<p>Year built: <?php echo date("Y", strtotime ($year_built)); ?></p>
<?php endif; ?>
So the two things you were missing was a proper test in the if statement and then echoing out the year_built with the date format. Now this is assuming a few things.
that es_the_property_field echos instead of returning which would be a very wordpress thing to do
that removing the "the" from that function is the version of that function that returns instead of echos.
Related
I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);
I am new to php and trying to trim the lines of text generated by the ('information') part of the code seen below (in my custom WP theme):
<div class="information"><?php echo get_post_meta(get_the_ID(),'information',true); ?></div>
Each post has a different lenght of it's information (basically like a excerpt, but pulled from a custom field in each post) text, which messes up my archive/blog page, as i use a 3 columns grid. I need the lenght of all posts seen on the archive page to be equally long, by restricting the "information" text to be no more than lets say 150 characters long.
I see the fenction in this piece of code for example, taken from a default WP file, but can't seem to wrap it around my own piece of code seen above, to make it work like i want it to:
<?php
et_divi_post_meta();
if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) ) {
truncate_post( 270 );
} else {
the_content();
}
?>
This is what i want/talk about:
Before:
POST 1
I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.
After:
POST 1
I am a wordpress post, with a long text, that should not be so long...
How do i do this?
Thanks!
You can use substr() php function to get part of the string.
Take a look at this example function.
function trimResult($string, $len = 150){
if(strlen($string)> $len){
$string = substr($string,0,$len) . "..."; // to let the user know that was truncated...
}
return $string;
}
ref: http://php.net/substr
Specific code example, will be a better solution to include the php function on a global file.
<?php
function trimResult($string, $len = 150){
if(strlen($string)> $len){
$string = substr($string,0,$len) . "..."; // to let the user know that was truncated...
}
return $string;
}
?>
<div class="information"><?php echo trimResult(get_post_meta(get_the_ID(),'information',true)); ?></div>
Hope it helps.
$post = "I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.";
$post = mb_strimwidth($post, 0, 70, "...");
echo $post;
result: "I am a wordpress post, with a long text, that should not be so long..."
Add ... if string is too long PHP
I'm working on a small function for a WordPress site, and can't figure why my function is echoing the data on my webpage.
My function is:
function get_post_titles($args) {
$titles = array();
$posts = new WP_Query($args);
while ($posts->have_posts()){
$posts->the_post();
$titles[] = the_title();
}
return $titles;
}
and I call it in a separate .php file:
<?php
$titles = get_post_titles($args);
?>
<select>
<option selected="selected">Choose an Article</option>
<?php
foreach($titles as $title){
?> <option> $title </option><?php //this isn't working, pls ignore for this post
}
?>
</select>
...
The result is a webpage with a Select box (yay!) but for some reason, the page also shows ALL of the titles in a row (it outputs the full array).
I would expect that if I had say, echo get_post_titles($args), but nowhere in my code do I ask it to echo. Why is my array visible?
I think it's because of my $titles = get_post... line. But again, why is it displaying $titles instead of simply declaring/assigning the variable?
(I know there's a WordPress SE site, but I think this is more directly a PHP issue than WP issue. However, if I should post over there instead, let me know).
the_title() displays the title. You should use get_the_title() instead.
From Wordpress Documentation, following is the usage of the_title:
the_title( $before, $after, $echo );
$echo
(Boolean) (optional) Display the title (TRUE) or return it for use in PHP (FALSE).
Default: TRUE
You can do the following (set the third argument to false):
$titles[] = the_title('','',false);
I'm trying to do something that seems really simple but I can't figure it out.
In my category template, I have this shortcode:
<?php echo do_shortcode( '[jobs categories="manufacturing"]' ); ?>
I also have this to show the title of the category:
<?php single_cat_title(); ?>
I would like to put them both together so I can pass the category into the shortcode so it fetches the correct job category listings. So I created this:
<?php $category = single_cat_title(); ?>
<?php echo do_shortcode('[jobs categories=' . $category . ']'); ?>
but it doesn't seem to work properly - Am I doing something wrong? Is this even possible?
Many thanks!!
If you look at the documentation for the single_cat_title() function, you'll see that it can either display or return the value, and it accepts two parameters $prefix and $display with default values of '' and true respectively.
What this means, is that just using single_cat_title(); will print Category Title to the document.
If you want to use it as a variable (without printing it to the document), you'll need to set the second parameter to false:
$category = single_cat_title( '', false );
This will define the $category variable for you, without printing anything, and you can then pass it to your shortcode (also note, that typically you'll want quotes around your attribute values in shortcodes):
echo do_shortcode( '[jobs categories="' . $category . '"]' );
You can make it a bit more succinct as well:
<?php
$category = single_cat_title( '', false );
echo do_shortcode( "[jobs categories='$category']" );
?>
I want to display author name for "author meta tag" , I used following code to do this, But I always get an empty string for that:
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<meta name="author" content="<?php echo $author; ?>">
<?php } ?>
How can I get the current displayed page/post author name ?
Thank you
You need to check first you if you get correct values from the get_the_author_meta(). So, you will know if the problem come from the trim/condition part, or from wordpress itself.
To do this, add in your code :
echo "Here is my result : ".get_the_author_meta('first_name')." ".get_the_author_meta('last_name');
Once this test done, i'm sure you'll need to edit your question.
Take this answer as a general advice for debugging, more than a solution for your problem.
Ok, got it. The name of the author is actually displayed by php in your HTML page, but it's encapsulated in a meta tag, which is only used in the head part of your page, and don't produce any visible output for the user.
Try to use a div tag instead of the meta one, and make sure you are writing inside the body part of your page.
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<div>Author: <?php echo $author; ?></div>
<?php } ?>