Display next posts when non previous - php

I have a wordpress theme displaying the Next / Previous post but having some trouble when you're viewing the first post.
Obviously on the first post there are no other posts previous, however I would like to display the next post 2 posts
My current code looks like:
<?php next_post('%','', TRUE, '1'); ?>
<?php previous_post('%','', TRUE, '1'); ?>

I usually get the current page and then apply a class to that page, I don't know if you can do this in wordpress.
// find the current page
$current_page = basename($_SERVER['SCRIPT_FILENAME']);
$class = '';
if($current_page === 'index.php'){
$class = ' hide'; //hide the previous link if the current page is index.php or the first page.
}
<ol>
<li class="<?php echo $class; ?>">prev</li>
<li>1</li>
<li>2</li>
</ol>
also check this php pagination script on github php paginator

Let'me try. You can check the position on the loop, to check if it is on beginning or in the end.
$firstPost=( $wp_query->current_post == 0 && !is_paged() );
$lastPost=( $wp_query->current_post == 0 && $wp_query->current_pos $wp->query->post_count-1 );
You can change something to get it better.

Related

Highlight a list element when I'm browsing the very page

I saw a few tutorial about this problem but none of them satisfied me. I want to highlight the single element of my list which matches the page that I'm browsing. I created the code with php, is a wordpress based website, and the code actually works because when I echo the uri which I'm on it will display the right uri, but the if statement I created to add a class when I'm on the website won't output anything.. and I don't understand why.. anyway.. here's the code:
header.php
<ul class="nav nav-pills sliding" id="Jcollapse">
<li class="<?php if ($current == "/why-chickapea/"){ echo "current";}?>"><span>Why Chickapea?</span></li>
<li class="<?php if ($current == "/our-pasta/"){ echo "current";}?>"><span>Our story</span></li>
<li class="<?php if ($current == "/shop-chickapea/"){ echo "current";}?>"><span>Shop</span></li>
<li class="<?php if ($current == "/recipes/"){ echo "current";}?>"><span>Recipes</span></li>
<li class="<?php if ($current == "/blog/"){ echo "current";}?>"><span>Blog</span></li>
</ul>
In each page I added a php snippet:
<?php $current = $_SERVER["REQUEST_URI"]; ?>
If I echo the var $current I will obtain the right url in this format: /pagename/
At the end I style the class current with a yellow color
.current {
color:yellow;
}
.current a {
color:yellow;
}
Does anyone know where my mistake is?
this is the page website: choosechickapea.com
As you can see the class that my code will generate is empty, but if I echo each value the uri I will obtain is the right one
The simplest explanation would be, that you print the header before $current is set.
The second simplest explanation is different scopes, meaning either you set $current in a non-global scope or you read it in a non-global scope, and those two (whatever they are) are different. Since someone said wordpress, I guess there is some encapsulation into functions (thus changing the scope). Using the global keyword may be a solution, but a dirty one. But since you're already avoiding wordpress functions ...
The actual code is:
Before declaring in the header the if statement, SET the value of the variable. If you'll declare in the body, even before loading the header with a, for example require once or in wordpress:
<?php get_header(); ?>
It won't work, the variable has to be set in the header like this:
<?php $current = $_SERVER["REQUEST_URI"]; ?>
<header class="navbar-fixed-top">
<ul class="nav nav-pills sliding">
<li class="<?php if ($current == "/your-url/"){ echo "current";}?>"><span>your url</span></li>
<li class="<?php if ($current == "/other-url/"){ echo "current";}?>"><span>/other url/</span></li>
</ul>
</header>

Getting first & last post in custom post type outside of loop

I'm using WP, and have a script where when an image is clicked, the single post content loads using .load(). The arrows to navigate through each single post is located inside the .project div that is being loaded using .load().
Problem is, on the first and last posts, I only want to display certain arrows.
For example, the first item in the post gets loaded in, it shouldn't have the 'previous' arrow because there are no previous posts. Same with the last post and the 'next' arrow.
So basically to work around this, I'm trying to come up with a PHP statement (without being in the loop) to tell if the current post is the last post or first post in the custom post type.
Here's what I have so far.. just not sure how to get ID's of first post and last post outside of the loop. Everything else besides that has been tested and works. Below is just the 'logic' behind the code mostly.
<?php
// Get other posts in post type
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
// if($next_id == $previous_id) because on first/last posts, get_next_post
// and get_previous_post return the same value.
if($next_id == $previous_id) {
if() {
// if last post in custom post type
} else() {
// if first post in custom post type
}
}
?>
<?php if(isnt first post) { ?>
<li class="left" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>
<li class="grid"></li>
<?php if(isnt last post) { ?>
<li class="right" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>
I haven't worked with WP so much, but since WP templates are all PHP files and WP exposes its own API to user you can use any PHP syntax in them. If you're not concerned about running two queries each time you navigate your page then this will help you to get the idea.
<?php
global $wpdb;
$last_one = FALSE;
$first_one = FALSE;
// Get last one
$last_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` DESC LIMIT 0, 1", ARRAY_A);
if($last_result){ if($last_result['id'] == $next_post){ $last_one = TRUE; } }
// Get first one
$first_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` ASC LIMIT 0, 1", ARRAY_A);
if($first_result){ if($first_result['id'] == $previous_post){ $first_one = TRUE; } }
?>
Remember to check the names of fields and tables as I don't know the names.
Ended up using this code and it works fine...
Edit: updated code.. if for any reason somebody else ever needs it:
$args = array('post_type'=>'your_post_type', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID; // To get ID of first post in custom post type
// outside of loop
$last_id = end($posts);
echo $last_id->ID; // To get ID of last post in custom post type outside of loop
if($current_id != $first_id) { ?>
<li class="left" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>
<?php if($current_id != $last_id->ID) { ?>
<li class="right" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>

Next & previous links: Not showing a link if there isn't a next page

I've followed the documentation here (at the bottom) to create next and back buttons at the bottom of my page.
It seems to work fine until I get to the last page where the link just redirects me back to the first page. Is there a way to say if there isn't a next page to not show the link? I assumed thats what the if statement was supposed to do!!
<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($prevID)) { ?>
<a class="back" href="<?php echo get_permalink($prevID); ?>">BACK</a>
<?php } ?>
<?php if (!empty($nextID)) { ?>
<a class="next" href="<?php echo get_permalink($nextID); ?>">NEXT</a>
<?php } ?>
p.s Please don't move my question to the Wordpress Stack - that seems to be dying a bit of a death and doesn't get many responses!
My Pages are setup like this:
Parent page
Sub page 1
Sub page 2
Sub page 3
I've created a link on the parent page to goto the first subpage. Then on the subpage template I've got the code above. I just want the next link to appear on each page then when it gets to page 3 it shouldn't show the next link.
If you are saying that this is effectively looping around then $nextID must never be empty, which would be why the link was always displayed.
You could set a $firstID, ie
$firstID = pages[0];
and then check;
if ($firstID != $nextID ) {
// Display link
}

Giving links in Menu based on some condition

I have a situation here. In my project the menu section contains some anchor tags, it will work perfectly when we in index page, but moving to other pages i want to give the real links there, so my question is how to check which page is viewing or how to check the the site viewer is not in index page
<li>Home</li>
<li>About Us</li>
I want to change href conditionally, for example when I'm in index the above href attribute is OK, and when I'm in another page, for example register, then the href attribute change to index.php/site/index#home
Thanks in advance
UPDATE :
thank you uttara,I found a solution with the help of her
<?php
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$href = ($filename=='root_directory' || $filename=='index' || $filename=='site')?'':Yii::app()->request->baseUrl;
?>
Home
You can use the CMenu widget, and this takes care of the highlights, appearance, etc. Plus you can use custom CSS afterwards.
<?php
$this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=> 'YOUR_URL#home'),
array('label'=>'About Us', 'url'=>'YOUR_URL#about_us)'
)
);
?>
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$filename will give you the name of current page being viewed
and you can check for
if($filename != 'index')
{
echo '<li>Home</li>
<li>About Us</li>';
}
else
{
echo '<li>Home</li>
<li>About Us</li>';
}
remember $filename gives you just the filename without extension
first give the id to anchor tag and then put condition like below
<?php
// the php code
$flag=strpos('index.php',$_SERVER['PHP_SELF'])
{
?>
<script>
$(document).ready(function(){
$("#idofanchortg").attr('href','hrefyou want to add');
});
</script>
<?php } ?>
you can use CMenu widget provided by Yii
http://www.yiiframework.com/wiki/211/creating-a-css-driven-drop-down-menu-using-cmenu/
First of all I prefer to leave responsibility in just one place.
<li>Home</li>
<li>About Us</li>
I prefer this way 'couse code is more clean. Well. Now we know that all our controllers extends Controller class (/protected/components/Controller.php). In this place we can add
public function getHomeHrefLink() {
// when I'm in index page the href attribute is #home
// when I'm in register page the href attribute is index.php/site/index#home
}
So come on: We can you $this->action (for controller name) and $this->action->id (for action name) to understand where we are:
public function getHomeHrefLink() {
return $this->createUrl('index/site', array());
// when I'm in register page the href attribute is index.php/site/index#home
return '#home;
}

How to create permalinks without single pages in wordpress?

I'm creating my own wordpress theme which is a bit different because it will not have single pages (or atleast, no single page will be reachable). The whole website contains just the homepage (with the loop) and previous posts pages.
I want to link to individual posts inside the loop like site.com#post-124 or site.com/paged=5#post-214.
I already created a function that does this:
function getPermalink($id,$postsPerPage) {
$postNumber = Get_Post_Number($id);
//a function that get's the post number based on
//the chronical order of published posts.
$page = floor(($postNumber - 1) / $postsPerPage);
$url = get_option('home');
if($page > 0) {
$url .= '/?paged=' . ($page + (1 - floor($page / 5)));
}
$url .= '#post-' . $id;
return $url;
}
You can see it live here: http://mijnrealiteit.nl (the previous posts pages are replaced by an infite scroll plugin).
This works, however it breaks when I start adding posts because all the posts before will get shifted back to pages further away (this makes the link invalid).
The way I see it there are two possible solutions:
Change the permalinkstructure to display paging backwards (so x.com/paged=231 becomes the first 'previous' page. However this is not userfriendly.
Make links with just the ID and let wordpress handle custom redirection to the page at that current point in time.
Are there better alternatives? I'm sure this is already solved somewhere, I just couldn't find it.
I got pushed in the right direction by a friend, I build it quite easily using option 2:
The getPermalink function is now much simpler:
function getPermalink($id) {
return get_option('home') . '/?f=' . $id;
}
I didn't make any custom redirection, I just checked at the homepage for a an 'f' being passed in the GET request:
$perma = $_GET['f'];
if(isset($perma) && !is_paged()) {
$customposts = get_posts('p=' . $perma );
foreach( $customposts as $post ) :
setup_postdata($post); ?>
//load the post
<?php endforeach;
}?>
If that is true the post will be fetched using the get_posts function by Wordpress. I also check the (normal) loop for the post that already has been served:
<?php while (have_posts()) : the_post();
if(get_the_ID() != $perma) { ?>
//load the post
<?php } endwhile; ?>

Categories