I have been working on this for a while basically i am trying to fetch the top three stick posts from a specific category in WordPress and display them only. I have some code below however this is fetching all the posts not just the specific ones marked as sticky in that category.
<?php $sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
);
$the_query = new WP_Query($query_args); ?>
<?php $count = 0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="featurethumb"><?php the_post_thumbnail(array(306,306), array ('class' => 'featurethumb')); ?>
<div class="featuretitle-bg"><div class="featuretitle"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></div>
<div class="featured-desc"><?php the_excerpt(__('(more…)')); ?></div></div>
</div>
<?php elseif ($count == 2) : ?>
<div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft1')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<?php the_excerpt(__('(more…)')); ?>
<?php else : ?>
<div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft2')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<?php the_excerpt(__('(more…)')); ?>
<?php endif; ?>
<?php endwhile; ?>
I'm assuming that you defined your $category somewhere as RST mentioned?
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );
/* Query sticky posts */
$query_args = array(
'post__in' => $sticky,
'category__in'=>array($category)
);
?>
$my_query = new WP_Query($query_args); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
Related
I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
I am working on a services cpt and would like to display a list of other posts, within the same cpt on the single post.
The code I am using is:
<?php
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6
));
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
Now my question is, is there a way to add a class to the current post? The intention being to style it different to the other posts in the list.
This is very easy you can always add the class in before and after
Read this documentation https://developer.wordpress.org/reference/functions/the_title/
<?php the_title( '<div class="wrapper">', '</div>' ); ?>
Yes First get current post id and match that id with loop id and if it is same than just add class whereever you want just use below code
<?php
$current_post_id = get_the_ID();
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6, ));
?>
<ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
$class = ($current_post_id == $loop->ID) ? "current-post" : '';
?>
<li class="<?php echo $class; ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
It will add class "current-post" to only current post and in other posts it will add nothing.
style using
<style>
li{
/*all posts*/
}
li.current-post{
/*specific for the current post*/
}
</style>
Using function
<?php post_class(); ?>
E.g:
<div <?php post_class(); ?>>
<?php
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6
));
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
I've 2 post types and I want to output them alternately but without using many loops so I found this solution which does that.
However, it is not ideal as I need to output the_post_thumbnail which I find I am unable to do using this method (echo $smallPosts->posts[$i]->post_thumbnail; does nothing). Additonally I've read post_content is not the same as the_content(); - with the latter what I want to use.
Any suggestions on how I can loop through the alternating post types and have more control over the output so I can use the_post_thumnail etc.?
Below is my code that does work but just doesn't quite do what I require.
<?php $args = array(
'post_type' => 'small_post',
'posts_per_page' => 3
);
$smallPosts = new WP_Query($args);
$args = array(
'post_type' => 'full_post',
'posts_per_page' => 3
);
$fullPosts = new WP_Query($args);
for ($i = 0; $i < 3; $i++) {
if ($smallPosts->post_count > $i)
echo $smallPosts->posts[$i]->post_title;
echo '<br />';
echo $smallPosts->posts[$i]->post_content;
echo '<br />';
if ($fullPosts->post_count > $i)
echo $fullPosts->posts[$i]->post_title;
echo '<br />';
echo $fullPosts->posts[$i]->post_content;
echo '<br />';
}
?>
This is my solution which outputs both post types and using the time published they can be alternated and I can use the_thumbnail( ); and other functions I need. Additionally I've used if statements to add classes as the different post types need to be styled differently.
<ul>
<?php
$args = array(
'posts_per_page' => 10,
'post_type' => (array('small_post','full_post')),
);
query_posts($args); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( get_post_type( get_the_ID() ) == 'small_post' ) { ?>
<li class="article small-post" style="">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(''); ?>
</a>
<?php endif;?>
<a href="<?php the_permalink(); ?>">
<h3>
<?php the_title(); ?>
</h3>
</a>
<p><?php the_excerpt(); ?></p>
</li>
<?php } ?>
<?php if ( get_post_type( get_the_ID() ) == 'full_post' ) { ?>
<li class="article full-post" style="">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(''); ?>
</a>
<?php endif;?>
<a href="<?php the_permalink(); ?>">
<h3>
<?php the_title(); ?>
</h3>
</a>
<p><?php the_excerpt(); ?></p>
</li>
<?php } ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</ul>
Please let me know why Next and Previous Post links are not working on the following code? I am trying to display only one post at a time. I have tried to check on different post but not able to find something similar to what I have. Please guide...
<?php
$args = array( 'numberposts' => 1 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<?php the_content(); ?>
<?php endforeach; ?>
<?php comments_template(); ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
<div class="next"><?php previous_posts_link('NEXT POSTS'); ?></div>
</nav>
I think I had the same problem as you, simply adding this will make the link go to previous_post_link or next_post_link
<?php previous_post_link( '%link','Previous' ) ?>
<?php next_post_link( '%link','Next' ) ?>
Best of luck
According to the get_posts documentation, you should use the following method to display prev/next links:
<?php
$postlist = get_posts( 'orderby=menu_order&sort_order=asc' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->
For example I have some category that includes five posts. When I select the post, need to display the rest four posts from category in which it situated.
<?php
$infocat = get_the_category();
$info = $infocat[0]->cat_ID;
$array = "orderby=rand&showposts=10&cat=$info";
query_posts($array);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a class="permlinccat" href="<?php the_permalink() ?>" title="Перейти к посту: <?php the_title(); ?>" ><?php the_title(); ?></a>
<?php endwhile; else: ?>
<?php endif; wp_reset_query(); ?>
Tried this function - almost good, but it get only titles of the posts
found a solution...
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category- >term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5 //
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Current category</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ? >"><?php the_title(); ?>
<img src="<?php echo first_image() ?>"title="<?php the_title(); ?>" alt="<?php the_title(); ?>"/>
</a>
<?php
}
echo '</ul>';
}
}
?>
Try this :) Just simply get the current category and exclude the current post from the query .
<?php
$cat = get_the_category();
$catOK = $cat[0]->cat_ID; //if multiple categories
$postID = $post->ID;
query_posts(
array(
'cat' => $catOK,
'post__not_in' => array($postID)
)
);
while (have_posts()) : the_post(); ?>
YOUR HTML CODE
<?php endwhile; ?>