How can I achieve something similar to this:
http://anidemon.com/latest-episodes/
Latest Posts are organized under header of the day. I don't about the "View" count.
How can I do this?
Virendar,
Using get_posts() should solve your problem as follows...
<ul>
<?php
$myposts = get_posts('orderby=date&order=DESC');
$tempdate = '';
foreach($myposts as $post) {
$postDate = strtotime( $post->post_date );
if ($tempdate == '' || $postDate < $tempdate) {
$tempdate = $postDate;
echo '<h3>Display Date Title here</h3>';
}
?>
<li><?php the_title(); ?></li>
<?php } ?>
</ul>
Hope this helps! Please note I haven't included any CSS formatting.
Related
Thanks for taking an intrest
I am looking for some to provide a clear, good practice, example (or link to an existing one) of how to create an A to Z list of posts with Pagination for Custom Post Type in the latest version of Wordpress based on the scenario below.
There are two existing questions I have found on this site but neither answer the question clearly or definitively.
To keep things simple for me to explain the scenario and for those responding. I am basing my explanation around a website all about Fruit. When trying to use the answers given I am hoping this will be easy for I and other learners to follow the replys and intergrate it into their own code.
Basic Structure
I have created a Custom Post Type called "Fruits".
I have created a series of Posts within Fruits, each with a Post Title
of a particular fruit e.g. "Bananas".
I have created a Page Template within which is the following div:<div id="MyAtoZArea" class="MyAtoZStyle"></div>.
Output
Within the div I would like code that will display a list of all the
posts in the custom post type "Fruits".
I would like to wrap the output in an unordered list <ul> with each title being a different list item <li>
I would like the list to be sorted alphabetically by the Post Title.
As each letter of the Alphabet changes I would like it to be inserted. For example:
A
Apples
Apricots
B
Bananas
I would like for letters of the alphabet not to be shown if there are no applicable posts. For example:
A
Apples
K
Kiwi
S
Strawberrys
Finally I would like to show a maximum of 20 posts, using pagination to show the next 20 and so on.
Can you help? If so post below.
All constructive guidance and comments are welcome.
I would do it like below. The following code you should post in your template:
<?php
//get the posts first
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 20,
'post_type' => 'fruits',
'post_status' => 'publish',
'orderby' => 'title'
);
$fruits = new WP_Query( $args );
?>
<ul>
<?php
//loop through all the posts in the custom query
if( $fruits->have_posts() ) {
$current_first_letter = '';
while($fruits->have_posts() ) {
$fruits->the_post();
$title = the_title();
if (is_string($title)) {
$first_letter = $title[0]
//output a first letter list item with a custom class to be styled differently in your css if it is a new first letter
if ($first_letter != $current_first_letter) {
echo '<li class="divider"'> . strtoupper($first_letter) . '</li>';
$current_first_letter = $first_letter;
}
}
?>
<li><?php the_title(); ?></li>
<?php
}
}
?>
</ul>
//pagination
<?php if ($fruits->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $fruits->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
//restore original post data
<?php wp_reset_postdata(); ?>
You could easyly alter your code to also display labels for letters without posts
Please use this below code. The following code you should use in your post type template: it's really work for me... I have also tested this code.... See screenshots 1. http://prntscr.com/dxzi22 (full A to Z) 2. http://prntscr.com/dxziih (Full A to Z with Pagination)... you need do to some CSS for that...
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('paged' => $paged,'posts_per_page' => -1,'post_type' => 'services','post_status' => 'publish','orderby' => 'title','order' => 'asc');
$fruits = new WP_Query($args);
if($fruits->have_posts()){
$current_first_letter = '';
$t = array();
$s = array();
while($fruits->have_posts()){
$fruits->the_post();
$title = get_the_title();
if(is_string($title)){
$first_letter = strtoupper($title[0]);
if($first_letter != $current_first_letter){
$t[] = $first_letter;
$current_first_letter = $first_letter;
}
}
$s[$first_letter][] = get_the_title();
}
}
$t = array_unique($t);
$tc = count($t);
$sc = count($s);
for($i=0; $i<$tc; $i++){
?>
<div>
<h4><?php echo $t[$i]; ?></h4>
<ul>
<?php
foreach($s as $key => $value){
if($key == $t[$i]){
$vc = count($value);
for($j=0; $j<$vc; $j++){
?>
<li><?php echo $value[$j]; ?></li>
<?php
}
}
}
?>
</ul>
</div>
<?php
}
if($fruits->max_num_pages > 1){ ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $fruits->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
First of all it's supposed to be used in the single page. After the single post I want to display the categories that the post belongs to.
The basic code for this is <?php the_category(' | '); ?> which outputs a simple link. Then we have <?php echo get_the_category_list(); ?> which outputs a more specific code (http://codex.wordpress.org/Function_Reference/get_the_category_list):
<ul class="post-categories">
<li>
Business
</li>
</ul>
However I need to decompose this code. For example, I want the <a> tag to be before the <li> tag. I've got a code that does what I want, but it's supposed to display all the categories available in my page, which is:
<?php
$categories = get_categories();
foreach($categories as $category)
{
?>
<li><?php echo $category->name ?> <span class="lower">(<?php echo $category->count ?>)</span></li>
<?php
}
?>
Any idea how I can make this work?
Thanks!
You can use get_the_category() its return you category objects that you can loop and do with them what you want.
$category = get_the_category( /* $post_id */ );
print_r($category);
foreach($category as $cat) {
?>
<?php echo $cat->name; ?>
<?php
}
<?php
$category = get_the_category($post_id);
foreach($category as $cat)
{
?>
<li><?php echo $cat->name ?></li>
<?php
}
?>
I have a code in a page template that looks like this (obviously first I set up IDs of categories):
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $post;
$myposts = get_posts("cat=$catIDs&posts_per_page=12&paged=" . $paged);
?>
<div class="column-odd">
<?php
$i=1;
foreach($myposts as $post) :
if($i%2 != 0) :
setup_postdata($post);
//output odd post
endif;
$i++;
endforeach;
?>
</div>
<div class="column-even">
<?php
$i=1;
foreach($myposts as $post) :
if($i%2 == 0) :
setup_postdata($post);
//output even post
endif;
$i++;
endforeach;
?>
</div>
<nav>
<ul>
<?php
if($link = get_previous_posts_link("« Poprzednia strona"))
echo '<li class="prev">'.$link.'</li>';
if($link = get_next_posts_link("Następna strona »"))
echo '<li class="next">'.$link.'</li>';
?>
</ul>
</nav>
The problem is that get_next_posts_link is not returning anything. How can I make it work?
It won't work with get_posts. You need to use query_posts. I think it should work with the same arguments. edit: you'll also need to save it as $myposts = query_posts( ... ), since you're using two foreach loops like this.
You simply need to add the following line:
<?php query_posts('post_type=post&paged='. get_query_var('paged')); ?>
just before your get_posts() loop starts.
Full article here: http://blog.onireon.it/wordpress-get_posts-and-pagination-posts-are-correctly-displayed-but-no-pagination-links-are-showing/
I have the following code that generates a list of terms in a taxonomy term, and then POSTS that are under each term.
I want to have a current-page-item class added to the current item, so that when you are on a page under the taxonomy term, its related item in the nav is styled. Here is my code:
<?php $terms = get_terms('benefit-cats');
echo "<ul>";
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug,'order'=>'asc','orderby'=>'title');
$query = new WP_Query ($wpq);
echo "<li class=".$term->slug."><span class=\"list-item\"><span class=\"text-arrow\">►</span> ".$term->name."</span>"; ////
echo "<ul class=\"children\">";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<li><span class="text-arrow">►</span> <?php the_title();?></li>
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "</ul></li>";
}
echo "</ul>";
?>
You can try something like this...
Specify the current post_id before your loop, then condition to see if post loop contains your post_id.
// before loop
$page_id = $wp_query->get_queried_object_id();
// replace <li><span class="text-arrow">►</span>
if($page_id ==$query->post->ID ) $class = " current-page-item";
<li><span class="text-arrow<?php echo $class; ?>">►</span>
with
<?php
if (is_category())
echo single_cat_title();
?>
i can show up the current category title..
how can i insert the tag single_cat_title() into:
<?php
$recent = new WP_Query("cat=10&showposts=4"); while($recent->have_posts()) : $recent->the_post();
if (in_category(10) && in_category(**Insert Here**)) { ?>
i tried it with
if (in_category(10) && in_category('.single_cat_title.')) { ?>
but no chance...
thank you!
Probably best to put it into a variable
$cat_title = single_cat_title();
then
if (in_category(10) && in_category($cat_title)) { ?>