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>
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();
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC' ,
'cat' => '3',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="post-thumbnail" style="width: 250px;height: 147px;">
<?php
$attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) {
?>
<img src="<?php echo wp_get_attachment_url($image->ID);?>" > <?php
}
?> </a>
<div class="entry-header">
<h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3></br>
<div class="entry-content">
<?php the_excerpt(); ?>
<a href="<?php the_permalink();
?>">Read More</a>
</div>
</div>
</li>
</br></ul>
</article>
<?php
endwhile;
I am trying to display all post from category 3 to a particular page.
I am getting the title and excerpt correctly.But I am not getting the image correctly.
I first Used :
the_post_thumbnail();
But it didnt worked.
Then I used
$attimages = get_attached_media('image', $post->ID);
Then For I post,I got image so I printed $attimages and found empty array.
Any help
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC' ,
'cat' => '3',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="post-thumbnail" style="width: 250px;height: 147px;">
<?php $attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) { ?>
<img src="<?php echo wp_get_attachment_url($image->ID);?>" >
<?php } ?>
</a>
<div class="entry-header">
<h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</div>
</article>
<?php
endwhile;
endif;
?>
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>
I want to add a Wordpress loop for a specific category in a post template that exculdes the current post.
I was suggested to use:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
But I'm having trouble getting it to work.
My loops currently looks like this.
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Try this code.
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Use
'post__not_in' => array($post->ID)
The two code blocks are using two different techniques for a Wordpress custom loop... the first modifies the global query, and the second creates a new custom query. I've outlined both below with your loop template.
Example with suggested code, global query:
Loop through the global $wp_query object in the loop code:
<div class="video">
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p
</div>
Example with original code, custom query:
Loop through the custom query, adding 'post__not_in':
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Sorry if my original answer was unclear, I initially thought you were combining the two code blocks.
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; ?>