Wordpress php echo issue - php

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();

Related

HTML5Blank Save excerpt into array

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);

do_shortcode within loop only returns content for first post

I have a shortcode that is supposed to return the title and content for multiple custom posts on a single page. It correctly displays the title for each post, but when it comes to displaying each post's content, it only displays the content from the first post. What am I doing wrong?
Figuring out how to get the content from within a shortcode has been tricky enough, so if anyone has any suggestions, I'd appreciate it!
My code is:
if($customposts->have_posts() ) : while ($customposts->have_posts() ) : $customposts->the_post();
$post= get_the_ID();
$foo = get_the_title();
$bar=do_shortcode($content);
echo "<h2>".$foo."</h2><p>".$bar."</p>";
endwhile; endif;
It doesn't look like you need to us do_shortcode. If title is working correctly, you should be able to assign get_the_content() to your $bar variable.
$bar = get_the_content();
Since you're looping through posts, store the html from each iteration of the loop, and then return all of the html at once (remember, you can't echo or print from a shortcode callback... well, you shouldn't, or you'll get some unexpected results):
$html = '';
if($customposts->have_posts() ) :
while ($customposts->have_posts() ) : $customposts->the_post();
$post= get_the_ID();
$foo = get_the_title();
$bar = get_the_content();
$html .= "<h2>$foo</h2><p>$bar</p>";
endwhile; endif;
return $html;
Also, be careful about using $post as your own variable, you will undoubtedly come into conflict with other scripts, including core.

Ho do I add/require a full php file into a variable in a function

I'm building in Wordpress and have some content that loads in using jQuery.load.
$item = '<h2>'. the_title() .'</h2><div>'. the_content() .'</div>';
//$item = include 'myrequire.php'; This doesn't seem to work.
I'd like to know how to make the php variable $item load a php file - so I can keep functions.php tidy and can just write the file like:
<h2><?php the_title(); ?><h2>
... does that make sense?
Thanks!
I followed this tutorial:
http://tomsbigbox.com/wordpress-load-more-posts-on-page-scroll/
here's the full snippet:
function getArchives($count,$offset){
query_posts('posts_per_page='.$count.'&offset='.$offset);
$posts = array();
if(have_posts()) : while(have_posts()) : the_post();
$item = '<h2>'. the_title() .'</h2><div>'. the_content() .'</div>';
//$item = include 'myrequire.php';
array_push($posts,$item); endwhile; endif;
return $posts;
};
http://www.php.net/manual/en/function.file-get-contents.php
You want to know the content of a php file?
include executes the file, you want file_get_contents
$var=file_get_contents($filename);
youcan also do it the wordpress way with get_template_part but that depends on what you want to do. I would look at the codex http://codex.wordpress.org/Function_Reference/get_template_part

Get the specific value of the_meta() in WordPress?

I'm trying exhaustively to get a specific row from within a loop using the_meta() array in Wordpress and assigning a variable to it (like the procedural while loop). Right now it simply pulls all of the rows at once. The result from this query returns 3 rows from the database. What I am trying to do it wrap a div tag around one of the returned rows that is stored within the_meta();.
I've tried exploding the array and returning the first row but it just returns everything all at once. I just want to get a row and put it in a variable so that I can style it using CSS.
Here is the code:
$args = array(
'post_type' => 'membersprofile',
'posts_per_page' => 20);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$newvar = explode(" ", the_meta());
echo $newvar[0];
endwhile;
Any help would be greatly appreciated!
EDIT: Thanks to Youn for pointing me in the right direction and finding the answer for me, The problem was I was using the_meta() which only returned the whole rows. By using get_post_meta I was able to assign variables to each row returned. The code that works is:
$key_2_value = get_post_meta(get_the_ID(), 'wpcf-shortbio', true);
// check if the custom field has a value
if($key_2_value != '') {
echo $key_2_value;
}
Hope this helps someone else!
Try using get_post_meta() in place of the meta. It will return the meta value for a specific page/post.
For more info visit at http://codex.wordpress.org/Function_Reference/get_post_meta
In loop you can use
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<p><?php echo post_custom( 'my_meta_key' ) ; ?></p>
<?php endwhile; endif; ?>

While cycle create an infinite loop

I wrote this PHP code to put on a wordpress page template:
<?php
query_posts('showposts=10&cat=7');
while (have_posts()) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; ?>
When I view the page I don't see any result and the right bar of the browser continue to reduce itself. I have understand that the code create an infinite loop.
Where I mistake?
Thanks
Firstly, you should not use query_posts. It's too invasive for simple loops, and messes with the entire WP_Query. Also showposts should be posts_per_page.
Secondly, it's hard to gauge what this issue is without more context. Perhaps pastebin your entire page, and edit it into your question. My guess is a loop within a loop, and should stop at like a 100 posts. (10 X 10) but if it's reset anywhere else if could very well go infinite!
Use this code instead to create loops:
$custom_query = new WP_Query( 'posts_per_page=10' );
if($custom_query->have_posts()) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
//global $post; // for stuff like $post->post_name
// Post stuff here
// the_title();
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Look at the WordPress codex for more details. http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
You should use an if statement in your loop:
<?php
query_posts('showposts=10&cat=7');
if ( have_posts() ): while ( have_posts() ) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; endif; ?>
Without further information, I would say that in each loop of the while statement, the function is returning the first row of the data? So each time the while loop executes you are calling the function again, which is returning the same row over and over again without actually iterating through the result set.

Categories