Wordpress displays posts of category ID - wordpress

Can't seem to find the right answer for what I thought would be trivial.
I have some categories arranged like this...
Parent Category 1
- Child Category 1
- Child Category 2
- Child Category 3
...and I have some posts that are in Child Category 2. I want my page to display all posts from the category I am currently in.
This is what I am doing right now:
<?php
query_posts('cat=2&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
As you can see I am having to manually specify the category (cat=2), but instead I want it to automatically detect the category I am already in and display the posts (that way if I'm in a different category it will display those posts).
Any suggestions?
Thanks in advance. (SO Community = Awesome Sauce).

try below code:
<?php
$current_cat_id = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>

If you are using category.php, you can omit the query_posts and it will auto fill in the posts for you.

<?php
// Start the loop.
$categories = get_categories('child_of=1');//Parents category id
foreach ($categories as $cat) {
$option = '<a href="/category/archives/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;//parents sub category name
$option .= '</a>';
echo $option;
query_posts('cat=$cat->cat_ID&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; }?>

<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => 1 );
// 1 is a cat id.
//
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>

try
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'cat' => 1 );

Try this, this is better solution for this and you can also use it to show Related Post by a category id...
Just call the function that mentioned below by using this line. Put this in your template or page.php/single.php file.
Call by put this line: related_post_title('enter cat id here..');
Here is the Function and put this in function.php file.
Related posts function:
function related_post_title($cat_id){
$cat = $cat_id;
// Check if it is page only
if ( is_page() || is_single()) {
$args=array(
'cat' => $cat,
'order' => DESC,
'orderby' => rand,
'post__not_in' => array($post->ID),
'posts_per_page' => 9999,
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo ' <ul> ';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
}
If any query please let me know, i will help you...

Related

Displaying specific post title on the front page in wordpress

I want to display the specific post title/content in the front static page.Remember not all posts just specific. So can anybody guide me how to do that..
Yes you can get specific posts in front page by passing post ids with array in include parameter something like this,
<ul>
<?php
global $post;
$args = array(
'offset'=> 1,
'include' => array(1,2,3) // PASS POST ID IN ARRAY
'post_type' => 'post', );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
<?php the_content(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
Hope this works.
<?php
$titles=array();
$contents=array();
$links=array();
// the query
$the_query = new WP_Query( array(
'posts_per_page' => 3,
));
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $titles[]=get_the_title(); ?>
<?php $contents[]=get_the_content(); ?>
<?php $links[]=get_the_permalink();?>
<?php endwhile; ?>
and now printed the value in my page wherever i wanted
<?php echo $titles[0]; ?>
<?php echo $titles[1]; ?>
<?php echo $titles[2]; ?>
And same for other declared arrays. :)

How to create single page of advance custom field in wordpress?

I am create post with advance custom field (movie-page.php) I want to create single page(single-movie.php) each post of movie-page.php when I am click on each post it link to full details but I can not do it. Help me please!!!. thanks you.
And here is my code movie-page.php
<?php
/*
* Template Name: Page - Movie Page
*/
?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'movie'
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<div class="jol">
<?php the_title($post->ID); ?>
<?php get_post_field('descriptionii',$post->ID ); ?>
<?php the_content() ?>
<?php the_field("descriptionii") ?>
</div>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>
<?php
/*
* Template Name: Page - Movie Page
*/
?>
<?php get_header(); ?>
<?php $args = array(
'posts_per_page' => -1,
'post_type' => 'movie',
'post_status' => 'publish',
);
$posts_array = get_posts($args);
foreach ($posts_array as $key => $value) {
echo "".$value->post_title."</br>";
echo $value->post_content;
}
?>
<?php get_footer();

Wordpress, how to make an "if in category then" inside a loop?

How do I make this conditional work inside a loop? This seems so easy but it does not work.
The relevant part:
<?php if (is_category('Sup-FAQ')) : ?>
<p>If in cat sup-faq</p>
<?php else : ?>
<p>If not then print this</p>
<?php endif; ?>
The whole loop:
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$args = array(
'post_type' => 'support_post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID,
),
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ): ?>
<h4><?php echo $cat->cat_name ; ?></h4>
<?php if (is_category('Sup-FAQ')) : ?>
<p>If in cat sup-faq</p>
<?php else : ?>
<p>If not then print this</p>
<?php endif; ?>
<ul>
<?php
while($query -> have_posts()) : $query -> the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php
wp_reset_query() ;
}
?>
The loop takes all posts from the custom post type 'support post' and lists them by category. Now I need a conditional inside that loop that will display some text if a category is met. Thanks in advance.
From the is_category() documentation: To test if a post is in a category use in_category().
On the other hand, in your case you've already got the category in your outer loop. Just check $cat->name:
<?php if ($cat->name == 'Sup-FAQ'): ?>

Wordpress Tags in class

I have added tags to my Custom Post Type.
Now I want to use them to create a isotope portfolio, I can load all tags with this code:
<?php $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 24;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="all <?php echo $tag->slug; ?>">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
<?php endwhile; ?>
But now I want to add the all tags that from each portfolio item to the class="".
With <div class="<?php $tag->slug; ?>"> I just get the last tag of all the tags that are used.
I know there are already a lot of posts about this problem, but every post I have found does not seem to work for me.
It now works with the following code:
<?php $tags = get_the_tags();
$tag = wp_list_pluck( $tags, 'slug' );
$tagToClass = implode(" ", $tag);
?>
And then use <?php echo $tagToClass ?>

Wordpress: single.php shows the newest post only

I'm developing a wordpress 3.3.1 theme and I'm having troubles with the single.php file.
It displays - no matter what post (&p=111 e.g.) you select - only the content of the newest post.
This is my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="page-title"><?php the_title() ?></h1>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="cover" />
<?php endif; ?>
<p class="page-text">
<?php the_content(); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
What could be wrong? I hope you've understood my problem. Thank you!
edit:
I recently updated the header file. When I delete this loop, it works fine:
<ul class="nav-dropdown">
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 5,
'exclude' => '1,2,3,4,5,6,8,9,10,11,12,13,14'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
$post_args = array(
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<li class="nav-dropdown"><?php the_title(); ?></li>
<?php
}
}
?>
</ul>
I'd change your variable names in the header as ones such as $post are reserved by Wordpress for handling single post pages.
I am not sure but please change the $post variable to any other variable and than try
may be your problem be solved.
Because $post is global variable of post.

Resources