WordPress loop displaying other post titles instead of just one - php

I have a problem with posts showing titles from other posts, instead of just the single post unique to it. Other parts are fine, i.e. no other repetition, just the titles.
My code that generates the content:
get_header();
$the_query = new WP_Query( 'category_name=careers&showposts=5' );
while ( $the_query->have_posts() ) :
$output = "";
$the_query->the_post();
$output_title .= get_the_title();
$output_content .= get_the_content();
$output_type = get_field('job_type');
$output_salary = get_field('job_salary');
$output_intro = get_field('job_intro');
$careers.= '
<div class="careers">
<h3>'.$output_title.'</h3>
<p class="type">'.$output_type.'</p>
<p class="salary">'.$output_salary.'</p>
<p>'.$output_intro.'</p>
</div>
';
endwhile;
wp_reset_postdata();
So what's happening is that the latest post shows a single title, which is fine, but then the second post shows its title + latest, the third job post shows its title +2+1st. For example:
Job post 1
Job Post 1Job post 2
Job Post 1 Job Post 2Job Post 3..
When it should be:
Job post 1
Job post 2
Job Post 3

Well it looks like you are concatenating strings i.e. remove the dot in front of the =
$output_title .= get_the_title();
Should be
$output_title = get_the_title();

The issue is caused by you using $output_title .= get_the_title(). The .= should be changed to just =.
while ( $the_query->have_posts() ) :
$output = "";
$the_query->the_post();
$output_title = get_the_title(); // Change this line
endwhile;

Related

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.

Post excerpt doesnt work

Hello stackoverflow people, I need help. I'm using wordpress and somehow I can't use this function:
$post_id = 12;
echo get_post($post_id)->post_excerpt;
Somehow it prints nothing. Here is the full div. Can you help me to solve this problem?
<div id="block1">
<div class="inblock1">
<h2>About boots</h2>
<p><?php $post_id = 12;
echo get_post($post_id)->post_excerpt; ?> </p>
apac
</div>
</div>
It sounds like you don't actually have an excerpt set for this post. You can always use a conditional to test it, and output a custom excerpt (from the post_content) if one doesn't exist:
$my_post = get_post($post_id);
// If the excerpt is empty, generate one from post_content, else display the saved excerpt
echo empty($my_post->post_excerpt) ? wp_trim_words($my_post->post_content, 55, '...') : $my_post->post_excerpt;
Read more about wp_trim_words() in the Codex.
You may need to use setup_postdata() because <?php $post_id = 12; echo get_post($post_id)->post_excerpt; ?> will return excerpt if you have excerpt data in excerpt field but below code will return data from content if you don't have data in excerpt field.
$post_id = 12;
$tempVar = $post;
$post = get_post($post_id);
setup_postdata($post);
the_excerpt();
wp_reset_postdata();
$post = $tempVar;

Wordpress: Get posts by tag where tag contains a name

I am working on a portfolio website with multiple artists, and each artist will have their own blog.
For each artist to have their own blog I decided for them to 'tag' themselves in posts that they make, on their portfolio pages I will list the posts with said tags.
The name of the page, is their name, so initially I thought just using:
<?php
$args=array('posts_per_page'=>2, 'tag' => $post->post_title);
$wp_query = new WP_Query( $args );
?>
would work, but doesn't seem to pull up any results. Yet when I echo the post title and the tag they are both displayed exactly the same.
So my next thought was to match the tag to a reg expression. Something like:
<?php
if( preg_match("/tony/i",$post->post_title)){
echo "Tony";
}
?>
but I do not know how to work that into a wp query.
Any idea how to do this, or if there is a better way to get to the same end result?
I quite recently was baffled by the exact same issue. I was trying to display articles based on a combination of a first and last name, but that wasn't working by just using the tag argument. I actually ended up using tag_slug__and for mine. Here's what I came up with...
<?php
$original_query = $wp_query;
$args = array( 'tag_slug__and' => array(strtolower($first_name) . "-" . strtolower($last_name)) );
$wp_query = new WP_Query( $args );
if(have_posts()):
echo "<h3>News Tagged " . $first_name . " " . $last_name . "</h3>";
while(have_posts()) : the_post();
$title = get_the_title();
$content = get_the_content();
$date = get_the_date();
//use the vars for something suuhhhhweeeeet!
endwhile;
endif;
$wp_query = $original_query;
wp_reset_postdata();
?>
The reason I used the tag_slug__and was because the tag slug is the lowercase tag with spaces delimited by a -. This is probably not the ideal solution, and just feels hacky, but it does work.
I'm curious why you would use tags, and not make use of the author page template and the native WordPress author system? There are lots of ways to do this without tags. If you need multiple authors, check out the co-authors plus plugin.
http://wp.tutsplus.com/tutorials/how-to-create-a-wordpress-authors-page-template/
http://wordpress.org/extend/plugins/co-authors-plus/

Show last 5 posts from specific category (Wordpress)

I'm trying to show the last 5 posts from a specific category, which will be linked to a function so I can insert the shortcode in a Wordpress page. The code I have is as below, it does everything I need (although I want to add featured image too) except it does not show posts from a specific category.
I've tried numerous things, but cannot find a working fix.
function Last5posts()
{
$args = array( "showposts" => 5, "category" => 3 );
$content = "";
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
$link = get_permalink();
$title = get_the_title();
$date = get_the_date();
$content .= "<div class='latest-posts'>";
$content .= "<h3><a href='$link' target='_top'>$title / $date</a </h3>\n";
$content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
$content .= "</div>";
endwhile;
wp_reset_query();
endif;
return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );
I have tried replacing lines 3 and 4 with the code below, but it throws an error "syntax error, unexpected '}' on line 31".
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
Any help would be greatly appreciated.
you can use code just like below
query_posts( 'cat=3&posts_per_page=5' );
using this wordpress by default will use last 5 post after this code...
Use this
$catnames[1] denotes which category u want to use related to that post.
<?php $catnames = get_the_category();
$postcatid=$catnames[1]->term_id;
$catquery = new WP_Query( 'cat='.$postcatid.'&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><?php the_title(); ?> </h3>
</li>
</ul>
<?php endwhile;
?>
Check this out: query posts parameters; you should definitely using "cat" instead of category.
Also, are you ending your "while" with an "endwhile;"?
What does your complete code now look like?

How can I combine this php statement to get the results of multiple variable inputs?

This is my WordPress query but is not a wordpress related question. It shows the posts that have meta_key as extra1 and meta_value as test
<?php $customkey1 = extra1; ?>
<?php $customvalue1 = test; ?>
<?php query_posts('meta_key=' . $customkey1 . '&meta_value=' . $customvalue1 . ''); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
My question is how can I still show the posts that have extra1 as metakey and test as metavalue but also the posts that have extra2 as metakey and test2 as metavalue in the same query. A combination of two or more variables.
So you want to combine the results of two queries? Check this out You may just need to make two query objects and combine them.
Something like (note, I do not have WordPress installed, nor use WordPress. But assuming that its API is not a lie):
<?php
// The Query
$the_query = new WP_Query( $args );
$the_second_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// The Second Loop
while ( $the_second_query->have_posts() ) : $the_second_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Note: This is hideous, ugly, and will work but in most cases should be considered WRONG by professionals. More elegantly, at least I would use a function to parse the post. Most elegantly, I would do what has been suggested elsewhere, encapsulate the combined query in a MySQL bit. But given the op's knowledge, this seems to be the "best approach" for quickly solving this problem, hopefully only once. (Repeated application of this approach will turn it into a messy nightmare)

Categories