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?
Related
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;
I am having a loop and that loop has only sticky posts in it. So my logic works like this:
"If Sticky Posts are "EMPTY" break the loop". That code works as expected and looks like this:
<?php //we will get "Sticky Posts" only with this loop and exlude Featured Category
$category = get_cat_ID('Featured');
$col = 1; //Let's create first column
$sticky = get_option( 'sticky_posts' );
$args = array(
/* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */
'paged' => $paged,
'category__not_in' => array($category),
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
/*Below is IMPORTANT PART*/
if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(empty($sticky))break;?>
<div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
<?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
<h3 class="mytitle"><?php the_title(); ?></h3>
<div class="entry">
<?php if ( has_post_thumbnail() ):?>
<div class="featured_img">
<?php
the_post_thumbnail();
echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
?>
</div><!--/featured_img-->
<?php endif; ?>
<?php // let's enable more link on pages...
global $more;
$more = 0;
?>
<?php the_content(__('Read more','override')); ?>
<div class="clear"></div>
<div class="custom_fields"><?php the_meta(); ?></div><br/>
<p class="postmetadata">
<?php _e('Filed under:','override'); ?> <?php the_category(', ') ?> <?php _e('by','override'); ?> <?php the_author(); ?><br/><?php the_tags(__('Tags:','override'), ', ', '<br />'); ?>
<?php _e('Posted on: ','override'); ?><?php the_time(get_option('date_format')); ?><br/>
<?php if ( comments_open() ) {
comments_popup_link(__('No Comments »','override'), __('1 Comment »','override'), __('% Comments »','override'));}
else {
_e('Comments are disabled!','override');
}
?>
<?php edit_post_link(__(' Edit','override'), __(' |','override'), ''); ?>
</p>
</div><!--/entry-->
</div><!--/post_class-->
<?php /*Enable Two Column Layout*/
if($col==1) {
$col=2;
echo "</div>";
}
else if($col==2) {
$col=1;
echo "</div>";
}
endwhile; ?>
<?php endif; ?><!--END if THE LOOP (Sticky)-->
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
Now before this working code I tried a different logic that goes like this:
"If NOT EMPTY continue the loop" so now everything in my code stays the same except:if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(empty($sticky))break;?> so now that code becomes:if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(!empty($sticky))continue;?>
Now this is where i got confused because if(!empty($sticky))continue; part does not work as expected because my loop CONTINUES (returns other posts) even if there are no "Stickies". I thought that loop will STOP if there are no stickies but it is not the case. My var_dump($sticky)
shows this if there are sticky postsarray(1) { [0]=> int(214) } and shows this if there are no stickiesarray(0) { }.
My question is: Why the loop continues to return other posts if using if(!empty($sticky))continue; (i thought it will return ONLY "Stickies" if they exist and return NOTHING if they are not here. )
Thank you!!
First off, let me poit out that your logic doesn't quite agree with your code :).
From what I understand from your code, you want to iterate all posts WP_Query() returned, but only render sticky ones. Your if is inside the wile loop, so you have to check if the current post is sticky or not. However, if(empty($sticky)) doesn't do that. It checks if there are any sticky posts at all. A way to check the current post would be if(is_sticky(the_ID())).
Now, concerning continue:
From the php manual:
continue is used within looping structures to skip the rest of the
current loop iteration and continue execution at the condition
evaluation and then the beginning of the next iteration.
So as you can see, continue doesn't stop the loop, but rather attempts to start the next iteration ignoring the rest of the code for the current step. Which is what you want, if the current post is not sticky, in other words if(!is_sticky(the_ID())).
However, I think you don't really need any check at all, since you already specified that you want WP_Query() to fetch only stickies ('post__in' => $sticky).
See also: this WordPress Answers topic.
I don't know well the wordpress code, but in that code, the $sticky variable is never updated in the while loop. So maybe you have to add $sticky = get_option( 'sticky_posts' ); right before the if condition.
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/
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)
Can someone tell me what the PHP would look like in order to get a category from Wordpress and then print it where ever I want?
I'm guessing I have to build a PHP function. Something like:
function get_a_category() {
$category = get_the_category(); <-----( not sure how to ge ta specific category )
echo $category;
}
I have no idea I know nothing about PHP
can someone help me please ?
You don't have to write your own function; you do have to work with the The Loop (WordPress Codex) and a new query (Function Reference/query posts « WordPress Codex) to keep the original WP loop in place. Run this new query in a page template to get the first post from "mycategory". Change showposts to the number of posts you want.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
Try this
function get_a_category() {
$category = get_the_category();
foreach ($category AS $key => $value) {
$category_name[] = $value->cat_name;
}
$categories = implode(', ',$category_name);
echo $categories;
}