I am trying to group the posts by Category in a Monthly archive but don't know how to achieve that.
Can someone here help me please.
Regards
This is the php code I am using in archive.php
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
The only problem is that its displaying same post titles for all the terms..
When you get to looping through the post in each category, you are not finding only the posts related to that category, and are instead looping through EVERY post. The below (untested, so comment if you get errors) should sort your problem.
Please note however that you may still get some repetition, if a post is assigned to more than one category.
Check each post to ensure it is in the desired category. Good point - only one query. Bad point - loops through all posts for every cat.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
Re-query the posts and output all that are grabbed. Good point - only pulls only the posts required for the category being looped. Bad point - lots of queries, so many queries could slow your site down.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>
Related
In a post (single.php) I want to list other posts in the category that post is included in.
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
?>
<ul>
<?php query_posts( 'showposts=5&orderby=date&cat=$cat_id' ); ?>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
Articles in all categories come with this code. However, I only want the articles in the category to which that post belongs to be listed.
I found the solution.
<ul>
<?php
foreach((get_the_category()) as $category) :
$cat= $category->cat_ID
?>
<?php $args = 'cat=' . $cat . '&orderby=date&order=ASC'; ?>
<?php query_posts( $args );?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php
the_title(); ?></a></li>
<?php endwhile; endforeach; ?>
</ul>
Everything about query_post () is here : https://developer.wordpress.org/reference/functions/query_posts/
I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
I have displayed "Home" category posts home page. Clicked on "Readmore" First post.
It is displaying all the details of first post.
And now i want to display all the posts below "first post" with "Readmore".
Here is my code, and it is displaying some other category posts.
<?php
$categories = get_the_category();
$catID = the_category_ID($echo=false); //Current selected category ID
$catPost = get_posts('cat=$catID&posts_per_page=3');
foreach ($catPost as $post) : setup_postdata($post);
?>
<h1><a><?php the_title(); ?></a></h1>
<?php the_excerpt(); ?>
<p class="postinfo">Written by: <?php the_author_posts_link(); ?>
Posted on: <?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?>
Categories: <?php the_category(', '); ?></p>
<hr />
<?php endforeach;?>
Posts Page
Try this way ..
<?php
global $post;
if(is_category() || is_single()){
foreach(get_the_category() as $category)
{
$current = $category->cat_ID;
$current_name = $category->cat_name;
//query_posts("cat=". $current);
$myposts = get_posts(array('category__in' => array($current)));
//$myposts = get_posts('numberposts=50&category='.$current);
//query_posts(array('category__in' => array(11)));
}
}
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
I have a global post query running, but for some reason my custom meta is not being output. When I try to call it inside a typical WordPress loop it works, but not in my code below. Is there any reason why this would be the case? Been trying to figure it out for an hour now....
<?php global $post; $cntr = 0; $myposts = get_posts('&post_type=go-deeper&posts_per_page=12');
foreach($myposts as $post) : setup_postdata($post);?>
<li class="<?php echo "slide_" . $cntr; ?>"><?php the_post_thumbnail('full'); ?></li>
<?php $cntr++; ?>
<?php endforeach; ?>
I just re-wrote the query and it works now:
<?php if(have_posts()): $cntr = 0;?>
<?php query_posts('&post_type=go-deeper&posts_per_page=12');?>
<?php while(have_posts()):the_post();?>
<?php $deeper_link = get_post_meta( get_the_ID(), 'll_deeper_link', true ); ?>
<li class="<?php echo "slide_" . $cntr; ?>">
<?php the_post_thumbnail('full'); ?>
</li>
<?php $cntr++; ?>
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php endif;?>
I'm having hard time trying to use teasers post in my wordpress theme (based on 960gs), as you can see here http://img17.imageshack.us/img17/794/schermata20110420a15045.png what I got till now is one "featured" post and three teasers post with thumbnails that will probably be six (so it'll have seven posts displaied in the homepage). The problem is that to do so I have to assign a class "grid_2 alpha" to the teasers post and I don't know how to assign this class to just the first teaser on the left, lefting the other ones with no alpha or omega class and putting the omega class to just the last teaser post (the seventh).
If can help, here's the code I'm using for the loop:
<?php $firstClass = 'firstpost'; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $firstClass; ?>">
<?php $firstClass = 'grid_2 alpha'; ?>
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" width="140" height="100" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>
also I would like to know how I can add some text above the teaser section under the first featured post. Sorry for the too many questions and for my bad english, as you can understand I'm not a developer but I searched for one week and couldn't find anything helpful for my problems. Thanks in advance for any help, I really appreciate it.
<?php $count = 0; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1):
$class = "firstpost';
elseif ($count == 2):
$class = "grid_2 alpha";
elseif ($count == $wp_query->post_count):
$class = "grid_2 omega";
else:
$class = "grid_2";
endif;
?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $class; ?>">
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>