I have the following code:
if( get_field('event_timedate') ): echo "<div class='bm-event_timedate'><p>".the_field('event_timedate')."</p></div>"; endif;
But for some reason the output is (the date appears before the div and p tags)
27/08/2022 8:00pm <div class='bm-event_timedate'><p>".the_field('event_timedate')."</p></div>
Id really appreciate any help, thanks in advance!
use get_field() function for echo as well
if( get_field('event_timedate') ):
echo "<div class='bm-event_timedate'><p>".get_field('event_timedate')."</p></div>";
endif;
The issue is due to the_field()
this function itself displays data but you insert the display data function inside echo.
either you can do like
if( get_field('event_timedate') ):
echo "<div class='bm-event_timedate'><p>";
the_field('event_timedate');
echo "</p></div>";
endif;
OR
you can use get_field() function instead of the_field() function;
if( get_field('event_timedate') ):
echo "<div class='bm-event_timedate'><p>". get_field ('event_timedate')."</p></div>";
endif;
Related
this is the piece of code I'm not sure how to deal with:
<?php echo get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="InsertHere"]'); ?>
I simply want to insert the first echo instead of InsertHere. The first echo should output the content of a custom field. The second should recall the slider with the specific number inserted in the custom field. When trying different possibilities I only get errors.
Can anybody help?
Thank you :)
Don't echo the first value, use it directly inside your second echo.
<?php
echo do_shortcode('[smartslider2 slider="'.get_post_meta($post->ID, 'smartslider2', true).'"]');
?>
Or you can easily put that first value in a variable and use that variable in the second line
<?php $slider = get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="'.$slider.'"]'); ?>
Im working on customizing a Wordpress theme with some custom fields. For this I'm using the plugin "Advanced Custom Fields", but I want it to display these fields ONLY IF something is written in them. This is the code I'm using to display the custom fields:
<p class="tittelboks">Artikkelforfatter:</p>
<?php if( get_field('artikkelforfatter') )
{
echo '<p>' . get_field('artikkelforfatter') . '</p>';
} ?>
How do I change the code so that it only echos the information AND the label (in this case .tittelboks) if something is written in the meta boxes?
Michael
<?php if( $field = get_field('artikkelforfatter') ): ?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $field; ?></p>
<?php endif; ?>
This is a different way of doing an if statement, so you don't have to enclose your HTML in quotes and worry about escaping. The two lines in the middle will only print out if get_field('artikkelforfatter') returns a value or true. That value will be assigned to the $field variable.
Something like this should work:
<?php
$artikkel = get_field( 'artikkelforfatter' );
if ( ! empty( $artikkel ) ) {
?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $artikkel; ?></p>
<?php
}
?>
To use and display custom fields need you may try to put this in your loop.
<?php $what_name_you_want=get_post_meta($post->ID,'Your Custom Field Name',true); ?>
<?php echo $what_name_you_want; ?>// This call the value of custom field
Only replace
what_name_you_want with your favorite name
and
Your Custom Field Name with the name of custom field
If the value of custom field is empty the echo will be empty to.
Tell me if it's work
How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>
This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.
I am trying to get a link together using 2 variables but the output is the link and title but no html / clickable link appearing.
I'm getting something link this:
http://www.mydomain.com/post1/post_title_here
Here is the code:
echo ''.the_title().'';
Can anyone help please?
Thanks
UPDATE:
Here's the whole block of code:
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li>';
echo ''.the_title().'';
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</div>
That's because the wordpress functions the_permalink() and the_title() display the respective outcomes already they need not be echoed. If you want functions that return the values, you have to use get_permalink() and get_the_title() instead.
So either do:
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li>';
echo ''.get_the_title().'';
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</div>
or
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;
wp_reset_postdata();
?>
</div>
Both will work.
Here's a checklist for debugging:
1.) Is the_title() returning an empty string? (You can check by looking at the html source)
2.) Are you echoing this inside of the body tag?
3.) Is this being echoed in a hidden html element?
echo ''.the_title().'';
In this sort of situation, you'll want to use get_permalink instead of the_permalink and get_the_title instead of the_title.
echo ''.get_the_title().'';
WordPress the_* functions do a direct echo call, whereas the get_* functions return a value that you can use for further processing, like the concatenation you're doing.
(also note the inconsistent naming conventions - this can be a pain)
You could use the corresponding get_* versions:
echo '' . get_the_title() . '';
See the codex reference for more
You need to absolutely make sure that .the_title(). is definately bieng set a value. If it isn't, then there will be no displayed HTML as there is no text for the anchor tag. Just a thought (i've done it many times, try print_f(); ing the the_title(). Hope it helped.