In sigle.php page I need to display a list of post that belong to the category of the post and the I have to display the post content.
So I did a query to get the posts tat belong to the post category:
<?php
$cat = get_the_category();
query_posts('cat='.$cat[0]->cat_ID);
while (have_posts()) : the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
Now how can I retrive the current post data?
If I do like this
<?php while ( have_posts() ) : the_post(); ?>
<?php echo the_post_thumbnail(get_the_ID());?>
<?php endwhile;?>
I still display query_posts.
I solved using WP_Query instead of query_posts();
<?php
$query = new WP_Query('cat='.$cat[0]->cat_ID);
while ($query->have_posts()) : $query->the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
Related
I am unable to fetch post tags added from wordpress admin panel. I am using fishpig magento extension and everything is working perfectly.
I am fetching posts using the code
$posts = $this->getPosts();
and I need tags associated with each post.
Please help.
You can get the tags for an individual post by calling the getTags method on the post object. Here is a snippet pertaining to post tags from the post view template:
<?php $tags = $post->getTags() ?>
<?php if (count($tags) > 0): ?>
<span><?php echo (count($categories) == 0) ? $this->__('This post was tagged with') : $this->__('and was tagged with') ?> </span>
<?php $it = count($tags) ?>
<?php foreach($tags as $tag): ?>
<a href="<?php echo $tag->getUrl() ?>">
<?php echo $tag->getName() ?>
</a><?php if (--$it > 0): ?>, <?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
You have a collection of posts rather than a single post, you can call getTags on an individual post as you are iterating over your collection.
foreach($posts as $post) {
$tags = $post->getTags();
}
Currently I am querying a post from my custom post type to my custom page template. This is the code I am using
<?php query_posts('post_type=testimonial&post_status=publish&posts_per_page=10&paged='.
get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
But I want to query a post from a specific category. For eg. my custom post type is "testimonial" it has 3 categories like category1, category2and category3 . I want to show only the post of category3 in my page template. How can I do that? thanks
Use this
<?php $posts = get_posts('category=3&orderby=rand&numberposts=5');
foreach($posts as $post) { ?>
<?php the_title(); ?>
<?php } ?>
Or
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
query_posts( $args );
Uncomment either one of the below as per you requirement.
If you want to query using the id of the category use the first one and replace 99 with your category id.
If you know the slug of your category use the second one with your category slug(not name).
<?php
//query_posts('cat=99&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged'));
//query_posts('category_name=your_category_slug&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
P.S: I recommend you to use wp_query() instead of query_post().
I have a slider script for my homepage (wp nivo slider), but only want to show slides (posts) if todays date is before post_end_date (custom field). This is so that I do not have to manually remove posts that are no longer relevant.
This is the code that loads the posts. Any help would be greatly appreciated.
Custom post field: post_end_date
<?php
$category = get_option('wpns_category');
$n_slices = get_option('wpns_slices');
?>
<?php query_posts( 'cat='.$category.'&posts_per_page=$n_slices' ); if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php if ( '' != get_the_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif ?>
<?php endwhile; endif;?>
<?php wp_reset_query();?>
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
in this function $postid is that id of wich is coming in loop and key is that name of custom field last in $single you can write true of false
get the custom fild by this function after that try the if condition for you custom postfeild values
hope this will help you
I am using the code below to attempt to display a list of tags associated with posts in the category 'html'
<ul>
<?php
query_posts('category_name=bikes');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
I am not seeing any results when i run it though, I have checked and there are lots of tags associated with the posts in the category.
Can anyone help?
You'll have to remove the $posttags = since you don't want to assign a variable but output it
<ul>
<?php
query_posts('category_name=bikes');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
A better way to get the results you're looking for would be not to use query_posts at all. Rather, use a new query to add to your loop. If my category were named photography, I would use this:
<ul>
<?php $photographyTags = new WP_Query(array('category_name' => 'photography')); ?>
<?php if($photographyTags->have_posts()) : while($photographyTags->have_posts()) : $photographyTags->the_post(); ?>
<?php
if( get_the_tag_list() ){
echo get_the_tag_list('<li>','</li><li>','</li>');
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</ul>
i want to show 4 posts in page and after 4 posts it will show next button to read next posts.
<?php while (have_posts()) : the_post(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&order=ASC&category=3');
foreach($myposts as $post) :
setup_postdata($post);
?>
here is content with html
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
You just need to add paged parameter to your query_posts:
$myposts = get_posts('numberposts=4&order=ASC&category=3&paged' . get_query_var('paged'));
And add post_nav_link() to display next and prev link.
<?php posts_nav_link(); ?>
Cheers, oh, btw, you can post specific wordpress question at wordpress.stackexchange.com.