Sorry, if I sound a bit naive..
I am a decent PHP developer and have been trying to learn wordpress recently.
Can anyone tell me or point to a tutorial that tells me the best way to show posts on a custom template with pagination ?
Since occasionally(or maybe most of the time, I'm not sure) you can get permalink conflicts and therefore unexpected 404 pages if you just make a custom query_posts() and then you add a pagination, with links to page-slug/page/2, page-slug/page/3, page-slug/page/n, I usually set the pagination as a $_GET parameter.
Here is an example code for that:
<?php
/*
Template Name: Custom Loop Template
*/
get_header();
// Set up the paged variable
$paged = ( isset( $_GET['pg'] ) && intval( $_GET['pg'] ) > 0 )? intval( $_GET['pg'] ) : 1;
query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 1 ) );
?>
<?php if ( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="pagination">
<?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) {
$link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
} ?>
</div>
<?php endif ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the main page</p>
</div>
</div>
<?php endif;
// Make sure the default query stays intact
wp_reset_query();
get_footer();
?>
This will create a custom Page template, called Custom Loop Template and will display the latest posts, 1 per page. It will have a basic pagination at the bottom starting from 1 to the maximum number of pages for that query.
Of course, that's just a pretty basic example, but it should be enough for you to figure the rest out.
Related
I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>
please don't give a negative feed like always and instead tell me the mistake.
I have a blog.
can i make the homepage display a summary of an article instead of the whole article?
if yes then where or how can i do it?
I have this pre-set in one of my WordPress sites. Here is what this looks like and it is in Appearance/Editor/Posts Page (home.php)
<div class="post-content">
<?php $content = get_the_content(); ?>
<?php echo wp_trim_words(strip_tags($content), 30); ?>
</div>
<a class="blog-post-read-more" href="<?php echo esc_url( get_the_permalink( get_the_ID() ) ); ?>"><?php echo esc_html( get_theme_mod( 'relia_blog_read_more', __( 'Read More', 'relia' ) ) ); ?></a>
</div>
So what this does is it will strip the words up to 30, wp_trim_words. And below that is the how to insert Read More.
Here are some links for you to check out:
https://codex.wordpress.org/Excerpt
https://codex.wordpress.org/Customizing_the_Read_More
Create one custom template that template you can assign your home page
/*
Template Name: Blog
*/
query_posts( array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ) ) );
<?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 the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
wp_reset_postdata();
In a wordpress website showing 3 post per row and almost unlimited rows due to ajax autoload for pagination.
What is the best way to add say for example a custom HTML code instead of post every 5th post?
You will need to add a count to the loop that is pulling the posts into the rows.
for example:
<?php $args = array(
'post_type' => 'posts',
'posts_per_page' => -1
);
$posts = get_posts( $args );
if ( $posts ) :
$temp_post = $post; ?>
<div class="row">
<?php $count = 1; foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?></h1>
<?php //This count adds the custom html mark usful if its only a sinle line of markup.
echo ( $count % 5 == 0 ) ? '<h2 class="custom-html-markup">Custom Markup</h2>' : ''; ?>
<?php // If the markup is longer than a single line.
if ( $count % 5 == 0 ) : ?>
<div class="custom-html-markup">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php // This count closes and re-opens the rows
echo ( $count % 3 == 0 ) ? '</div><div class="row">' : ''; ?>
<?php $count++; endforeach; ?>
</div>
<?php $post = $temp_post;
endif; ?>
I'm currently working on integrating wordpress with my website, but I'm hitting a wall here.
I need the initial blog page(basically just the index.php of WP) to pick up the <!--more--> quicktags, but I can't seem to get it to work.
I've followed the WP Codex, but that didn't help me at all.
So here's part of the template file: content.php
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_excerpt();
?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<div class="bthumb2">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(220, 130));
}
?>
</div>
<?php
the_content("READ MORE");
?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'tinfoilhats' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
But it just keeps displaying the <!--more--> tag in the posts once I load the page.
I've tried it with the global $more thing, but that didn't work for me.
<?php while ( have_posts() ) : the_post(); ?>
<?php
global $more;
$more = 0;
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
So what am I doing wrong?
Your global $more example looks good, and almost follows the recommendation on the documentation page applicable (the global $more line itself is supposed to be outside "the loop" - before while ( have_posts() ) - leaving the next line where it is.)
However, I couldn't get this to work on my own blog (using a a child theme of Coraline) until I moved both lines to before "the loop". Starting with your code, I'd suggest something like this:
<?php
global $more;
$more = 0;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
On the Wordpress site I'm working on, it lists posts by category, but I am also after a page that lists ALL the posts (with pagination, showing 10 per page). How would I go about achieving this?
Thanks
You could create a new page template with this loop in it:
<?php
$paged = get_query_var('paged')? get_query_var('paged') : 1;
$args = [
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
];
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>
For others who might be Googling this... If you have replaced the front page of your site with a static page, but still want your list of posts to appear under a separate link, you need to:
Create an empty page (and specify any URL/slug you like)
Under Settings > Reading, choose this new page as your "Posts page"
Now when you click the link to this page in your menu, it should list all your recent posts (no messing with code required).
A bit more fancy solution based on #Gavins answer
<?php
/*
Template Name: List-all-chronological
*/
function trimStringIfTooLong($s) {
$maxLength = 60;
if (strlen($s) > $maxLength) {
echo substr($s, 0, $maxLength - 5) . ' ...';
} else {
echo $s;
}
}
?>
<ul>
<?php
$query = array( 'posts_per_page' => -1, 'order' => 'ASC' );
$wp_query = new WP_Query($query);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php the_time( 'Y-m-d' ) ?>
<?php trimStringIfTooLong(get_the_title()); ?>
</a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>