I have managed to get recent posts showing on my homepage which is working well. They only problem i have is that the full post and not the excerpt is showing.
<div id="home-news-container">
<?php query_posts('cat=#&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-post-container">
<div class="home-post-thumb">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail('large_wide'); ?>
</a>
</div>
<div class="home-post-copy">
<h4>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>
<h5>
<?php the_date(); ?>
</h5>
<?php echo the_excerpt(); ?>
<div class="home-news-readmore">
read more
</div>
</div>
</div> <!-- end home-post-container -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="home-news-readmore news-extra">
more news
</div>
</div> <!--- end home-post-container -->
I don't understand what the problem is to be honest. I created a new full width template for the homepage which i thought might be causing it but its not. Bit stumped to be honest. Any help would be greatly appreciated
Use wp_get_recent_posts to get recent post
Use substr to display some limeted character of post character
<?php
$args = array (
'numberposts' => 3,
'post_type' => 'your post type name'
);
// the query
$recent_posts = new wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
echo substr($recent["post_content"],0,100);
}
?>
Related
I recently developed a Wordpress theme, this theme has little bit complex custom post type.
Below is my custom post query code. I would like kind of a else content whenever the request doesn't return any post. Its currently just blank.
I am not a PHP developer though, would someone give me a hand adding such a alternative content ?
Thank you.
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
//second query - posts
// Define the query
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
// output the term name in a heading tag
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<!-- row -->
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php } // end of check for query having posts
// use reset postdata to restore orginal query
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
<!--==== Top catogory section ====-->
<!-- front deal closed -->
</div>
elseif(!$query->have_posts()){//HTML here}
or
else{ //HTML here}
right after the end of check for your query , if you want to follow an OOP rule to make your code cleaner , move everything into a function inside function.php file and call the function there instead
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php }else{
// Add your code here for else part
}
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
</div>
I am working on a site with a page that has multiple sections, each section has multiple loops featuring multiple categories. I use Ajax Load More plugin to load new posts for each sections. The issue is when I click on Load More, it loads both the posts already shown and the one that hasn't been shown. I want it to load only new posts not already shown.
Here is the shortcode I used:
echo do_shortcode('[ajax_load_more container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
Here is the loop on on of the sections
<div class="row">
<div class="col-lg-12 col-sm-12">
<div class="music_box bg-color1">
<div class="music_box_top">
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post__in' => $sticky,
'posts_per_page' => 1,
'cat' => 34
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<a href="<?php the_permalink(); ?>">
<div class="fashion_box_thumb">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full', array() );
}
?>
</div>
</a>
<div class="fashion_box_text">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<p><?php the_excerpt(); ?></p>
<div class="post_cont_icons">
<span class="fa fa-comments cmnt"> <?php comments_number('0','1','%'); ?></span>
<?php echo getPostLikeLink(get_the_ID());?>
<span class="matchtime2"><i class="fa fa-clock-o"></i> <?php the_time();?><br></span>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<div class="clear"></div>
</div><!--music_box_top-->
<div class="fashion_box_bottom">
<?php
$args = array(
'post__not_in' => get_option( 'sticky_posts' ),
'posts_per_page' => 4,
'cat' => 34
);
$sticky_query = new WP_Query( $args );
$count = 0;
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) :
?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php elseif ($count == 2) : ?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php elseif ($count == 3) : ?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php elseif ($count == 4) : ?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<div class="clear"></div>
</div><!--music_box_bottom-->
</div><!--music_box-->
</div><!--col-lg-12-->
<?php else :
get_template_part( 'woodclefpro/pro_template3' );
endif;
endwhile;
wp_reset_postdata(); ?>
</div><!--row-->
<div class="row">
<?php
echo do_shortcode('[ajax_load_more container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
?>
</div>
This is for those that might come across the question above and are facing the same issue that I was. Here is how I solved it.
Add the code below right before endwhile
$do_not_duplicate[] = $post->ID;
Add this inside your shortcode: post__not_in="'.$post__not_in.'"
Then your final shortcode looks like this:
echo do_shortcode('[ajax_load_more ajax_load_more post__not_in="'.$post__not_in.'" container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
Not quite right. The fact is that on the page of one of the Addons to this plugin say that the template output single entry, for example "single.php" should be nothing but a shortcode. All content single.php should be placed in the template used by the plugin. Sorry for the crooked English.
https://connekthq.com/plugins/ajax-load-more/add-ons/single-posts/
Note: Ajax Load More will take care of loading ALL posts, including
the initial post when a user lands on the page. All that should remain
in your single.php loop is the ajax_load_more shortcode (as seen
above).
I'm building a theme and on my category.php page I want to show several full posts (let's say 3, but need to be able to change this to 2 or 1 easily), and then the rest of the posts in the category as title links.
I have quite a bit of HTML in my loop for styling my posts and adding custom fields so sorry about all the code, but this is what my category.php page looks like now. I've tried a few things that haven't worked so have edited this to show my original code which just has a normal list of posts. I'm somewhat new to editing The Loop so would appreciate as much explanation/clarity as possible.
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><?php the_title(); ?></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span><br />
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span><br />
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
You can achive above thing using following code:
First you have to loop all post and and put counter when it reach more then 2 its stop to print a content.but title will be there always.
<?php $countPost=1;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<?php the_title(); ?></h2>
<?php if($countPost>2) : /*Condition for Content*/
the_content();
endif;
?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','« Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries »','') ?>
</div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
</div>
For more details please refer :
https://codex.wordpress.org/The_Loop_in_Action
I figured out a bit of a workaround solution on my own, although it relies on using plugins/widgets which isn't what I'd prefer.
I simply set the Reading settings to display 2 posts, and then below the Loop I added a widget area and used the Recent Posts Extended widget to display a list of titles/links. This widget allows you to skip a certain amount of posts in the list, so I set it to start at post #3. There was no option to show posts from the current category only, so I had to use the Widget Context plugin as well and make individual widgets with a specific category to show on each corresponding category page. As I said, a bit of a convoluted solution, but the end result is exactly what I wanted to achieve.
I'm trying to loop only custom post types using the WP loop, but only shows the ones I give by ID.
This is my "normal" loop right now:
<?php $args = array(
'post_type' => 'referenties', 'posts_per_page' => 5, 'order' => 'DESC',
); ?>
<?php
$number = 0;
query_posts($args);
if(have_posts()):
?>
<!-- /Carousel script -->
<div class="container">
<div class="carousel-loop">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<?php while(have_posts()): the_post(); ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php endwhile; ?>
</ol>
<div class="controle-buttons">
<a class="carousel-control left" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-circle-left"></i></a>
<a class="carousel-control right" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-circle-right"></i></a>
</div>
<!-- Carousel items -->
<div class="carousel-inner">
<?php while(have_posts()): the_post(); ?>
<!-- Carousel nav -->
<div class="item">
<div class="col-sm-2">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(150,150)); // Declare pixel size you need inside the array ?>
</div>
<div class="col-sm-4">
<h4><?php the_title(); ?></h4>
<?php $bedrijf = get_field('naam_bedrijf'); ?>
<?php $feest = get_field('feest'); ?>
<?php $link = get_field('mylink'); ?>
<?php echo '<p>Bedrijfsnaam: ' . $bedrijf . '</p>'; ?>
<?php $post_object = get_field('mylink');
if( $post_object ): $post = $post_object; setup_postdata( $post ); ?>
<p>Feest type: <a style="color:#ff6600" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endif; ?>
</div><?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<div class="col-sm-4 col-sm-offset-1">
<h4>Opmerking</h4>
<p><?php echo custom_field_excerpt_longer(); ?></p>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php echo '<p>' . wp_review_show_total() . '</p>'; ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
But I only want to show post ID's: 2706, 2462, 2514, 2511 and 2505.
The loop is shown in a carousel, this works fine. But I just want the ID's to show and not all posts added.
Use something like this:
<?php
$args = array(
'post_type' => 'referenties',
'post__in' => array(2706, 2462, 2514, 2511, 2505),
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//post content output goes here
}
// Restore original Post Data
wp_reset_postdata();
} else {
// no posts found
}
The post__in () argument uses array with desired post ids to retrieve.
Don't use query_posts for custom queries. Too much that can go wrong.
Hope this helps :)
I have a problem with my pagination system (wordpress). It's on my search page (display results), currently, the search system include only 2 post types, and the search system works fine, but clearly impossible to create a pagination system. I just want to create a simple previous and next page nav with the number of the current page.
<div class="contenu cont-menu fixed-cont aff">
<div class="page p-spe">Résults :
<span class="blue"><?php the_search_query(); ?></span></div>
<div class="nombre"><?php global $wp_query; echo ' ' .
$wp_query->found_posts . ' Result(s)'; ?></div>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="article-menu">
<div class="article-menu a-m-sp">
<a href="<?php the_permalink(); ?>">
<span class="rollover r2"> </span>
<?php the_post_thumbnail(array(226,150)); ?>
</a>
<div class="right p-special">
<h2 class="h2-article-menu h2-ma"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></h2>
<p class="date"><?php the_time('j F Y'); ?></p>
<?php the_excerpt(); ?>
Know more
</div>
</div>
<div class="sep sp-s"> </div>
</article>
<?php endwhile; ?>
<nav class="nav-pages">
<?php previous_posts_link('« Previous') ?>
<span class="nombre-pages"><?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo $paged.' / '.$wp_query->max_num_pages;
?></span>
<?php next_posts_link('Next »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
<?php else : ?>
<div class="aucun-resultat">
<?php _e( 'No results' ); ?>
</div>
<?php endif; ?>
<?php
get_footer();
?>
So i add the pages nav just after the endwhile of the search results. I don't know why my system doesn't work. I'm a wordpress beginner, thank you for your help. Bernard
You don't need a custom query for this. Just add paginate_links() which generates pagination with arrows and numbers (can be configured to display as you wish).
http://codex.wordpress.org/Function_Reference/paginate_links
I update my post, because I made a pagination for 1 post type and It works fine. But now I would like to do a pagination of 2 post types. How i can change this code to make it works ?
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=4&post_type=dossiers'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>