Creating PHP previous page button on first page in WordPress - php

I have this PHP code that works excellent in displaying arrows to previous/next pages of WordPress child pages:
<?php // Button Code for Prev and Next Page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(
array('post_type' => 'page'));
$portfolio = get_page_by_title('Services');
$args = array (
'child_of' => $portfolio->ID,
'sort_order' => 'asc',
'post_type' => 'page'
);
$portfolio_children = get_pages( $args );
$pages = array();
foreach ($portfolio_children as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="previousarrow">
<a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>">
<img src="<?php bloginfo('url');?>/wp-content/uploads/2016/06/previousbutton.png">
</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="nextarrow">
<a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>">
<img src="<?php bloginfo('url');?>/wp-content/uploads/2016/06/nextbutton.png">
</a>
</div>
<?php } ?>
</div>
Problem is that on the first and last pages, I can't get it to show the previous/next button to go to the 'last or first' page of the child pages.
Example
A (parent)
1 (child page 1)
2 (child page 2)
3 (child page 3)
I want the previous option on page 1 to go to page 3 and vice versa but I can't figure that out. Any help is appreciated.

The buttons are not appearing because of the php if parameters, which eliminates the previous arrow on page 1 and next arrow on page 3 because it recognizes them as empty. One way around this would be to remove the if clauses and use if statements to define your prevID and nextID variables, or you can simply add else statements after each if statement defines the previous/next buttons to be shown. However, both of these solutions are temporary, as you would require the number of pages in order to implement them, which is subject to change. Instead, use this code to ensure this feature will stay intact even if you add additional pages:
<?php if (empty($prevID)) { $firstpage = $pages[$current]; }
if (empty($nextID)) {$lastpage = $pages[$current]; }
This will define the value of the first and last pages, and can simply be inserted after you define prevID and nextID. Now simply add php else statements like so:
</div> <?php }
else { ?>
<div class="previousarrow>
<a href="<?php echo get_permalink($lastpage); ?>"
title="<?php echo get_the_title($lastpage); ?>">
<img src="<?php bloginfo('url');?>
/wp-content/uploads/2016/06/previousbutton.png"></a></div>
<?php }
This block of code would be inserted after your first div class, as the previous arrow should navigate to the last page when no previous page exists. Do the exact same after the second div class, replacing the lastpage variable with the firstpage one we created. Hope this helps!

Related

How can I show only elements with same page_id in my dropdown menu with PHP? How do I write the 'if' statement in foreach loop?

I have multiple dropdown menus and I only want to show subpages that have same page_id as the main pages in the dropdown.
I have two SQL tables:
'pages' => page_id, page_name;
'subpages' => subpage_id, page_id, page_name;
Subpages get same page_id as the chosen parent page has, when I insert them via my form.
The problem is, all sub-pages are shown under every drop-down menu. I only want to show sub-pages under pages that share the same page_id.
I suppose I need to write an IF statement in the foreach loop, but not sure how to do that..
index.php:
include_once('classes.php');
$page = new Page;
$subpage = new Subpage;
$pages = $page->fetch_all();
$subpages = $subpage->fetch_all();
<?php foreach ($pages as $page) { ?>
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
</button>
<div class="dropdown-container">
<?php foreach ($subpages as $subpage) { ?>
<a href="../subpage.php?id=<?php echo $subpage['subpage_id']; ?>">
<?php echo $subpage['subpage_name']; ?>
</a>
<?php } ?>
</div>
<?php } ?>
Summary: All subpages show under every pages dropdown - I would like to show subpages that share page_id with main pages. I suppose I need to write an IF statement in the foreach loop, but not sure how to do that..
Any help is appreciated!
You may match respective page_id show only if its related to main page
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
</button>
<div class="dropdown-container">
<?php foreach ($subpages as $subpage) {
// match respective page_id show only if its related to main page
if($subpage['page_id'] == $page['page_id'])
{
?>
<a href="../subpage.php?id=<?php echo $subpage['subpage_id']; ?>">
<?php echo $subpage['subpage_name']; ?>
</a>
<?php } } ?>
</div>
<?php } ?>

Editing a wordpress loop to bring in one page via its ID number

This loop brings in all the pages and shows it on a single page. How can I edit it so that I only bring in one page identified by its ID? (the page wont change)
Thanks
<section id="<?php echo $post->post_name;?>" class="page-area<?php echo $bgClass;?>"<?php echo $style;?>>
<div class="wrapper"<?php if($fullEmbed<>''):?> style="width:100%"<?php endif;?>>
<?php if($hideTitle!='Yes'):?>
<hgroup class="title">
<h1<?php echo $font;?>><strong><?php echo $mainHeading;?></strong></h1>
<?php if($subHeading<>''):?><p<?php echo $font;?>><?php echo $subHeading;?></p><?php endif;?>
</hgroup>
<?php endif;?>
<?php if($fullEmbed<>''):?>
<div class="full-embed"><?php echo van_shortcode($fullEmbed);?></div>
<?php else:?>
<div class="entry"<?php echo $font;?>>
<?php van_content(true,true);?>
</div>
<?php endif;?>
</div>
</section>
<?php endwhile;?>
You mean like:
<?php
$page_id = 123; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), WordPress will generate an error. By default, this will return an object.
echo '<h3>'. $page_data->post_title .'</h3>';// echo the title
echo apply_filters('the_content', $page_data->post_content); // echo the content and retain WordPress filters such as paragraph tags. Origin: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
?>
Straight from the WP Codex http://codex.wordpress.org/Function_Reference/get_page

Href working on one page but not on another?

This href I have on a page is included in a cycle 2 slider so a caption under an image. BUT it doesn't work when clicking and it opening a new tab, it only works when you right click and then click "go to ....". Does anyone know why?
The plugin on wordpress: http://www.advancedcustomfields.com/
Code:
<a class="alt-caption" href="http://<?php the_field('url_site'); ?>"><?php the_field('url_site'); ?></a>
<div class="paginawrap no_overflow">
<div class="wraptest">
<div class="cycle-slideshow"
data-cycle-fx="carousel"
data-cycle-speed="500"
data-cycle-delay=5000
data-cycle-next=" > img"
data-cycle-caption=".alt-caption"
data-cycle-caption-template="{{alt}}"
data-cycle-carousel-fluid=true
data-allow-wrap=false
>
<?php $args = array(
'post_type' => 'project',
'posts_per_page' => 10
);
$query = new WP_Query( $args );
if ( $query->have_posts()) :
while ( $query->have_posts()) : $query->the_post();
the_post_thumbnail('home');
endwhile; wp_reset_postdata();
endif; ?>
<div class="alt-caption"></div>
</div>
<div class="archief1">
</div>
</div>
</div>
Your href is empty. Let's have a look at your source code:
<a class="alt-caption external" href="http://">www.aimassociates.nl</a>
The href attribute is looking at "http://".
Maybe you must add an echo?
<?php echo the_field('url_site'); ?>
It's weird that the same PHP code returns different result, have you copy/paste the code just as is in your files?
Why don't you try this?
<?php $site_url = the_field('url_site'); ?>
<a class="alt-caption" href="http://<?php echo $site_url; ?>"><?php echo $site_url; ?></a>
You use php to load the link. Why not use php the entire way for that line of code? Something like this:
echo('<a class="alt-caption" href="' . $the_field . '" target="_blank"><' . $the_field . '></a>');
Also make sure that your homepage correctly reads the php data.

Highlight menu item depending on anchor selected?

I'm using the following tutorial to create a one page website:
http://www.insitedesignlab.com/how-to-make-a-single-page-website/
Is it possible to change the color of the menu item depending on what anchor is selected?
This is how I've setup the menu
<?php $current = $post->ID; $counter = 1; $mypages = get_pages(array('sort_column' => 'menu_order'));
foreach( $mypages as $page ) { ?>
<?php if ($current == $page->ID) { ?>
<li><a style="color:white!IMPORTANT;" id="menu<?php echo $counter; ?>" href="#section<?php echo $counter; ?>"><?php echo $page->post_title; ?></a></li>
<?php } else { ?>
<li><a id="menu<?php echo $counter; ?>" href="#section<?php echo $counter; ?>"><?php echo $page->post_title; ?></a></li>
<?php } ?>
<?php $counter++; } ?>
So each menu iterates by 1 each time it loops.
You can try Jquery Tabs... Maybe its easier
Sure, I think the right approach here is to bind the tabs, I mean not depending in the anchor, so everytime a tab is clicked it will change,so first you have to add a class to all tabs like so:
<li><a style="color:white!IMPORTANT;" id="menu<?php echo $counter; ?>" class="tab" href="#section<?php echo $counter; ?>"><?php echo $page->post_title; ?></a></li>
after that you just need to bind the tab elements to the click function to fire the event every time a tab is selected:
$(".tab").click(function(e){
$(".tab").css("color", ""); //Remove the color from all tabs
$(this).css("color", "white!IMPORTANT"); //Apply the "highlight" color to the selected
});
this way we remove the color from all "tab" elements, and then we apply the color to the selected tab.
It would be better if you use a css class for the link that you want to be highlighted.
CSS:
.linkactive{
color:pink;
font-weight:bold;
}
jQuery:
$("#nav a").click(){
$(".linkactive").removeClass("linkactive");
$(this).addClass("linkactive");
});
Please remember to add the class "linkactive" to your home or starting tab.

Custom header image for category in opencart

I have 4 main categories in an opencart site.
I want to have a different header image for the 4 separate categories.
How can I get the current category name and put an if statement to select the image for the header?
Current header image code:
<div id="headerWrapper">
<div id="header">
<div class="div1">
<div class="div2">
<?php if ($logo) { ?>
<img src="<?php echo $logo; ?>" title="<?php echo $store; ?>" alt="<?php echo $store; ?>" />
<?php } ?>
</div>
</div>
</div>
You will need to work out if the path is set and the route is product/category to begin with, to check if you are even on a category page. Then you will need to use that information in a switch really (rather than a large if else listing). Can you give more detail on what it is you are wanting to change in the code above? Is it the logo or are you wanting to add a class to the header that you can then use to style via your stylesheet
To find out if you are in the category, use this in the index() method in catalog/controller/common/header.php
$get = $this->request->get;
$this->data['cat_id'] = false;
$this->data['cat_name'] = '';
if(!empty($get['route']) && $get['route'] == 'product/category' && !empty($get['path'])) {
$cats = explode('_', $get['path']);
$cat_id = array_pop($cats);
$cat_id = (int) $cat_id;
if($cat_id) {
$this->load->model('catalog/category');
$result = $this->model_catalog_category->getCategory($cat_id);
$this->data['cat_id'] = $cat_id;
$this->data['cat_name'] = $result['name'];
}
}
This will give you two variables to use in your template's common/header.tpl file
$cat_id and $cat_name
$cat_id will be the current category id, or false if it's not a category page
$cat_name will have the current category name if one exists, or an empty string if it doesn't

Categories