I don't know why but when I want to display the posts with comment opened, it shows posts which are among the last 10 posts (depend how much is in READING menu), so posts which are in the first page.
My code :
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php
$PostID = get_the_ID();
if (comments_open( $post_id )) { ?>
<?php p2_load_entry(); ?>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
This is my file :
<?php
/**
* Static page template.
Template Name: test
*
* #package P2
*/
?>
<?php get_header(); ?>
<ul id="postlist">
<?php
query_posts( $args );
while ( have_posts() ) : the_post(); ?>
<?php
$PostID = get_the_ID();
if (comments_open( $post_id )) { ?><?php p2_load_entry(); ?>
<?php } ?>
<?php endwhile;
wp_reset_query();
?>
</ul>
<?php get_footer(); ?>
Related
I need to exclude first post from wordpress loop on archive.php page. The loop code is this.
<div class="article-container">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'archive' ); ?>
<?php endwhile; ?>
</div>
There are several ways. You can use a counter or boolean variable. This example uses the latter (I don't know what you are using 'global $post_i; $post_i = 1;' for but you could use $post_i as a counter, increment it in the while loop and use a condition, if that was your intention):
<div class="article-container">
<?php global $post_i; $post_i = 1; ?>
<?php $show_post = false; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($show_post === false): ?>
<?php $show_post = true; ?>
<?php else: ?>
<?php get_template_part( 'content', 'archive' ); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
Then hook into the 'pre_get_posts' and offset the query by 1. Add this to your functions file:
add_action('pre_get_posts', 'offset_posts_wordpress_archive', 50);
function offset_posts_wordpress_archive($query){
if(!is_admin() && $query->is_archive()){
$query->set('offset', 1);
}
}
I am using wp-paginate plugin for pagination on my wordpress site. The pagination links are showing fine. The problem is every page shows a blank page.
I am using following code to call pagination. I am new to wordpress so sorry for any mistakes..
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php //twentytwelve_content_nav( 'nav-below' ); ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentytwelve_content_nav( 'nav-below' );
}
?>
You have stared if block but not close it. Enable errors you will get error in php
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php //twentytwelve_content_nav( 'nav-below' ); ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentytwelve_content_nav( 'nav-below' );
}
endif; //<--------------add end if
?>
Finally I found the solution. There was a page named page.php on my server's root folder. I deleted it and problem solved.
I want to create custom page to show list title post by tag.
Example:
This title post by tag "handphone"
Title post
Title post
Title post
...etc
Any have code for this problem?
Create a template for your page
write the postcode below into the template
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>5, 'tag' => $brand_name);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
?>
after that assigne the template from admin end to your page and lets see it's wwork or not .
Create a php file in your theme directory. You can give any name. And use code something like this. Replate tag_name by your desire tag. And then create a page. Set the template. You will see the list.
<?php
/**
* Template Name: Title by Tag
*
*/
get_header();
$args=array('posts_per_page'=>5, 'tag' => 'tag_name');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Create page and add below code to your page
<?php
/**
* Template Name: Get title by tag
*
*/
get_header();
$tagname = 'handphone';
$wp_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => $tagname,
'posts_per_page' => -1
));
if ( have_posts() ) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});
I try to display a message if there are no posts for a certain category
<?php $recent = new WP_Query("cat=9&showposts=10");
while($recent->have_posts()) : $recent->the_post();?>
<?php the_title(); ?>
<?php the_content();?>
<?php endwhile; ?>
But when I add an 'else', I get the blank screen
<?php else: ?>
message ////
<?php endif; ?>
you can do something like this
<?php
if ( have_posts() )
{
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
}
else {
echo 'Sorry, no posts were found';
}
?>