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.
Related
In WordPress we use the code <?php the_title(); ?> for displaying the title of a post. Now, I want to display only the first letter of the title in a different place. How can I do that?
I have tried this, but it doesn't work:
<?php $my_title = the_title(); ?>
<?php
$first_char = $my_title[0];
echo $first_char;
?>
// Get the first character.
// $firstCharacter = $string[0];
$my_title = get_the_title();
// Get the first character using substr.
$firstCharacter = substr($my_title, 0, 1);
echo $firstCharacter;
The the_title() function will print it by default if the echo parameter is not set to false. get_the_title() will retrieve the title.
You can simply do this:
<?php echo get_the_title()[0]; ?>
As long as the title is not empty. Or:
<?php echo substr(get_the_title(),0,1); ?>
<?php echo preg_replace('/^(\w).+/','\1',get_the_title()); ?>
<?php echo str_split(get_the_title())[0]; ?>
<?php printf("%.1s", get_the_title()); ?> //echo sprintf
etc...
Or if you want to get complicated, you can use a "stream" yea!:
$f = fopen('php://memory', 'w+');
fputs($f, get_the_title());
rewind($f);
echo fgetc($f);
fclose($f);
LOL - that was the hardest way I could think of, that does what it's supposed to and doesn't have any unnecessary steps (well, except fclose, but in this case we can recover the memory);
Sandbox
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!
One of the problems I have is that my title contains an "&" but it displays as "&" when I click share.
Another problem I have is that the code below does not recognize "< br >" or "\n".
How do I make it so it will recognize "&, ', -, etc.." as well as line breaks.
<?php
$gettitle = get_the_title();
$getcontent = get_the_content();
$content = substr($getcontent, 0, 20);
$content .= "<br>".$moretexthere;
$link = get_permalink();
$title=urlencode($gettitle);
$url=urlencode($link);
$summary=urlencode($content);
?>
<a onClick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php echo $title;?>&p[summary]=<?php echo $summary;?>&p[url]=<?php echo $url; ?>','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)">Share</a>
Well, i use this as sharer url..
https://www.facebook.com/sharer/sharer.php?u=URLENCODEDURL
Use the urlencode PHP function.
"This function is convenient when encoding a string to be used in a
query part of a URL..."
http://php.net/urlencode
WordPress Example:
<?php echo urlencode(get_the_title()); ?>
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.
Im trying to filter out all of the tags inside a post in wordpress using strip_tags(). I set it up like this:
<?php
// The Query
echo date ("Y");
query_posts( 'p=10' );
// The Loop
while ( have_posts() ) : the_post();
$footertext = the_content();
echo strip_tags($footertext);
endwhile;
// Reset Query
?>
This doesn't seem to strip out the tags they way I expected.
Change the_content() to get_the_content().
the_content() is used to directly output the contents whereas get_the_content returns contents to be stored in variable for further processing.
$footertext = get_the_content();