I'm an apprentice WP developer and am trying to wrap up a project for work. I'm trying to loop in the first 6 or so posts on the homepage, but every time I add the loop the page breaks and loads empty (except for the header).
I'm using the HTML5 blank WordPress theme as the basis for the custom theme I'm developing and using ACF to create a custom page builder. The homepage is loading from page.php. Running WP 4.9.5.
<?php $args = array ( 'post_type' => 'post' );
$post_query = new WP_Query($args); ?>
<?php if($post_query->have_posts()): ?>
<?php while($post_query->have_posts() : $post_query->thepost(); ?>
<h2>THIS IS A TEST</h2>
<?php wp_reset_postdata(); endwhile; ?>
<?php else :?>
<p>Whoops. No posts.</p>
<?php endif; ?>
This is your problem: $post_query->thepost();. It should be: $post_query->the_post();. And you're missing a parenthesis after while($post_query->have_posts().
Also, wp_reset_postdata() should be called after the while loop.
Here's the updated code (with a couple of tweaks):
<?php
$args = array (
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if ( $post_query->have_posts() ):
while( $post_query->have_posts() ) : $post_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php
endwhile;
wp_reset_postdata();
?>
<?php
else : // No posts found.
?>
<p>Whoops. No posts.</p>
<?php
endif;
Related
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']
i want to display some posts in home page according to post type but i can't access posts and post loop returns " Home " post only ,so what is the proper way to make this happen ?
my reading settings
Front page displays : A static page
Front page :home
Posts page :blogs
Keep your reading settings the way they are.
You can create a theme file called front-page.php and use this to control your home page.
Within your front-page.php you can use get_posts to retrieve posts and then loop through them
Example:
<?php get_header(); ?>
<?php
$args = array(
'numberposts' => 10, // number of posts to return
'post_type' => 'your-post-type' // change this to the post type you want to retrieve
);
$posts = get_posts( $args );
if ( $posts ) :
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<article <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>
<?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 am building a basic Wordpress blog which is build on Duplex theme.
I Would like to do some minor customization to the site where in the site has an Instagram widget after two posts and then continue the posts.
Just to simplify the order of the page would be :
2 posts.
Instagram widget
Continue from 3rd post.
The Front page displays set to posts like the way I want.
How can I achieve getting the Instagram widget in between of the posts without disturbing the flow?
What you do is run the loop two times to display the first posts, reset it and then start it again after your widget - skipping the two first posts.
You may achieve this like so:
First run the loop, displaying the two first posts.
// The Query
$the_query = new WP_Query( array( 'posts_per_page' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
//Display post
}
Reset the query, and display your widget
/* Restore original Post Data */
wp_reset_postdata();
//Your widget here.
Display the next posts, skipping the first two.
// The Second Query
$the_query = new WP_Query( array( 'posts_per_page' => 10, 'offset' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
//Display post
}
Thanks a ton #danjah! Below is my code
<?php get_header(); ?>
<div id="content">
<div class="posts clearfix">
<?php if ( is_search() ) { ?>
<h2 class="archive-title"><i class="icon-search"></i> <?php echo $wp_query->found_posts; ?> <?php _e('Results for','hyphatheme'); ?> "<?php the_search_query() ?>" </h2>
<?php } else if ( is_tag() ) { ?>
<h2 class="archive-title"><i class="icon-tag"></i> <?php single_tag_title(); ?></h2>
<?php } else if ( is_day() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date(); ?></h2>
<?php } else if ( is_month() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('F Y'); ?></h2>
<?php } else if ( is_year() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('Y'); ?></h2>
<?php } else if ( is_category() ) { ?>
<h2 class="archive-title"><i class="icon-list-ul"></i> <?php single_cat_title(); ?></h2>
<?php } else if ( is_author() ) { ?>
<h2 class="archive-title"><i class="icon-pencil"></i> <?php echo get_userdata($author)->display_name; ?></h2>
<?php } ?>
<?php
if ( have_posts()) :
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
else:
/*
* If no posts are found in the loop, include the "No Posts Found" template.
*/
get_template_part( 'content', 'none' );
endif;
?>
</div><!--END .posts-->
<?php if ( hypha_page_has_nav() ) : ?>
<!-- post navigation -->
<?php $post_nav_class = ( get_option( 'hypha_customizer_infinite_scroll' ) == 'enable' ) ? ' infinite' : '' ; ?>
<div class="post-nav<?php echo $post_nav_class; ?>">
<div class="post-nav-inside clearfix">
<div class="post-nav-previous">
<?php previous_posts_link(__('<i class="icon-arrow-left"></i> Newer Posts', 'hyphatheme')) ?>
</div>
<div class="post-nav-next">
<?php next_posts_link(__('Older Posts <i class="icon-arrow-right"></i>', 'hyphatheme')) ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if ( is_single() ) { ?>
<!-- comments template -->
<?php if ( 'open' == $post->comment_status ) : ?>
<?php comments_template(); ?>
<?php endif; ?>
<?php } ?>
</div><!--END #content-->
One approach would be the one mentioned would be the one by #danjah wehre you're creating a secondary loop on the page to get the first two posts. Since you're new to wordPress, lets start at the beginning.
All of your templating revolves around the loop. The loop works with the query already defined on the page. wordPress has various default queries depending on which template its loading (a post, an archive, a single post, a page, the index)
The Loop
The Tempalte Heirarchy
So a) wordpress defines the query based on the route you're on, and b) you use the loop to get at it. BUT. You can alater the query, override it, or run additional queries.
Have a loop at WP_Query
Lets loop at the standard loop. In your index.php you probably have something like :
<!-- Start the Loop. -->
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<!-- use markup or template helpers in here -->
<?php endwhile;
endif; ?>
While you could run a secondary query before the main loop, insert the thing you want, and then alter the main loop to skip those first two, the second way would be to, rather than creating another loop and altering the main one, why not rather keep a count. You could do this with a variable that you increment (its just a standard php while loop after all), But I believe that you can also get the current post index from the global $wp_query object.
$index = 1;
<!-- Start the Loop. -->
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<!-- use markup or template helpers in here -->
<?php if ($index == 2) : ?>
<!-- do something after the second post -->
<?php endif; ?>
<!-- increment the index -->
$index++;
<?php endwhile;
endif; ?>
I believe you can get the index as well with $wp_query->current_post, but I haven't done this for a while, and haven't tested this.
Hope it helps!
I am new in PHP and in WordPress world and I having a problem with the creation of a loop that show only the post having a specific tag.
In homepage I would create a loop that show me only the articles that have setted a specific tag, so I have implement the following PHP loop for wordpress:
<div id="column2">
<?php
query_posts( 'tag=sasha' );
if(have_posts()):
while (have_posts()): the_post();
?>
<?php endwhile; else: ?>
<?php endif; ?>
</div> <!-- end column2 -->
I have an article in which I have setted a tag as: sasha
The problem is that don't work and my column2 div still remain empty. Why? Can you help me?
Tnx
Andrea
Here's how it should look for you when using WP_QUERY:
$args = array('tag' => 'sasha');
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
wp_reset_postdata();