Wordpress Navigation - php

I am working on a Wordpress Theme, I need to work on the navigation, I am having a little trouble creating it.
The navigation I am looking for looks like this: www.neu.edu/humanities.
I have gotten this far:
if (is_front_page()) {
wp_list_pages('title_li=&exclude=12&depth=1');
}
else {
// display the subpages of the current page while
// display all of the main pages and all of the
// and display the parent pages while on the subpages
}

<?php
if (is_front_page()) {
wp_list_pages('title_li=&exclude12&depth=1');
}
else {
$output = wp_list_pages('echo=0&depth=1&title_li=&exclude=12');
if (is_page()) {
$page = $post-ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
$children = wp_list_pages('echo=0&child_of='.$page.'&title_li=&exclude=12');
if ($children) {
$output = wp_list_pages('echo=0&child_of='.$page.'&title_li=&exclude=12');
}
}
echo $output;
}
?>

Related

How to refresh with ajax in php?

I'm trying to make a likes/claps/heart system on my site and found this site (https://www.techolac.com/wordpress/how-to-add-likes-to-posts-in-wordpress-without-a-plugin/) that taught how to do it. I made some adjustments the problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Functions.php / Wordpress
// Add buttons to top of post content
function ip_post_likes($content) {
ob_start();
?>
<a href="<?php echo add_query_arg('post_action', 'like'); ?>">
<span class="icon-claps pr-2"><?php echo get_like_count('likes') ?></span>
</a>
<?php
$output = ob_get_clean();
return $output . $content;
}
add_filter('the_content', 'ip_post_likes');
//Get like
function get_like_count($type = 'likes') {
$current_count = get_post_meta(get_the_id(), $type, true);
return ($current_count ? $current_count : '');
}
//Process like
function ip_process_like() {
$processed_like = false;
$redirect = false;
// Check if like
if(is_singular('post')) {
if(isset($_GET['post_action'])) {
if($_GET['post_action'] == 'like') {
// Like
$like_count = get_post_meta(get_the_id(), 'likes', true);
if($like_count) {
$like_count = $like_count + 1;
}else {
$like_count = 0;
}
$processed_like = update_post_meta(get_the_id(), 'likes', $like_count);
}
if($processed_like) {
$redirect = get_the_permalink();
}
}
}
// Redirect
if($redirect) {
wp_redirect($redirect);
die;
}
}
add_action('template_redirect', 'ip_process_like');
The problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Image:
This is the whole code. I am using wordpres and twig / timber

Sub-sub-list disappearing in Wordpress site

I have taken over a wordpress site built by someone else (londonschoolofsamba.co.uk) and am having difficulties with sub-sub-lists disappearing in the navigation.
If I visit a page that has children, then the navigation displays the parent pages, and the children and grandchildren of that page. If I visit one of the pages that is a child, then the navigation still displays the parent and grandchildren, but if I go to a grandchild page, then the navigation returns to displaying only the parents.
Can anyone help me to get it so that when visiting a grandchild page, that the navigation still shows the children and grandchildren, and not just the parents?
<?php
$this_post = $wp_query->get_queried_object();
$temp = get_post_ancestors($this_post);
if(sizeof($temp) > 0)
{
$this_page_id = $temp[0];
}
else
{
$this_page_id = $this_post->ID;
}
$home_page = get_page_by_title('Home');
$pages = get_pages("sort_column=menu_order&parent=0&exclude=".$home_page->ID);
foreach($pages as $page)
{
$classes = "menu_item";
if($this_page_id == $page->ID)
{
$classes .= " selected_menu";
}
?>
<div class="<?php echo $classes; ?>">
<div class="menu_item_picture">
<?php echo $page->post_title;?>
</div>
<?php
$list = wp_list_pages("title_li=&child_of=".$page->ID."&depth=2&echo=0");
if($this_page_id == $page->ID && strlen($list) >0)
{
?>
<ul>
<?php echo $list;?>
</ul>
<?php
}
?>
</div>
<?php
}
?>

To define CURRENT page for current menu item

I've created a navigation menu by PHP.
1) I need help how can i change link class for current page. I mean for example when HOME PAGE is open link should be like class="bla bla CURRENT"
2) Is there any suggest to better way for the LINK to that buttons.
HERE IS THE CODES
<?php
require_once('../config.php');
$sql= "SELECT * FROM veri_kategori";
foreach ($dbh->query($sql) as $row)
{
echo "<li class=\"dropdown\">".$row["tr"]."</li>";
}
?>
Try this solution:
<?
require_once('../config.php');
// Get the current page.
$pag = $_GET['pag'];
if (isset($pag)) {
if ($page == 'about') {
// Redirect or include your page.
} else if ($page == 'contact') {
// Redirect or include your page.
}
} else {
// Redirect or include your home page.
}
$sql = "SELECT * FROM veri_kategori";
foreach ($dbh->query($sql) as $row) {
// Set default class.
$class = "dropdown-toggle";
// If home page, set another class.
if (!isset($pag)) {
$class = "bla bla CURRENT";
}
echo "<li class=\"dropdown\">{$row["tr"]}</li>";
}
?>

$post->post_parent behaves different on page then in functions.php

I created/modified a function to display breadcrumbs on pages on WordPress. The modified version makes use of #post->post_parent to get the parent of a page in order to have a full breadcrumb trail (home > page 1 > page 2 > page 3 vs. home > page 3)
The code executes perfectly on page (ie. home > page 1 > page 2 > page 3). But when I place it into a function and call it form the functions.php page it cannot detect if the page has a parent using $post->post_parent (ie. page 3 vs. home > page 3).
Could this be because the on page code is executed in the_loop but the function is somehow outside of it?
On page code:
if (!is_home()) {
echo "<ul id='breadcrumb'>";
echo '<li><a href="';
echo get_option('home');
echo '">HOME';
echo "</a></li>";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
the_title('<li>', '</li>');
echo "</ul>";
}
} elseif (is_page()) {
if(!$post->post_parent){
//echo "No Parent";
}
else{
echo '<li>'. wp_list_pages('include='.$post->post_parent.'&title_li=' ).'</li>';
}
the_title('<li>', '</li>');
echo "</ul>";
}
}
Function code:
function the_breadcrumb() {
if (!is_home()) {
echo "<ul id='breadcrumb'>";
echo '<li><a href="';
echo get_option('home');
echo '">HOME';
echo "</a></li>";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
the_title('<li>', '</li>');
echo "</ul>";
}
} elseif (is_page()) {
if(!$post->post_parent){
//echo "No Parent";
}
else{
echo '<li>'. wp_list_pages('include='.$post->post_parent.'&title_li=' ).'</li>';
}
the_title('<li>', '</li>');
echo "</ul>";
}
}
}
There is nothing inherently different about this code except that it is now wrapped in a function. The fact that it doesn't display the parent pages is frustrating. I don't want to have to include this code on every page template I create.
Help & Suggestions will be greatly appreciated!
$post ist not defined in your function. Try to give $post as parameter to the function:
function the_breadcrumb($post) {
Or, define
global $post;
at the top of the function.
function fname()
{
global $post;
code...
}

Weird problem with PHP Breadcrumb Script

I'm using Mick Sears' php breadcrumb script - found here:
http://www.roscripts.com/PHP_breadcrumbs-118.html
I've used this script several times with no problems. But with this one site I'm having the weirdest problem... Home page - fine. Level 1 page - fine. But every time I move to a level2 page, the correct level1 crumb is replaced by "Help". The link on the crumb is the correct one for the help page. This happens even if I clear all browser caches and don't go to the Help section of the site at all.
The site is http://www.fastexas.org. The script is there, but I gave the breadcrumb div display:none; until I can figure this out.
This script seems to have been around awhile and I'm wondering if anyone else has seen this problem.
The Breadcrumb Script:
<?php
class Breadcrumb{
var $output;
var $crumbs = array();
var $location;
function Breadcrumb(){
if ($_SESSION['breadcrumb'] != null){
$this->crumbs = $_SESSION['breadcrumb'];} }
function add($label, $url, $level){
$crumb = array();
$crumb['label'] = $label;
$crumb['url'] = $url;
if ($crumb['label'] != null && $crumb['url'] != null && isset($level)){
while(count($this->crumbs) > $level){
array_pop($this->crumbs); }
if (!isset($this->crumbs[0]) && $level > 0){
$this->crumbs[0]['url'] = "/index.php";
$this->crumbs[0]['label'] = "Home";}
$this->crumbs[$level] = $crumb;}
$_SESSION['breadcrumb'] = $this->crumbs;
$this->crumbs[$level]['url'] = null;}
function output(){
echo "<ul>";
foreach ($this->crumbs as $crumb){
if ($crumb['url'] != null){
echo "<li> <a href='".$crumb['url']."' title='".$crumb['label']."'>".$crumb['label']."</a></li> ";} else {
echo "<li class='last'>".$crumb['label']."</li> ";}}
echo "</ul>";}}
?>
Each page begins with something like:
<?php session_start();
$level= '1';
$label= 'Honors Circle';
$url= '/honors/'; include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level); ?>
or
<?php
session_start();
$level= '2';
$label= 'Districts';
$url= '/honors/district.php';
include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level);
?>
And to print the breadcrumb trail:
<div id="breadcrumb"><?php $trail->output(); ?></div>

Categories