Wordpress, custom page theme next/previos posts - wordpress

I have a custom page template with code:
<?php
/*
Template Name: Featured
*/
get_header(); ?>
<div id="content_box">
<div id="content" class="posts">
<img src="http://www.dinneralovestory.com/wp-content/uploads/2010/04/favorites.png" alt="Favourites"/><br clear="all" /><br />
<?php
//The Query
$my_query = new WP_Query('category_name=favourites');
if ($my_query -> have_posts()) :
?>
<?php while ($my_query -> have_posts()) : $my_query -> the_post(); ?>
<div class="featured_box">
<div class="featured_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="featured_content">
<span class="post_title"><?php the_title(); ?></span>
<?php the_excerpt(); ?>
</div>
</div>
<br clear="all" /><br />
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/navigation.php'); ?>
<?php else : ?>
<h2 class="page_header center">Not Found</h2>
<div class="entry">
<p class="center">Sorry, but you are looking for something that isn't here.</p>
</div>
<?php
endif;
wp_reset_query();
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
The navigation.php file has the previous / next code (it works fine for the standard post pages and archive pages)
navigation.php:
<?php if (is_single()) : ?>
<div class="navigation">
<span class="previous"><?php previous_post_link('← %link') ?></span>
<span class="next"><?php next_post_link('%link →') ?></span>
</div>
<div class="clear whitespace"></div>
<?php else : ?>
<div class="navigation">
<div class="previous"><?php next_posts_link('← Previous Entries') ?></div>
<div class="next"><?php previous_posts_link('Next Entries →') ?></div>
</div>
<div class="clear flat"></div>
<?php endif; ?>
I have set the maximum posts per page to 5, but the page using this theme template wont show the links. Any ideas? What code can i use to get them.
Thankyou

previous_post_link and next_post_link and the like don't make any sense for Pages. Pages are not ordered by date and time, they are hierarchical. In other words, there is no "next" Page. That doesn't make any sense in that respect.
Your base problem is that you're using a custom Page template to display Posts from a specific Category. This is the wrong way to do it, WordPress has perfectly valid category archives already, which work fine and which expect to display Posts properly.
Long story short: You will never get your Page Template approach to work 100% correctly. It just doesn't work that way. Posts were never meant to be displayed on Pages, and trying to do so only leads to things not working.

Related

How can I retrieve an ACF field from a custom post type to the archive page of this CPT?

I create a Custom Post Type with ACF field inside. Everything works great, but when I want to create my archive-cpt.php page, I can't get my ACF field to be seen on this page.
Here is my archive page code :
<?php get_header(); ?>
<main role="main">
<!-- section -->
<section>
<h1><?php _e( 'Archives', 'trad' ); ?></h1>
<div class="container">
<div class="row">
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="col-lg-4 mx-auto">
<!-- article -->
<h2 class="titre-extranet-article">
<?php the_title(); ?>
</h2>
<li>
<?php // display a sub field value
get_sub_field('titre'); ?></li>
</div>
<!-- /post thumbnail -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'trad' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
</div>
</div>
<?php get_template_part('pagination'); ?>
</section>
<!-- /section -->
</main>
<?php get_footer(); ?>
The sub field 'titre' doesn't appear on my archive page.
Where I am wrong ?
Thank you.
get_sub_field() is for a repeater field. So you need to get the parent first e.g.
if( have_rows('parent_field') ):
while ( have_rows('parent_field') ) : the_row();
$titre = get_sub_field('titre');
// Do something...
endwhile;
endif;
You can get that field just with get_field() if it is a single text field.
Try with get_field()
<?php echo get_field('titre'); ?>
And one more thing you forgot to add echo before the get_sub_field(). So try with the echo.
<?php echo get_sub_field( 'titre' ); ?>

Wordpress - How do I show only the 4 newest posts on my page?

I am trying to only load the 4 latest posts on my page and I have found a line of code that should work. The problem is that I don't know where to put it.
<?php query_posts('posts_per_page=20'); ?>
This is what I found when I was searching for a solution (although I don't know if this will also work for showing only the 4 newest posts. As you can see I already have 'catname=projecten' on that line and I have no idea how to combine the two.
<?php
query_posts('catname=projecten');
while (have_posts()) : the_post();?>
<div class="col-md-3">
<div class="newsfeed center">
<h1><?php echo get_the_title( $ID ); ?> </h1>
<p>
<?php the_content(); ?>
</p>
</div>
</div>
<?php endwhile;
?>
I hope someone can help me out!
I'd like to answer it, because there are few things need to be corrected. This should do it, and notes below.
<?php
query_posts('category_name=projecten&posts_per_page=4');
while (have_posts()) : the_post(); ?>
<div class="col-md-3">
<div class="newsfeed center">
<h2><?php echo get_the_title(); ?> </h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
A few notes:
It's category_name, not catname.
You should not use <h1> for each title, for a better accessibility use <h2> or <h3> etc.
get_the_title( $ID ); the $ID seems not necessary to be there.
Don't wrap the_content(); with <p> it's rich content already, use <div> if you need.
Don't forget to reset the query afterwards.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query

Wordpress built-in pagination not working

In the Wordpress reading options, I set it so that the index only displays 2 posts at most. However, the next and previous page buttons are missing. I went into my template, tried the default methods, and none of them worked.
I tried adding
<?php posts_nav_link(); ?>
Didn't work. I then tried
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>
Which also didn't work. Nothing showed up in the source code of the final page, at all. These are default Wordpress functions, right? What is going wrong here?
If it's of any use, here's the index.php
<?php get_header(); ?>
<section class="content">
<ul class="items wrap">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<h4><?php the_time('F jS, Y') ?></h4>
<p><?php the_content(__('(more...)')); ?></p>
<hr>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we couldn’t find the post you are looking for.'); ?></p>
<?php endif; ?>
<!-- I added the pagination code here. -->
</ul>
</section>
<div id="delimiter"></div>
<?php get_footer(); ?>
Thank you!
Turns out there were only two posts to display, so of course it didn't show the next/previous links. So there it is, problem solved.

Modify global wp_query to return latest of custom post type

I have developed a theme with custom post types. One of those post_types is called events.
I wan't to display the latest event in a "page" called "upcoming event". So i created this template file called "Events". Now i need to modify the global wp_query so that i return this last event.
This is how my single.php looks like:
<?php
get_header();
setup_postdata($post);
get_page_post_content();
get_footer();
?>
Works fine when viewing event as custom posts types "normal". And this is my template file which should contain the latest event:
<?php
/*
* Template Name: Event
*/
?>
<?php
global $wp_query;
$wp_query = new WP_Query('post_type=event&posts_per_page=1');
include 'single.php';
?>
However this does not work as expected since i have other parts of my template depending on properies like "is_single()" which is this case returns false, since the query is called from a page. I somehow need to set the query so those properies are changed. Anyone knows how or how i should solve this?
Thanks!! :D
Put below code in your template file.
<?php
/**
Template Name: Event
*/
get_header();
?>
<div id="content" >
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts('post_type=event&paged='.$paged.'&posts_per_page=10');
if (have_posts ()) :
while (have_posts ()) : the_post(); ?>
<div <?php post_class() ?>>
<div class="post-title">
<h2 class="alignleft"><?php the_title(); ?></h2>
</div><!-- .post-title -->
<div class="post-content">
<?php the_excerpt('read more');?>
</div>
</div><!--end of .post_class -->
<div class="post-meta">
<p class="alignright"><a class="readmore" title="<?php get_the_title();?>" href="<?php echo get_permalink( $post->ID );?>" rel="nofollow">Read</a></p>
<span class="alignright comment-cnt"><?php comments_popup_link( 'No Comments', '1 Comment', '% Comments' ); ?></span>
<div class="clear"></div>
</div><!-- end of .post-meta -->
<?php
endwhile;
endif;
?>
</div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>

WordPress previous_posts_link() leads to a 404 error not found

Below is the code I am using. I have tried everything I have been able to find and it still doesn't work. My permalink structure is /%category%/%postname%/. I believe that the url is correct that it is trying to go to i.e. http://localhost:8888/wordpress/blog/page/2. Annoyingly, the exact same code works on another site I have designed previously.
Could someone point me in the right direction please? Thanks
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=3&showposts=2&paged=" . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php if (have_posts()) : ?>
<div id="lefttop"></div>
<div id="blogpoint">
<div id="leftcol">
<?php while (have_posts()) : the_post(); ?>
<div id="leftsquidge">
<h2><?php the_title(); ?></h2><br /><br />
<?php the_excerpt(); ?>
</div>
<div id="rightsquidge">
<?php the_tags( '<p><strong>File under:</strong> ', ', ', '</p>'); ?>
<?php the_time('F jS, Y') ?> by <strong><?php the_author() ?></strong>
</div>
<div style="clear:both;"></div>
<br /><br />
<?php endwhile; ?>
<div class="navigation" style="padding:0px;margin:0px;">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php endif; ?>
<div style="clear:both;"></div>
</div>
</div>
<div id="leftbot"></div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
EDIT
I have answered my own question. It was something I had tried before and wasn't working. You have to create a page, on the dashboard, that uses your category as the template.
Try going to "Reading Settings" in the Wordpress admin, and makes sure the "Blog pages show at most _ posts" is set to 2, or whatever your 'showposts' limit is in your query. Your query_posts is giving you accurate results but Wordpress is paging it based on your reading settings.
If the same code works fine in another site then check your settings for this site. Compare your permalinks settings on both the sites.
Does both the site work on the same environment (Apache or iis)?

Resources