I am very newbie into wordpress development so i had face a issue for displaying some post for specific category...
Like, I am adding(posting) some images into database for post_picture category...
Now at front end i have a page name Picture Mania where i just want to display only those image which i added into post_picture category...
For this purpose first i installed the php-exec plugin , and now I am trying to retrieve my desire category images on that page by this code
<?php query_posts('post_picture =post_picture&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?>
<?php
endwhile;
?>
It working fine, but displaying all category images, so conclusion I am not getting my desire output...
Answer will be much appreciated....and thanks for help in advance
You can pass category id (cat=3) such as
<?php query_posts('cat=3&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?>
<?php
endwhile;
?>
Or you can use category name in arary as well:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
query_post Reference: http://codex.wordpress.org/Function_Reference/query_posts
Although WP_Query is recommended approach instead of query_posts
for showing posts of specific category, you could add cat=YOUR_CATEGORY_ID in query_posts() like:
query_posts('post_picture=post_picture&showposts=5&cat=$your_Category_Id');
as in query_posts() example
try this:
<?php query_posts('cat=your-post_picture-category-id&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a>
Related
I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11.
However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.
Here is the loop on the custom page template
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else:
// no posts.
endif;
?>
And here is what I have on single.php
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
You should call the_post() in single.php before doing anything else.
Try this:
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Leave your other code alone. It looks like it should be working as expected.
Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.
In single.php use the following code to get the content and title.
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
use this in your custom page, i used wp_reset_postdata();
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else:
// no posts.
endif;
?>
And on single.php use this
<?php
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
?>
I am quite new to WordPress and PHP. I have a problem with posting specific posts on the front page (but I don't use static page option, using the index.php).
I have 2 categories (blog and tips) and currently, I am using 2 formats (standard and aside). Blogs are only assigned to standard and tips are only for the aside format. I want to display them in loop with posts which are in the Blog category & standard format.
<?php if ( have_posts()) : ?>
<div id="ob-grid" class="grid-layout">
<?php if( !in_category('Blog') && !has_post_format()) : ?>
<?php $i=1; while (have_posts() && $i<5 ) : the_post();?>
<?php get_template_part( 'content', get_post_format() );?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : the_post();?>
<?php get_template_part( 'content', get_post_format() );?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php do_action( 'E2R_posts_navigation' ); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
The problem is that the !in_category('Blog') && !has_post_format() are excluding each other and getting into else condition (displays all posts) and I can't find the problem (before I used instead of 'Blog', 'Tips' but it did not work either). When I put the condition for !has_post_format() in the while loop, then it filters correctly but the numbering then does not work properly.
The post formats are defined as:
add_theme_support(
'post-formats', array(
'aside',
'image',
'video',
'quote',
'link',
)
);
I have found solution for this problem by position change of the $i iteration. It looks like:
<div id="ob-grid" class="grid-layout">
<?php if( !is_category('Blog')) : ?>
<?php $i=1; while (have_posts() && $i<5 ) : the_post();?>
<?php if( !has_post_format()) : ?>
<?php get_template_part( 'content', get_post_format() );?>
<?php $i++ ;?>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
I got back to the point: ok, here it was displaying correctly but the number of the post was incorrect (the number of standard-blog posts minus number of aside-tip posts). so I said increment $i only if the post has no format -> !has_post_format() -> is standard. The syntax and functionality of this WordPress functions is weird. but it works for me.
I'm having some real trouble with the below code in my taxonomy.php template in Wordpress. The query is working (i.e pulling posts only from within that custom taxonomy) but it is only displaying 2 posts (4 are in the taxonomy).
All my efforts to convert this into a standard loop using $args simply result in posts from all taxonomies being pulled into the page. I was hoping it is as simple as adding posts_per_page => -1 but this just causes every post in the entire site to display.
As I understand it from the codex, taxonomy pages should pull the relevent posts by default, without a need for a loop?
Any help much appreciated!
taxonomy.php
<?php get_header(); ?>
<main>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
UPDATE
<main>
<?php
$args = array(
'posts_per_page' => -1
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
If you have 6 different taxonomies then there will be 6 different template files to show those taxonomies appropriately. In your case your templates will be taxonomy-topics.php taxonomy-places.php taxonomy-dates.php taxonomy-interviewee.php taxonomy-period.php and taxonomy-a-z.php
In this way once these templates are created your templates will be showing appropriate posts. To achieve that you can use posts_per_page argument or you can visit this page for better understanding about fetching posts WP_Query Codex Page Hope that makes sense by now
To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});
I want to get the Post title, image and excerpt of the specific WordPress category. I am using the below code to get the post titles of the specific category.
global $post;
$myposts = get_posts('numberposts=5&category_name=Cooling Towers');
foreach($myposts as $post) :
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
Any ideas, how do I get post image as well as excerpt for the specific WordPress category?
You can use this
<?php
query_posts( array('posts_per_page'=>5, 'category_name'=>'Cooling Towers') );
while ( have_posts() ) : the_post();
?>
<h2><?php the_title(); ?></h2>
<?php if ( has_post_thumbnail() ): // check for the featured image ?>
<?php the_post_thumbnail(); ?> <!--echo the featured image-->
<?php
endif;
the_excerpt(); // echo the excerpt
endwhile;
wp_reset_query(); // resets main query
?>