paginating WordPress main loop - php

I am using a WP theme that uses in the archive.php main loop. The code is like that:
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'content', 'archive' );
endwhile;
else :
get_template_part( 'content', 'none' );
endif; ?>
In some categories I have lots of articles, therefore pagination is necessary. If I define in the WP settings the max. number of posts that are shown (e.g. 20) then no pagination is visible on the archive page.
Therefore my questions are:
Can I paginate the main loop anyhow and if yes: how?
Or should I use a WP query that can be paginated? ( I tried it, but it did not show the right articles anymore.) If so: How that query might look like?

Here is how to paginate the main loops (on archive.php) file:
<?php if (have_posts()):
while (have_posts()):
the_post();
get_template_part("content", "archive");
endwhile;
// Add pagination links
the_posts_pagination([
"mid_size" => 2,
"prev_text" => __("Previous", "textdomain"),
"next_text" => __("Next", "textdomain"),
]);
else:
get_template_part("content", "none");
endif; ?>

Related

Wordpress one-page template with dynamic portfolio section

I've been searching all morning for answers to no avail. I'm building a one page WordPress template. I have a home page which uses a one page template called one-page.php that is brining in all the other pages. Heres the php from that template:
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; endif; ?>
This code works great. All the sections can use the content-page.php template part, excluding the portfolio section which I would like to use another template part that brings all the portfolio custom post types.
I've tried to add conditional if statements to both the one-page.php and the content-page.php, like this:
<?php if ( is_page( 'portfolio' ) ) : ?>
//My portfolio custom post type loop is here
<? endif; ?>
But that didn't work either - I think that is because the is_page() function will be checking the current page being displayed which is the Home page. Rather than figuring out what page the query is currently dealing with - but I'm not sure.
Can anyone help me understand how I would go about conditionally loading the portfolio section into a separate template part?
You can achieve this checking page slug, which you can get in the loop. If it is "portfolio" (or whatever you saved), load content-portfolio.php, otherwise content-page.php. Something like this:
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if ("portfolio" === $post->post_name) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
Set condition in file
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if (is_page('portfolio')) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
?>
create a template file named content-portfolio.php which is copy of content-page.php and put the below code in it. if it show 'call' it means your template is working.
portfolio.php
<?php
echo "portfolio.php";
die('Call');
?>

Override default setting for category archive output (in twentytwelve)

I have this snippet in a custom category template which outputs the last X posts of that category.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); get_template_part( 'content', get_post_format() ); endwhile; twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
Problem is this outputs the posts as excerpts.. How can I change it to the_content() without affecting all the other loops? I only need to have the_content here.
Do I need to manually output the title, then the date, then author and then the_content() or can I just replace something in the snippet above ?
From what you have given, it seems that you have customized the content.php template.
To show the content just for your category page, look, you'll need to add that condition. As you did not add the code for content.php, I can't exactly say where to add it, but you have to do somthing like this
if(is_category()) {
the_content();
}else{
the_excerpt();
}

Wordpress Page of Posts

I am building a personal website to host my university work, personal projects and photos etc.
The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.
I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.
What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.
If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.
Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.
Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts
Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!
<?php
/*
Template Name: Page Of Posts
*/
/* This example is for a child theme of Twenty Thirteen:
* You'll need to adapt it the HTML structure of your own theme.
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
/* The loop: the_post retrieves the content
* of the new Page you created to list the posts,
* e.g., an intro describing the posts shown listed on this Page..
*/
global $post;
$slug = get_post( $post )->post_name;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display content of page
get_template_part( 'content', get_post_format() );
wp_reset_postdata();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// Change these category SLUGS to suit your use. category_name is comma separated.
'tag' => $slug,
'paged' => $paged
);
$list_of_posts = new WP_Query( $args );
?>
<?php if ( $list_of_posts->have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
<?php // Display content of posts ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>

How to display more listings in WordPress

I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.

Creating a separate wordpress home page and blog

Hello I want to Create a home page that has updating blog entries.
So 4 lists of headlines from different categories
And I want to have a link to the regular blog page with a different template.
Right now I just changed index.php around to have the containers for the featured posts content.
So this is a two part question how do I get these mini updates for the thing
I want to use query_posts() multiple times I assume and separate by category.
And how do I make a linkable page to a blog.php file which currently is telling me that all these functions are undefined.
<?php get_header(); ?>
<?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 endif; ?>
<?php get_footer(); ?>
If you want to create a page that includes WP data/posts outside the themes or blog folder, you need to include and make available the Wordpress functions first:
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
And then you can make the queries for each set of posts by category:
$args = array( 'numberposts' => '5, 'offset'=> 1, 'category' => 'your category ID' );
$myposts = get_posts( $args );
foreach($myposts as $post) {
...some code...
}
I hope it helps.

Categories