Problem with PHP Syntax - php

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.

Related

PHP echo field, field appears before div

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;

Using Shortcodes within functions on Wordpress [duplicate]

This question already has answers here:
How can I call a WordPress shortcode within a template?
(3 answers)
Closed 4 years ago.
I have a functions file within a wordpress site that contains various functions that are called up by their relevant hook when a page on the website is rendered.
A particular function is working fine but I have now added a shortcode relating to the Wordpress Plugin "Collapse-O-Matic" with in the function's code. When the page is rendered the shortcode shows up as the shortcode itself in Square brackets! I presume there is something I'm not understanding about how to render the result of a shortcode and wondered if someone was able to explain to me how to do this correctly.
The shortcode is [expand title="Open" swaptitle="Close"]Target Content[/expand] and I have placed it in this function as follows (please note this is not all the code inside the function):
<ul class="admin">
<?php
// loop through rows (sub repeater)
while( have_rows('item_list_details') ): the_row()
// display each item as a list
?>
<?php
$date = new DateTime(get_sub_field('date'));
$now = new DateTime(Date('Y-m-d'));
$diff = $now->diff($date);
if ($diff->days > $latest): //Use $diff->days and not $diff->d
?>
<li class='research'>
<?php else: ?>
<li class='researchLatest'>
<?php endif;?>
<div class='itemTitle'>
<?php $link = get_sub_field('link_url'); if( $link ): ?>
<a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>" title="<?php echo $link['title']; ?>">
<?php endif; ?>
<?php the_sub_field('link_name'); ?>
<?php $link = get_sub_field('link_url'); if( $link ): ?>
</a>
<?php endif; ?><p class='alert'> - <strong>NEW</strong></p>
</div>
<br/>
<div class="itemDescription">
[expand title="Open" swaptitle="Close"]<?php the_sub_field('link_description'); ?>[/expand]
</div>
</li>
<?php endwhile; ?>
</ul>`
As you can see there is a php expression inside the shortcode (<?php the_sub_field('link_description'); ?>) but hope it is still possible to make this render correctly.
The function you are looking for is do_shortcode().
Generally, it's simply a matter of doing:
echo do_shortcode('[shortcode]whatever[/shortcode]');
Additionally, you are using the_sub_field() from ACF, which output directly and doesn't return anything, so you can’t pass its result to do_shortcode().
You could use get_sub_field() instead, and capture the output, and then pass everything to do_shortcode().
E.g.:
$linkDescription = get_sub_field('link_description');
$renderedShortcode = do_shortcode("[expand title="Open" swaptitle="Close"]$linkDescription[/expand]");
echo $renderedShortcode;
If you need to check if the shortcode exists before using, you have shortcode_exists() available.
E.g.
if (shortcode_exists('expand')) {
echo do_shortcode("[expand title="Open" swaptitle="Close"]$linkDescription[/expand]");
}
else {
echo $linkDescription;
}
Documentation:
get_sub_field()
do_shortcode()
shortcode_exists()
inside your code you have to use the wordpress function do_shortcode( $string )
<?php echo do_shortcode("[expand title=\"Open\" swaptitle=\"Close\"]".get_sub_field('link_description')."[/expand]") ?>
you can actually pass whole blocks mixed with html and shortcodes into this function, it will return a string with everything rendered out.
this works also obviously
$output=do_shortcode("[expand title=\"Open\" swaptitle=\"Close\"]".get_sub_field('link_description')."[/expand]");
edit: fixed get_sub_field according yivi's input

Wordpress Query on a Page using Allow PHP in Posts and Pages Plugin

It does what I want it to do, displays a certain category of posts in a column on a page. Right now its just the title, but I want to link that title and the permalink section isnt working, or rather the href.
[php]
// The Query
$the_query = new WP_Query( 'cat=3' );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul style="list-style:none;">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . '<a href="the_permalink();">' . get_the_title() . '[/a]'. '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
[/php]
It links to subdomain.domain.com/site/the_permalink(); instead pulling the permalink for that post and linking to it.
the_permalink(); return to echo your link.
You need to return only string. For this you can use get_the_permalink($post->ID);
Because your permalink function is inside of echo function.
When you enter the a-tag you start with HTML, if you close it you change to BBCode.
Fatih is right, you need to use get_permalink function. Since you're inside the loop, you don't need to specify the parameter ($post->ID).
Also, you need to switch back to PHP (that is your main problem) as you did with get_the_title function. There are multiple syntax issues with your code (brackets!).
The line should go like this:
echo '<li>' . get_the_title() . '</li>';
Learn the difference between echo and return in (not only) PHP functions!

php link code - separate the code

hi i need help with a small bit of code for my wordpress theme.
<a href="<?php echo $link; ?>">
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.', </li>';
}
?>
</a>
the code
<?php echo $link; ?>
this is for a link of a website that is already on the page the link part works great the only issue is i want the links (tags that are being used as keywords/anchor text)to be seo friendly for outbound links
what changes are needed? feel free to spice the link up for seo :)
If you're wanting the same link for each of the tags, then this is a solution:
<ul>
<?php
global $post;
foreach( get_the_tags($post->ID) as $tag ) {
echo '<li>' . $tag->name . '</li>';
}
?>
</ul>
Please note: I've added the unordered list start and end elements as these were missing, these could also be ordered list start and ends.
EDIT:
to reflect your code changes.
The code still looks the same...I would assume it should look something like this: (and I could be completely wrong) :)
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.'</li>';
}
?>

String concatenation in Wordpress

In my Wordpress site, I am trying to retrieve the most recent 10 posts and store them in a string. After that I will write this content into a text file. Below is the code I am using.
<?php $str = ''; ?>
<?php
require_once('../wp-blog-header.php');
query_posts('&showposts=10&order=DESC&caller_get_posts=1');
while (have_posts()) : the_post(); ?>
<?php $str .= '' .the_title() . ''; ?>
<?php endwhile; ?>
<?php $fp = fopen("latestposts.txt", "w");
fwrite($fp, $str);
fclose($fp);?>
The problem is, when I execute this page, the permalink and title are returning in this page and empty ''....'' tags are coming in text file. If I am not using the string, the href tags are returning correctly in the same file.
the_permalink() and the_title() does not return anything they are to print values.
You have to use their get_ version. Those are get_permalink() and get_the_title()
<?php $str .= '' .get_the_title() . ''; ?>
This is more of a wordpress question, but you should be using get_permalink() and get_the_title() instead of the functions you have there. Those functions will echo the link and title, and not return it in string form for use in your concatenation.

Categories