WP Query not limiting amount of posts that's shown - php

Basically i am trying to display the 3 most recent posts from a category on a wordpress site.
The Query gets the posts fine, but does not limit them to 3 posts on the page, it instead shows every posts in that category, Here is the complete code for the page, any ideas?
<?php
if (is_front_page( )) {
?>
<style>
.site-inner {
max-width: 100%;
}
</style>
<div class="sponsor-section">
<div class="one-third first">
<?php
$catquery = new WP_Query(array(
'posts_per_page' => 3,
'category_name' => 'general',
));
while($catquery->have_posts()) : $catquery->the_post();
?>
<h4><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h4>
<p><i><?php echo get_the_date(); ?></i></p>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<div class="one-third">
<!-- MAP start-->
[4web_scripts id="1"]
</div>
<div class="one-third">
<button>Book Now</button>
<br />
<?php putRevSlider("sponsors") ?>
</div>
</div>
<?php }
?>

If you write it like this it should work just fine:
$catquery = new WP_Query(array(
'post_type' => 'post'
'category_name' => 'general',
'posts_per_page' => 3,
'nopaging' => true
));
If this does not work, do you have sticky posts? They are always shown as far as I know.

Related

Show only one latest article in Wordpress

I would like to show the lastest article as a "main" one, so it can be styles differently. Underneath it there should be other latest articles, but without the newest one, so it does not show twice. I currently have this, which duplicates the latest article inside the list, but I am hoping that there should be a better solution, any help much appreciated.
<div class="main-content__container">
<article>
<h2>Lastest Article Here</h2>
<p>Subtitle</p>
<?php
$latestPost = new WP_Query(array(
'posts_per_page' => 1
));
while($latestPost->have_posts()) {
$latestPost->the_post(); ?>
<div class="article-older">
<h3 class="article-older__title"><?php the_title(); ?></h3>
<p class="article-older__description">
<?php echo wp_trim_words(get_the_content(), 20) ?>
</p>
<span><?php the_time('M d') ?></span>
<span>Read more</span>
</div>
<?php } wp_reset_postdata();
?>
</article>
<div class="article-list">
<h2>Latest Articles (Without the main one which is above)</h2>
<?php
$homepagePosts = new WP_Query(array(
'posts_per_page' => 3,
'offset' => 1
));
while($homepagePosts->have_posts()) {
$homepagePosts->the_post(); ?>
<div class="article-older">
<h3 class="article-older__title"><?php the_title(); ?></h3>
<p class="article-older__description">
<?php echo wp_trim_words(get_the_content(), 20) ?>
</p>
<span><?php the_time('M d') ?></span>
<span>Read more</span>
</div>
<?php } wp_reset_postdata();
?>
<p class="btn">View All Articles</p>
</div>
</div>
Add an offset to the second query as recommended by #CBroe
$homepagePosts = new WP_Query(array( 'posts_per_page' => 3, 'offset' => 1 ));

Return to first slide when passing end of image carousel

I have this image carousel. Currently the slides run correctly (they are 4) but once the 4 logos are finished, the slider becomes empty.
How can I get it back to the first slide every time to start over? You can view the slider on the homepage: fintechdistrict.com
<h6>OUR PARTNERS:</h6>
<?php
$incrementalvartwo = 20;
$args = array(
'numberposts=1',
'communitypartners',
'post_type' => 'custom_post_commun',
'posts_per_page' => 7,
'orderby' => 'rand',
'category_name' => 'communitypartners');
$posts = get_posts($args);
foreach($posts as $post): ?>
<?php $incrementalvartwo = $incrementalvartwo + 1; ?>
<div class="c-homepage-single-com-partner" id="<?php echo $incrementalvartwo; ?>">
<!--<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >-->
<?php the_post_thumbnail('post-thumbnail', ['style' => 'max-height: 9vw;' ]); ?>
<!--</a>-->
<div class="clearfix"></div>
</div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata();?>
</article>

How to make php post loop with category icons

So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>

How do you print an entire post into a page (wordpress)

Hi I've made a custom post type 'work_fields' that calls in information from yet another custom post type 'members' into the post, and now I'm trying to make a PAGE TEMPLATE that shows a list of the titles of custom post type 'work_fields', and when you click a title, the whole post('work_fields') will show up on a div called 'single-post-container' below the titles. right now I've got everything working fine, but I want to display a post in the div 'single-post-container' when the page loads. (as of now, just the titles of the posts are displayed and there is nothing in the div). How do I get the div to display the most recent post of custom post type 'work_fields' on page load? This is the code for the custom page template.
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array('post_type' => 'work_fields',);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
//THIS IS WHERE THE POST CONTENTS SHOWS BUT I WANT THE MOST RECENT POST TO BE HERE ON PAGE LOAD, BEFORE I CLICK ANY OTHER POST//
</div>
Thank you! Your help is much appreciated!
Just use the WP_query twice by getting recent posts in the arguments,
$args2 = array('post_type' => 'work_fields', 'orderby' => 'ID', 'order'=> 'DESC' , 'posts_per_page' => 5);
$query2 = new WP_Query( $args2 );
?><div id="single-post-container"><?php
// The Loop
if ( $query2->have_posts() ) {
echo '<ul>';
while ( $query2->have_posts() ) {
$query2->the_post();
echo '<li>' . get_the_content() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
}
?></div><?php
Put the div outside loop. It will show the content of recent 5 posts.
If everything above the div single-post-container is working fine then for this specific div you can load most recent post by using code below. Be sure to reset previous post data using wp_reset_postdata()
Codex Documentation. https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
So I just recreated the post type markup on my page. I'm sure there's a better way to do this but for times sake I had to at least make it work. I also tried using a jquery onclick function and just click the first title after everything loads, but there was an error that just kept pushing all of the titles so I pretty much gave up.
here's the code
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array(
'post_type' => 'work_fields',
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
<?php
$args2 = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$query2 = new WP_Query( $args2 );
if ( $query2->have_posts() ) : ?>
<?php $post = get_post($_POST['id']); ?>
<div id="single-post post-<?php the_ID(); ?>">
<?php while ( $query2->have_posts() ) : $query2->the_post(); ?>
<div class="row section">
<div class="small-12 medium-7 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
<h3>소개</h3>
<p class="halfsection"><?php the_field('work_fields_intro'); ?></p>
<h3>주요서비스</h3>
<p class="halfsection"><?php the_field('work_fields_service'); ?></p>
<h3>주요실적</h3>
<p class="halfsection"><?php the_field('work_fields_accomplishment'); ?></p>
</div>
<?php endwhile; endif; ?>
<div class="small-6 medium-3 large-2 columns large-offset-1 end">
<?php
$posts = get_field('team_member');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="member_container halfsection">
<div class="member"><?php the_post_thumbnail(); ?></div>
<p class="member_name"><?php the_title(); ?></p>
<ul class="members_info">
<li><?php the_field('members_position'); ?></li>
<li><?php the_field('members_e-mail'); ?></li>
<li><?php the_field('members_phone'); ?></li>
</ul>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>

how to use Wordpress next/previous postlink within a specific category

I am new in wordpress. I have limit my post from my products category to 6, but in the reading settings of my wordpress it is by default post per page = 10. I want to do the next/previous postlink only from my products category where i have limiting it to 6 post per page:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'products',
'posts_per_page' => '6',
);
$productsBlog = new WP_Query( $args );
?>
I want to put next/previous post link within only in products category without adjusting the reading settings in wordpress and adjust the postperpage by default that is equal to 10 post per page.
Here is my fullcode with my next/previous postlink:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'products',
'posts_per_page' => '6',
);
$productsBlog = new WP_Query( $args );
$count = 0;
if( $productsBlog->have_posts() ) :
?>
<div class="container">
<ul class="image">
<div class="row row-centered">
<?php while ( $productsBlog->have_posts() ) : $productsBlog->the_post(); ?>
<div class="col-xs-4 col-box1" onMouseOver="show_title<?php echo $count ?>()" onMouseOut="hide_title<?php echo $count ?>()">
<li class="top-featured-image">
<span class="effect" id="hoverli<?php echo $count ?>"><?php the_title( sprintf('', esc_url(get_permalink())),''); ?></span>
<?php the_post_thumbnail('productsize'); ?>
<?php the_title(); ?>
</div>
<?php if($count==2) :
echo '</div>';
echo '<div class="row row-centered">';
endif; ?>
</li>
<?php $count++; endwhile; ?>
</ul>
</div>
</div>
<div class="row controller-post">
<div class="col-md-6">
<?php next_posts_link('<< Older Posts'); ?>
</div>
<div class="col-md-6">
<?php previous_posts_link('Newer Posts >>'); ?>
</div>
</div>
<br><br>
<?php endif;
wp_reset_postdata();
?>
Check out the documentation:
Next, Prev WP-link documentation
You should be able to set the third parameter to true and it will show next and previouse post relevant to that posts current category. If you need to apply a custom taxonomy, you can add the last parameter as shown below:
<?php next_posts_link('', '<< Older Posts', true, '', 'products'); ?>
and
<?php previous_posts_link('', 'Newer Posts >>', true, '', 'products'); ?>

Categories