Show only one latest article in Wordpress - php

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 ));

Related

WP Query not limiting amount of posts that's shown

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.

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>

Get 2 posts per slide, Unslider, Wordpress

I'm trying to get two posts per slide in my Wordpress theme. I get posts from the category "news" and want to display about 5 pages with 2 posts. At this moment it displays just one.
This is the code I've got at the moment.
<div class="banner">
<ul>
<?php $logo = new WP_Query( array( 'category_name' => 'news', 'posts_per_page' => 10 ) ); ?>
<?php while ($logo->have_posts()) : $logo->the_post(); ?>
<?php $do_not_duplicate[] = $post->ID; ?>
<li class="news-item col-1-1 no-pad">
<div class="description">
<h4><?php the_title(); ?></h4>
<p class="post-date">Gepubliceerd op: <?php echo get_the_date('d-m-Y'); ?></p>
<p><?php content(160); ?></p>
</div>
<a class="button-med" href="<?php echo get_permalink(); ?>">Lees meer</a>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</div>

Querying Advanced Custom Fields; want to paginate the results

Not sure if this pertains specifically to Advanced Custom Fields, but I'll keep this short. I'm relatively new to PHP and want to know the best way of approaching this task. Also, this is a WordPress (4.1.1) environment.
I'm sending a query for all post types with an associated category type to pull in and display. If the query can find posts, it produces an <article> block with additional html for each result. Not to complicated right?
Now, I want to create a pagination element if/when the query produces greater than 8 results.
Environment with the front-end of the query results can be found:
http://test-hdwg.pantheon.io/news/
Template being utilized for this page (news.php) contains the following:
<?php /* Template Name: News */ ?>
<?php get_header(); ?>
<main id="primary" class="content-area">
<div class="jumbotron">
<div class="row">
<div class="intro">
<p><?php the_field('news_heading') ?></p>
</div>
</div>
</div>
<div class="news">
<div class="row news-articles">
<div class="column-wrapper">
<div class="small-4 columns news-left-column">
<div class="select-dropdown">
<button href="#" data-dropdown="dropdown-items" aria-controls="dropdown-items" aria-expanded="false" class="dropdown">Select a news category</button>
<ul id="dropdown-items" data-dropdown-content class="f-dropdown" aria-hidden="true" tabindex="-1">
<?php $args = array(
'exclude' => '',
'title_li' => __( '' ),
'show_option_none' => __( '<li>No categories</li>' ),
'taxonomy' => 'category',
'child_of' => 2,
'current_category' => 0
); ?>
<?php wp_list_categories($args); ?>
<li class="cat-item cat-item-all-news">All News
</ul>
</div>
<div class="news-block">
<span class="news-block-heading">Email Newsletters</span>
<div class="news-inner-block">
<?php if( have_rows('newsletters_list') ): ?>
<?php while( have_rows('newsletters_list') ): the_row(); ?>
<span class="year"><?php the_sub_field('newsletter_year'); ?></span>
<?php if( have_rows('newsletter_resource') ): ?>
<ul>
<?php while( have_rows('newsletter_resource') ): the_row(); ?>
<li><?php the_sub_field('newsletter_title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="small-8 columns news-right-column">
<?php query_posts( 'post_type=post&cat=' ); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article class="large-6 columns ui-article-block">
<h3><?php echo get_the_date( 'd M Y' ); ?></h3>
<hr/>
<h2><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></h2>
<h1><?php the_title(); ?></h1>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 175) . "..."; // set a character cut-off at 175 characters
?>
</p>
Read More
</article>
<?php endwhile; ?>
<?php else : ?>
<div class="large-8 columns">
<p class="lead">Well this is embarrassing. There are currently no news articles.</p>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
</main>
<?php get_footer(); ?>
Just to be clear, this is not an issue, but more-so a request from more experience developers on how to approach a task utilizing Advanced Custom Field elements (if that matters).
Any help would be greatly appreciated!
EDIT
This edit is after clarification from OP on nature of the question.
The tutorial here, explains how to handle numeric navigation in posts at a beginner level.

WordPress Dividing posts over columns dynamically

I've been building a WordPress template based on inuitcss to get a responsive page.
In this template, I'm attempting to get the WP posts from a certain category, and divide them over a predefined amount of columns. To achieve this I'm counting the amount of posts from category and compute the necessary data for the posts to split up evenly.
I've been using this (last on the page) example and updated several deprecated functions.
The issue I am facing at the moment is that all columns are loading the same posts, and they are only loading the last two instead of all available posts in the category.
The code below is what I am using at the moment, the result can be found at http://www.alikivanderkruijs.com/wp (click on 'Projects').
I've been sitting on this for a while and can't seem to get it right. I hope someone can help me out!
<?php
$proj_posts = get_posts('cat=15');
$count_proj_posts = count($proj_posts);
$split_proj_posts = round($count_proj_posts/2);
?>
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=0'); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count1 = 0; if ($proj_count1 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<?php $proj_count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php rewind_posts(); ?>
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=' . $split_proj_posts . ''); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count2 = 0; if ($proj_count2 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<?php $proj_count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
You are overcomplicating it, simply get all the posts and count up until half, then echo out the closing and opening tags to create a new row:
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
$args = array(
'numberposts' => -1,
'posts_per_page' => '',
'offset' => 0,
'category' => '15',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true);
$posts_array = get_posts($args);
$split_at = round(count($posts_array)/2)-1;
$count = 0;
foreach ($posts_array as $post_object):setup_postdata($post);
?>
<div <?php post_class(); ?>>
<h4><a href="<?php the_permalink() ?>" rel="bookmark"
title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h4>
<?php the_content(); ?>
</div>
<?php
//IF we are halfway through, close 1st row and
start a new one
if ($count == $split_at) {
?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
}
$count++; ?>
<?
endforeach;
?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
</div>
I have left the full args array, incase it is usefull to others, but you only need to use the cat argument in your case.
EDIT ok, setup_postdata is flawed, just checked the docs and it doesnt work as expected. The suggested workaround is to set the global post object like so:
setup_postdata( $GLOBALS['post'] =& $post_object )
Change that section should fix that issue. The count problem is still weird though.

Categories