Wordpress Category Page Navigation - wordpress

my problem is that I can navigate between Blog pages and posts but I cant navigate between the category pages.
My code looks like that:
<div class="previous-page">
<?php
if(is_single()){
previous_post_link( '%link', __( '<div title="%title">‹</div>') );
} else {
previous_posts_link( __('‹') );
}
?>
</div>
and I have the same code for the next_post/next_posts.
I thougt that the previous code also covers the category.
So what I am missing?
Greetings and Thanks
Chris

Try using this code which I currently use for all my themes:
This goes in your functions.php file:
function show_posts_nav() {
global $wp_query;
return ($wp_query->max_num_pages > 1);
}
Then in your template files use this to show the navigation:
<?php if (show_posts_nav()) : ?>
<div class='navigation'>
<?php next_posts_link('« Older Entries'); ?>
<?php previous_posts_link('Newer Entries »'); ?>
</div>
<?php endif; ?>

Related

Remove Page Header from Blog Posts

I have a page-header that is getting copied over to all of my blog posts that I want to remove completely. I am removing it visually via CSS, but SEO crawlers are still picking it up via the tag.
I am using a standard wordpress them augmented with Elementor. Here is a screenshot of the SEO report.
And here is a screenshot of the actual HTML code
Let me know if any of you have any additional questions! Thank you for your help!
You can make a conditional statement that just outputs the title of the post if on single blog posts, so something like this
<div class="container clr page-header-inner">
<?php
// Return if page header is disabled
if ( oceanwp_has_page_header_heading() ) { ?>
<!-- check to see if single blog posts -->
<?php if (is_single()) : ?>
<h1><?php echo get_the_title(); ?></h1>
<!-- otherwise show the existing title format -->
<?php else: ?>
<<?php echo esc_attr( $heading ); ?> class="page-header-title clr"<?php oceanwp_schema_markup( 'headline' ); ?>><?php echo wp_kses_post( oceanwp_title() ); ?></<?php echo esc_attr( $heading ); ?>>
<?php endif; ?>
<?php get_template_part( 'partials/page-header-subheading' ); ?>
<?php } ?>
<?php if ( function_exists( 'oceanwp_breadcrumb_trail' ) ) {
oceanwp_breadcrumb_trail();
} ?>
</div><!-- .page-header-inner -->
you would have to replace the existing code

Wordpress latest posts featured image from category

quick Wordpress question.
I want to display the last 30 posts from my category "photos", but to only display the featured image on the relevant page as a link that will take the user to the actual post.
I have managed to do this, but it displays posts from all categories, not the "photos" category. Code I'm using is below.
I'm sure it's simple, but would love to know how to display just the recent posts from the photo category only (as the featured image).
Thanks
<!-- In functions.php -->
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=100');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<div class="photos">
<li class="recent">
<?php the_post_thumbnail('recent-thumbnails'); ?>
</li>
</div>
<?php endwhile;
wp_reset_query();
}
<!-- this is on the page template -->
<?php echo recentPosts(); ?>
Just add the category selection to your query.
Replace:
$rPosts = new WP_Query();
$rPosts->query('showposts=100');
With:
$rPosts = new WP_Query('category_name=photos&showposts=100');
Reference: http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category
You need to provide the loop argument that you want post only of certain category by providing category id cat=1. Replace 1 with the id of your photos category
<!-- In functions.php -->
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=100&cat=1');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<div class="photos">
<li class="recent">
<?php the_post_thumbnail('recent-thumbnails'); ?>
</li>
</div>
<?php endwhile;
wp_reset_query();
}
<!-- this is on the page template -->
<?php echo recentPosts(); ?>
Add the category ID to your query arguments. echo on the last line is redundant by the way. Your function outputs the HTML directly rather than returning it.
Finally your original markup was invalid. An li can't be the child of a div so I've corrected that in my example.
function recentPosts() {
$rPosts = new WP_Query( array(
'posts_per_page' => 30,
'cat' => 1
'no_found_rows' => true // more efficient way to perform query that doesn't require pagination.
) );
if ( $rPosts->have_posts() ) :
echo '<ul class="photos">';
while ( $rPosts->have_posts() ) : $rPosts->the_post(); ?>
<li class="recent">
<?php the_post_thumbnail( 'recent-thumbnails' ); ?>
</li>
<?php endwhile;
echo '</ul>';
endif;
// Restore global $post.
wp_reset_postdata();
}
<!-- this is on the page template -->
<?php recentPosts(); ?>

Wordpress previous post shortlink

Hey guys so far I have this code that gives me the full link to my previous post
<?php $prevPost = get_previous_post(FALSE);
if($prevPost) {?>
<div class="prev1" onclick="javascript:_gaq.push(['_trackSocial', 'Nav', 'Prev-Thumb']);">
<?php previous_post_link('%link',"«", FALSE); ?>
</div>
<?php } ?>
But I want the shortlink instead of the full link
How can I accomplish that ?
Thank you in advance
Swap out your existing code with the following.
<?php
// Get the previous post.
$previous_post = get_previous_post();
// Check a post was found.
if ( $previous_post ) : ?>
<div class="prev1" onclick="javascript:_gaq.push(['_trackSocial', 'Nav', 'Prev-Thumb']);">
«
</div>
<?php endif; ?>

Wordpress homepage with latest posts and Advanced Custom fields - How?

I'd like my homepage to display my latest posts which are portfolio projects of mine. Beneath these project thumbnails I've got my static content which I'm using the Repeater add-on from Advanced Custom Fields to grab. I cant get it all to work on the same page... either get the blog to work or the ACF stuff to work. Never both because one needs to be a designated static page and one needs to be a posts page. I don't understand how to tie it together and make it appear as one page.
I've experimented with the settings in the reading panel..
I've tried using front-page.php
I've read up on the WP hierarchy etc
I've tried using templates...
I've tried wp_reset_postdata(); which I read about elsewhere on Stack Overflow.
What settings must I use in the reading panel. Do I need to use a template file?
Here's the code I'm working with, I've split the code between templates and different files already, but just for ease of reading its all together here (maybe that's the right way to do it anyway, I wouldn't know..)
<!-- The posts/portfolio items -->
<?php get_header(); ?>
<div>
<?php if(have_posts()) : ?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<!-- Permalink,title and post thumbnail here (omitted) -->
</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<h2>No Posts found</h2>
<?php endif; ?>
</div>
<!-- Now for the ACF Stuff -->
<?php if(get_field('care_list')): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Please help a frustrated learner! Thanks in advance.
It looks like you're going to need to add the post id of your 'home page' (the one with the ACF repeater on it) to the get_field() function like so:
<?php $post_id = **post_id_of_your_homepage_here**; ?>
<?php if(get_field('care_list', $post_id)): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
This is because the $post_id parameter defaults to the current post being brought up by wordpress, which means ACF is looking for the repeater on the last Portfolio item/post you are displaying. If you set the $post_id parameter to the ID of your homepage, ACF will instead look for the repeater on that page.
Source: http://www.advancedcustomfields.com/resources/functions/get_field/#parameters
If I'm understanding correctly, you have a bunch of posts and you want to display a list of them with title and post thumbnail on your homepage, and then display a custom field you've assigned to the homepage underneath the list of posts?
Step 1: Create a new page template by copying page.php, changing the name to homepage.php and adding this to the top:
<?php
/*
Template Name: Homepage
*/ ?>
Step 2: Crete a Wordpress page called "Homepage" and in the attributes module in the right sidebar of the page creation tool, select "Homepage" as your page template.
Step 3: In your reading settings, change the front page from posts page to "Homepage." Now your homepage is your page called "Homepage."
Step 4: Make something like this the full code on your new page template homepage.php. It will output your posts list followed by your page custom field:
<?php get_header(); ?>
<?php $the_query = new WP_Query( $args );
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php if(get_field('repeater_field_name')): ?>
<?php while(has_sub_field('repeater_field_name')): ?>
<?php the_sub_field('sub_field_1'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>

Next / Prev post link not working wp

Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.

Resources