I created a custom page template to display the latest 12 posts with their respective title and excerpt, but I tought that It would be easier if I could call this with a shortcode.
This is the loop in "post-grid.php" which calls to those 3 things.
<section class="post-grid">
<?php
$grid = array('post_per_page' => 12);
$query = new WP_Query( $grid );
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="grid-posts">
<h2><?php the_title(); ?></h2><br>
<?php the_post_thumbnail('featured'); ?><br>
<?php the_excerpt() ?><br>
</div>
<?php endwhile; // end of the loop. ?>
</section>
How can I create a shortcode that executes that loop?
I know how to add a shortcode using
add_shortcode('postGrid', 'postGrid');
function postGrid()
{
//Code here
}
But im not sure how to implement the above as a function. I appreciate your help!
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $page,
);
query_posts($args);?>
hope this will help you :)
Point related to add Post Thumbnail:
// check if the post has a Post Thumbnail assigned to it.
<?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_content(); ?>
Hope this help you :)
Since you aren't editing any code - you are creating your own - just put all that code in the call back function as is and it should work.
add_shortcode('postGrid', 'postGrid');
function postGrid()
{
<section class="post-grid">
<?php
$grid = array('post_per_page' => 12);
$query = new WP_Query( $grid );
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="grid-posts">
<h2><?php the_title(); ?></h2><br>
<?php the_post_thumbnail('featured'); ?><br>
<?php the_excerpt() ?><br>
</div>
<?php endwhile; // end of the loop. ?>
</section>
}
Related
on my WordPress front-page.php theme-page i use the WordPress Loop to show a number of Youtube Videos. I use acf to get the Youtube-URL and create the iframe from the url with the function convertYoutube. Now when i execute the loop each item is showing 5 videos (number of posts im querying) and each video, also the videos from the other posts. What am i doing wrong?
<?php get_header(); ?>
<main>
<div class="inner">
<div class="video-wrapper">
<?php
// video function
function convertYoutube($string) {
return preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
"<iframe src=\"//www.youtube.com/embed/$2?rel=0\" allowfullscreen></iframe>",
$string
);
}
// args
$args = array(
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => 5,
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="video">
<?php
$title = get_the_title();
echo $title;
$videoURL = get_field('video_url');
$videoEmbedCode = convertYoutube($videoURL);
echo $videoEmbedCode;
?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
</div>
</main>
I am trying to create something like our menu section in http://www.naijacanteen.com. I have created that section successfully, however I want to only display six item and have a view all link. Any idea how I can do that? Here is my code:
<?php
if(has_post_thumbnail())
the_post_thumbnail();
?>
</div>
<h3>
<?php the_title();?></h3>
<p>
<?php the_content();?></p>
</div>
</div>
<?php endwhile;?>
For create you have to loop through all post.Its better to use shortcode or template to make it flexible.I have always use a shortcode.
How to create short code
You have to open you functions.php and add below code
function add_resturant_menu(){
$args = array( 'post_type' => 'resturant_menu_item', 'posts_per_page' => 6 );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo "<h2>".the_title()."</h2>";
echo "<div class='entry-content'>";
the_content();
echo "</div>";
wp_reset_postdata();
endif;
return;
}
add_shortcode('add_resturant_menu','add_resturant_menu');
How to use in PHP file:
It simple to add code inside the php file using
<?php do_shortcode('[add_resturant_menu]'); ?>
How to use in WYISWYG editor:
add line code into editor.
['add_resturant_menu']
<?php
$wpb_all_query = new WP_Query(array(
'post_type'=>'post', 'post_status'=>'publish',
'posts_per_page'=>-1
));
if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
$cats = get_the_category();
if ($cats[0]->cat_name !== 'Coaching' && $cats[0]->cat_name !== 'Jobs') { ?>
<div class="callout horizontal word-wrap"
data-category="
<?php
echo $cats[0]->cat_name;
?>">
<?php the_post_thumbnail() ?>
<h5><?php the_title(); ?></h5>
<?php the_content(); ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
What is the most simplist way to add pagination to these post in wordpress showing 5 posts per page?
I then want to use ajax to replace the pagination to update the posts that are shown.
I'm looking for an answer that also explains the posts_per_page as I thought this is what I would need to make my pagination .
With this args you can display posts.
$posts_per_page = get_option('posts_per_page');
$args = array('paged'=>$paged,
'posts_per_page'=>$posts_per_page,
'post_type'=> 'post',
'post_status'=>'publish'
);
query_posts($args);
Then use Wp pagination
Go to your WP-Admin => Setting => Reading
And set Blog pages show at most = number post you want show in one page
That's it!
I try to build an option for the user to highlight "related posts" to be shown at the bottom of a single post, so I put this code at the bottom of "content-single.php":
<div class="related-post">
<?php
$args = array( 'post__in' => array(38328, 38359, 21321) );//it's going to be the user choice, for example, I put three id's
$related_query = new WP_Query($args);
if ($related_query):
echo '<div class="row">';
while ($related_query->have_posts()): $related_query->the_post();?>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()):
the_post_thumbnail();
endif;
?>
<?php the_title() . '<br/>'; ?>
</div>
<?php endwhile;
echo '</div>';
wp_reset_postdata();
?>
</div>
But in some posts, I get 8 diefferent posts, in some posts I don't get any posts (though I put only three id's in post__in).
I guess the problem is that I try to loop inside the main loop.
I tried with query_posts and with get_posts, doesn't work.
Any help will be appreciate!
I think that is related to the missing post_type argument. Yet you need some corrections in your loop structure:
<div class="related-post">
<?php
$args = array( 'post_type' => 'your_post_type', 'post__in' => array(38328, 38359, 21321) );
$related_query = new WP_Query($args);
if( $related_query->have_posts() ){ ?>
<div class="row"><?php
while( $related_query->have_posts() ){ $related_query->the_post();?>
<div class="col-sm-4"><?php
if( has_post_thumbnail() ) the_post_thumbnail();
the_title();?>
</div><?php
}
wp_reset_postdata();?>
</div><?php
}?>
</div>
UPDATE
Please try to add 'posts_per_page' => 3 to the args array.
I have a solution to this post. Hope so you are wrong with the query post.
<div class="related-post">
<?php
$args = array(
'post_type' => 'your_post_type',
'post__in' => array(38328, 38359, 21321),
'post_status'=>'publish'
);
$related_query = new WP_Query( $args );
?>
<?php if ( $related_query->have_posts() ) : ?>
<div class="row">
<?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
<div class="col-sm-4">
<!-- do stuff ... -->
the_post_thumbnail();
the_title();
</div>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
</div>
Hope so it helps you to fetch out your Output Required.
Make sure you have post in the ID that you have mentioned in the post_in section.
Well, I manage to solve this by adding this 'ignore_sticky_posts' => 1 to the arguments. thanks #Pieter Goosen
I'm having hard time with this problems since I cant figure it out.
I'm using 2 WP_Query loops for custom post types (slider and portfolio) on the same page.
I also created a custom meta box for both custom post types.
So here is the code for index.php which Im using as Home template for displaying slider and portfolio items:
<?php
/*
Template Name: Home
*/
?>
<?php get_header(); ?>
<div id="header-container">
<div id="header">
<?php rm_slider(); ?> // This is where Im calling slider function to display the slider.
</div>
</div>
<div id="content">
<div class="container">
<?php $loop = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => -1
));
?>
<?php if ($loop->have_posts()) { ?>
<ul class="services">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
<?php if (has_post_thumbnail()) : the_post_thumbnail(); ?>
<?php else: ?>
<p>No portfolio image</p>
<?php endif; ?>
<h3><?php the_title(); ?></h3>
<p>Client: <?php echo get_post_meta($post->ID, '_project_client', true); ?></p>
<p>Client website: <?php echo get_post_meta($post->ID, '_project_client_url', true); ?></p>
</li>
<?php endwhile; } ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
And here is the code for slider.php:
<?php
// create slider markup
function rm_slider() {
$slider_loop = new WP_Query(
array(
'post_type' => 'slider',
'posts_per_page' => -1
));
if ($slider_loop->have_posts()) { ?>
<div id="slider">
<div class="slider-container">
<?php while ($slider_loop->have_posts()) : $slider_loop->the_post(); ?>
<div>
<?php if (has_post_thumbnail()) : the_post_thumbnail(); ?>
<?php else: ?>
<p>No slider image</p>
<?php endif; ?>
<div class="slide-info">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
$slide_url = get_post_meta($post->ID, '_slide_url', true);
if ($slide_url != '') { ?>
<?php echo $slide_url; ?>
<?php } else { echo 'empty?'; ?>
<?php
}
?>
</div>
<?php endwhile; ?>
</div><!-- .slider-container -->
</div><!-- #slider -->
<?php }
wp_reset_query();
}
?>
Im sure that the actual content from custom meta boxes is there, because when I only use 1 loop, it displays perfectly. But when using both loops, it only displays custom post meta only for the portfolio section. Im struggling with this problem whole day, please help me! Thanks :)
Strange, try changing this:
$slide_url = get_post_meta($post->ID, '_slide_url', true);
echo get_post_meta($post->ID, '_project_client', true);
for this:
$slide_url = get_post_meta(get_the_ID(), '_slide_url', true);
echo get_post_meta(get_the_ID(), '_project_client', true);
You could also try getting all post meta just to see if its all there.
$meta = get_post_meta( get_the_ID( ) );
print_r($meta); // prints the meta array to the screen, check your data is there.
As far as I know, after every WP_Query() you should use:
wp_reset_postdata();
NOT wp_reset_query();. Have a try with this.
wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function. As noted in the examples below, it's heavily encouraged to use the pre_get_posts filter to alter query parameters before the query is made.
and
wp_reset_postdata() is used to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query. It restores the $post variable to the current post in the main query.
And I also suggest you to try changing the possible redundant variable name like $loop to something like $portfoliowLoop etc.