Hi everyone so I got this piece of code to display posts but instead or rendering posts it renders the about us page can anyone help. I'm running WordPress 4.5.3
<?php get_header(); ?>
<?php
if(have_posts() ):
while(have_posts() ) : the_post();
the_title();
the_content();
endwhile;
endif;
?>
<?php get_footer(); ?>
Use This :
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
query_posts($args);
if(have_posts()): the_post();
while(have_posts()): the_post();
?>
<h4> <?php the_title();?> </h4>
<?php endwhile;?>
<?php endif; ?>
Related
I want to display pagination on category pages of WordPress theme
This is my code
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$posts = get_posts(array(
'numberposts' => 150,
'posts_per_page'=>16,
'offset' => 0,
'category__not_inint' => array($category),
'post_status'=>'publish',
'order'=>'ASC'
));
foreach($posts as $post){
setup_postdata($post);
$city_name = get_field( "city-name" );
$display = '' . $city_name . '';
}
the_posts_pagination();
wp_reset_query();
return $display;
and displays the pagination. But the results of all pages are similar to the first page.
You shouldn't be using get_posts if you need the query to be paginated.
Whilst it can be done, this is a total ball ache to achieve. Instead, you should be looking at WP_Query.
Further reading on WP_Query - WP_Query # wordpress.org
Your code could look something like the following;
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; // explain to wordpress we need this paged
$wp_query = new WP_Query(array( // the query
'post_type' => 'post',
'post_category' => '',
'post_status' => 'publish',
'numberposts' => 150,
'posts_per_page' => 15,
//'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged));
while ($wp_query->have_posts()) : $wp_query->the_post(); // the loop
// some code to make it look pretty
?>
<div class="post-grid">
<a href="<?php the_permalink(); ?>">
<h3 class="card-title"><?php the_title(); ?> </h3>
</a>
</div>
<?php endwhile;
echo ( paginate_links($args = array(
'base' => site_url().'%_%', // site_url prefix is needed for pagination on homepage
'format' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $paged,
'show_all' => false,
'end_size' => 2,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => 'Prev',
'next_text' => 'Next',
'type' => 'list',
'add_args' => false,
'add_fragment' => ''
)));
wp_reset_query();
I try to customize the course content list forming by the LearnDash plugin. Now on the lesson page in the sidebar displays just a boring list of the lessons included in the course (the current shortcode formed it). But I need to display the list of the lessons with thumbnails and other meta information from the lessons. For this purpose, I try to use this chunk of code:
<?php
$args = array(
'cat' => 153,
'post_type' => 'sfwd-courses',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
And I always see the same answer: "No Posts Sorry". Please help to sort out this issue.
Please try below code which helps you to display lessons fo specific category.
<?php
$args = array(
'post_type' => 'sfwd-lessons',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'ld_lesson_category', //double check your taxonomy name in you db
'field' => 'id',
'terms' => 26,
),
),
);
$q = new WP_Query($args); ?>
<div class="timeline"> <?php
if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
In my wordpress site, I use a custom field/meta-key called "offline".
I would like to exclude from the basic loop (in tag.php and category.php) all the posts that have the custom field/meta-key "offline" set to "true". Can someone help me? Thank you.
This is my code currently:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
... Display post content
<?php endwhile; ?>
<?php endif; ?>
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'offline',
'value' => 'true',
'compare' => '!='
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$post_id = get_the_ID();
endwhile;
endif;
So all posts with meta_key=offline and meta_value=true is not included in loop.
Exclude all the posts that have this meta-checkbox
'meta_query' => array(
array(
'key' => 'meta-checkbox',
'value' => '1',
'compare' => '<'
)
),
This worked for me.
i'm creating a blog page in my custom theme wordpress, i would like use pagination in my list posts, my query with list of posts work well but the pagination doesn't work. i see always the same first 2 posts.
page-blog.php
/*
* Template Name: Pagina Blog
*/
<?php get_header(); ?>
<?php
$posts_per_page = 2;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
//$posts_array = get_posts( $args );
$wp_query = new WP_Query( $args );
//query_posts( $args );
?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- Title -->
<h1 class="my-4"><?php the_title() ?></h1>
<h2 class="card-title">
<?php the_title(); ?>
</h2>
<!-- Content -->
<?php the_content() ?>
<!-- Pagination -->
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php endwhile;
// Reset Query
wp_reset_query(); ?>
<?php get_footer(); ?>
I think there some issue with the loop, but i'm not sure, i can see "Newer" and "Older" links but when i go on older i see the same posts, so "www.mysite.com/blog" have the same post of "www.mysite.com/blog/2" :(
Here lies your problem:
'offset' => 0,
Replace that with:
'offset' => $posts_per_page * ($paged - 1)
Alternativley, if I'm reading the documentation correctly, you can maybe just remove the offset value entirely.
I’m trying to create many WordPres post and page. I include the post and page to various category. In each category I add post and page both. In this circumstances I need to show post and page under the particular category. And I want to sort the post and page ascending or descending under the Category. I need the PHP Coding this purpose. Please Give me assistance. I have created category.php by the code bellow.
<div class="cate-top ">
<h1 class="cat-page-title"><?php printf( __( ' Your are Browsing: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>
<?php if ( category_description() ) : // Show an optional category description ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
<?php while(have_posts()): the_post();?>
</div>
<div class="category-page">
<div class="cate-inn ">
<h2> <?php the_title();?></h2>
<div class="cat-image fix">
<?php the_post_thumbnail();?>
</div>
<div class="cat-read-more fix">
<?php read_more(0);?>Read More
</div>
</div>
<?php endwhile;?>
You can use get_posts or WP_Query to get the page and post with your desired category, For example
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
You can just change your category name in args,
If you are using custom taxonomy instead default category, you may use following code
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'';
endwhile;
}
}
For more help you can VISIT, for get_post you may VISIT