I'm using following code to display my 3 level menu:
if(!$post->post_parent){
// will display the subpages of this top level page
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
// diplays only the subpages of parent level
//$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
if($post->ancestors) {
// now you can get the the top ID of this page
// wp is putting the ids DESC, thats why the top level ID is the last one
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
// you will always get the whole subpages list
}
}
if ($children) { ?>
<ul id="submenu">
<?php echo $children; ?>
</ul>
<?php } ?>
It lists pages in side bar, second level then 3rd level too. I would like to include very top level too so i would like to my structure to look as follow:
*A
-a
--a
-b
--b
-c
--c
Where as above code is not listing main page i.e. *A, i hope that make sense and someone will be able to help
Thanks,
I found this code snippet at the wordpress codex site, and I think it's exactly what you're looking for, I've pasted it in for convenience:
<?php
//if the post has a parent
if($post->post_parent){
//collect ancestor pages
$relations = get_post_ancestors($post->ID);
//get child pages
$result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
if ($result){
foreach($result as $pageID){
array_push($relations, $pageID->ID);
}
}
//add current post to pages
array_push($relations, $post->ID); // <---- THIS IS INCLUDING THE PARENT
//get comma delimited list of children and parents and self
$relations_string = implode(",",$relations);
//use include to list only the collected pages.
$sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
}else{
// display only main level and children
$sidelinks = wp_list_pages("title_li=&echo=0&depth=2&child_of=".$post->ID);
}
if ($sidelinks) { ?>
<h2><?php the_title() ;?></h2>
<ul>
//links in <li> tags
<?php echo $sidelinks; ?>
</ul>
<?php } ?>
It also has some built-in logic to not display everything if this is a highest-level page. Hope this helps!
<div id="breadcrumbs">
Home
<?php
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ''.get_the_title($page->ID).'';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo ' / '.$crumb;
?>
</div>
<h1><?php the_title(); ?></h1>
credit : Ivovic
from : http://wordpress.org/support/topic/display-page-parent-on-page-with-title?replies=13
Related
Inside a template part file there's a variable I'd like to be able to use in my page templates. Page templates are in the theme directory, and the template part file are in a sub-directory called template parts.
My template part file is called 'roster-get-annual-data.php', and the variable is the title of a custom post type. How can I make this title variable available in my page template?
Inside my template file (roster-get-annual-data.php) - within a wp_query:
if ($rosterQuery->have_posts()) {
while ($rosterQuery->have_posts()) {
$rosterQuery->the_post();
$post_title = $post->post_title;
set_query_var('post_title', $post_title);
?>
<h1><?php echo $post_title; ?></h1>
<div class="teams-wrapper">
<?php
$teams = get_field('single_rosters');
foreach ($teams as $post) {
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post);
// set the individual roster's ID
$rosterId = $post->ID;
// get the Class Title
if (!empty(get_field('class', $rosterId)) && !empty(get_field('grad_year', $rosterId))) {
$classTitle = ucwords(get_field('class', $rosterId)) . ' - Class of ' . get_field('grad_year', $rosterId);
} else {
$classTitle = get_the_title();
}
?>
<h2><?php echo $classTitle; ?></h2>
<?php
// create empty $players array
$players = array();
// loop through the players acf repeater
if (have_rows('players', $rosterId)) {
?>
<?php
while (have_rows('players', $rosterId)) {
the_row();
// place players into an array
$players[] = get_sub_field('name');
}
?>
<?php
}
// display the PLAYERS NAMES
?>
<p class="players"><?php echo implode(', ', $players); ?></p>
<?php
}
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
?>
</div>;
?>
And then in my page template:
$post_title = get_query_var('post_title');
get_template_part('template-parts/roster-get-annual', 'data');
I thought this would give me just the $post_title variable, but instead it outputs the entire template without the $post_title variable.
It´s not 100% clear to me what you want to achieve. But if you want pass args to a template you can do it with the 3rd parameter:
get_template_part('template-part', 'name', array('customkey' => 'value') );
see doc:
https://developer.wordpress.org/reference/functions/get_template_part/
I have a sidebar menu on a WordPress site that simply outputs all the child pages under that parent. I'm trying to highlight (or hopefully add an arrow) the current child page that is selected. I've run into a wall with my limited PHP experience to figure out how to do that.
Any help would be greatly appreciated. The relevant code is below:
<?php
/* if the current pages has a parent, i.e. we are on a subpage */
if($post->post_parent){
/* $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page */
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
}
/* else if the current page does not have a parent, i.e. this is a top level page */
else {
//$children = wp_list_pages("title_li=&include=".$post->ID."&echo=0"); // include the parent page as well
$children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&"); // form a list of the children of the current page
}
/* if we ended up with any pages from the queries above */
if ($children) { ?>
<ul class="submenu">
<?php echo $children; /*print list of pages*/ ?>
</ul>
<?php } ?>
I'm assuming it would be in the output section, but I simply don't know how to target the current child page that's being browsed and highlight that accordingly.
I had a little fun with this one. And my result might not be exactly what you were looking for, but I think it might be better. In short, the below snippet allows you to easily identify the page hierarchy (parent/children) for the current page. As such, you can then do all sorts of wonderful things to your heart's desire. For the purposes of your specific request, I made the custom function "custom_list_child_pages(...)" return a full list of links from the page hierarchy related to the "current page".
There is tons of logic, all related to the key situations you were trying to tackle. You should be able to identify when you are in a sub-page or parent page, you should be able to call out that little function and modify it to make it do whatever you need it to do with all the relevant players (current, parent [or self if no other], child pages, etc).
Let me know if this helps you and if you get stuck with the code or any other aspects regarding what you are trying to achieve let me know as well and I will see how I can assist you further.
Code:
<?php
//die(var_dump($foo)); // Quickly check a variable value.
function custom_list_child_pages($the_current_page_id, $the_parent_page_id, $child_pages){
$html_page_listing = '';
// Get creative with the parent page.
$the_parent_page = get_page( $the_parent_page_id );
$html_page_listing .= "<h3>" . "<a href='$the_parent_page->guid'>$the_parent_page->post_title</a>" . "</h3>";
// Get creative with the child pages.
$html_page_listing .= "<h3>" . "Child pages:" . "</h3>";
$html_page_listing .= "<ul>";
// Iterate through child pages as desired.
foreach ($child_pages as $key => $page) {
$current_page = get_page_by_title( $page->post_title );
$current_page_id = $page->ID;
if ($current_page_id == $the_current_page_id) {
// Do something great.
$html_page_listing .= "<li> ->" . "<a href='$page->guid'>$page->post_title</a>" . "</li>";
} else {
$html_page_listing .= "<li>" . "<a href='$page->guid'>$page->post_title</a>" . "</li>";
}
}
$html_page_listing .= "</ul>";
return $html_page_listing;
}
global $post; // If outside the loop.
// Set up the objects needed.
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
if ( is_page() && $post->post_parent ) {
// This is a subpage.
$the_current_page_id = $page_id;
$the_parent_page_id = wp_get_post_parent_id( $post_ID );
$the_child_pages = get_page_children( $the_parent_page_id, $all_wp_pages ); // Form a list of the children of the current page.
$the_parent_page_id = wp_get_post_parent_id( $post_ID ); // Include the parent page as well.
$menu = custom_list_child_pages($the_current_page_id, $the_parent_page_id, $the_child_pages); // ~append the list of children pages to the same $children variable
echo $menu; // Print list of pages.
} else {
// This is not a subpage.
$the_current_page_id = $page_id;
$the_parent_page_id = $page_id;
$the_child_pages = get_page_children( $page_id, $all_wp_pages ); // Form a list of the children of the current page.
$the_parent_page_id = $page_id; // Include the parent page as well.
$menu = custom_list_child_pages($the_current_page_id, $the_parent_page_id, $the_child_pages);
echo $menu; // Print list of pages.
}
?>
Regards,
Arty
I'm trying to create php code that will get the list of categories that are assigned to a wordpress post (of the current page) and then loop through the array list and each category that IS has been assigned to the post and add it to the script.
this is the script i need to inject the category name in:
<script> window.abkw = '<category name>';</script>
so i wrote this but it does not seem to work...
<?php
$ads = the_category();
for ($i = 0; $i < count($ads); ++$i) {
$d = $ads[$i];
<script> window.abkw = $d[$i];</script>
}
?>here
I'm new to php so please excuse my ignorance... and thank you so much.!
use get_categories() (https://developer.wordpress.org/reference/functions/get_categories/)
$categories = get_categories(array('hide_empty' => 0'));
foreach($categories as $category) {
echo '<script> window.abkw = '.$category->cat_name.';</script> ';
}
What if you use jquery data()...
https://api.jquery.com/data/#data-html5
--edited--
I expand the way
template-example.php
...
<?php
$categories = get_categories(array('hide_empty' => 0'));
foreach($categories as $category): ?>
<div class="js-category" style="visibility:hidden;" data-category = "<?php echo $category->cat_name ?>"></div> //invisible div
<?php endforeach; ?>
...
file.js
var categories = $(".js-category").data("category");
...
Here Is my question: What I am wanting To do is Take Results from a mysql table and turn them into a menu and a drop down menu
HERE IS A QUICK EXAMPLE:
if you see in my mysql table i have page_name and parent, So the example is:
page_name and if i have row 1 the page_name is 'Home' now it's parent is 'none' right but on id number 39 the page_name is 'Contact Us' and the Parent Is 'Far Far Away 123' so if the parent is equal to 'none' then it will show at the top of the menu not the drop down if it has a parent it will show under that parent like this:
Home | the ben page | The Brock Page | Far Far Away 123 | dsfk
Contact Us
You see Contact Us is under Far Far Away Because the parent Is Far Far Away 123
here is my table:
Here is my code That I am trying but it is not working for some reason:
<ul>
<?php
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($links = mysqli_fetch_assoc($result)) {
if($links['parent'] !== "none") {
?>
<li id = "<?php echo $links['id']; ?>"><a href="
<?php
echo "page.php?id=" . $links['id'] . "\" title=\"" . $links['page_title'] . "\"";
?>>
<?php
echo $links['page_name'];
?>
</a>
<?php
if($links['parent'] !== "none") {
$child = "";
$sql = "SELECT * FROM pages";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
if($row['parent'] !== "none") {
$child = $row['page_name'];
}
}
echo "<ul id=\"sub_menu\" class=\"sub_navagation" . $links['id'] . "\">";
echo "<li>";
echo $child;
echo "<li>";
echo "</ul>";
}
?>
</li>
<?php
}
}
?>
</ul>
CSS:
#sub_menu {
display: none;
}
#sub_menu:hover {
display: block;
}
Ok if as you can see i have the parent row in the MYSQL table and on id number 39 i want the 'Far Far Away123' to be the parent of Contact Us and i want to show it when i hover over 'Far Far Away123'
My suggestion is to build out an array of all the results. Then run through that array (instead of multiple database queries).
I added a function build_dropdown() that will take the page name and run through the array of pages to see if there are any items with a parent matching. If so, we make an array of those items and run through them to build the dropdown menu. If not, it does nothing and moves on to the next menu item.
<?php
function build_dropdown ($parent, $pages){
foreach($pages as $page){
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $item['id'] .'">';
foreach($items as $item){
echo '<li>'.$item['name'].'<li>';
} // END foreach
echo '</ul>';
} // END if
}
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
?>
</li>
<?php
} // END if
} // END foreach
?>
I suggest you will need to JOIN your table to basically query it again to get the parent value, and add that to your markup.
SELECT *
FROM Pages
LEFT JOIN Pages p2 on page_name = p2.parent
(note: the syntax above may not be right, but I wanted to give you an idea of where I would start).
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
}
?>