I need some help doing a navigation for my site but I couldn't figure out how to do it using Wordpress' wp_list_pages() function.
For reference, the navigation system on http://www.rwgroup.com/ should give the exact idea of what I am aiming for.
Before I tried using wp_list_pages('child_of='.$post->parent_post) but it seems that the pages only display wrt it's immediate parent and forgotten it's ancestors. I've been trying this out for a while now but I just couldn't get my head around it.
Any ideas would be great
Try this code to list out current page with its ancestors and children-
<?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);
//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=1&child_of=".$post->ID);
}
if ($sidelinks) { ?>
<h2><?php the_title(); ?></h2>
<ul>
<?php //links in <li> tags
echo $sidelinks; ?>
</ul>
<?php } ?>
Related
Having a few issues in customizing a WordPress categories list and would appreciate any examples of how I can achieve the following:
I have created a WordPress categories list that shows the count for all categories. However, I am unable to show the count for show_option_all ie show the total number of posts for all categories combined in the same way that all the other categories display the post count?
How can I add a separator between each category list item ie between each li?
If category list appears on a particular page, is their a way of giving a specific category within the list, an active state?
Please see existing code below:
<ul>
<?php
$categories = wp_list_categories('title_li=&show_count=1&echo=0&child_of=18&show_option_all=ALL');
$categories = preg_replace('/<\/a> \(([0-9]+)\)/', ' <span class="count"><sup>\\1</sup></span></a>', $categories);
echo $categories;
?>
</ul>
Any help would be appreciated.
EDIT: Ended up using the code below which achieved the desired results:
<ul>
<?php
$categories = wp_list_categories('title_li=&show_count=1&echo=0&child_of=18');
$categories = preg_replace('/<\/a> \(([0-9]+)\)/', '</a> <span class="count"><sup>\\1</sup></span></li><span class="separator">/</span>', $categories);
echo $categories;
?>
</ul>
Use Like
<?php
$terms = get_terms('category'); /*Name Of category*/
foreach ( $terms as $term ) {
echo $term->name.'<br/> '.$term->count.'<br/>';
}
?>
Output
I have a normal posts page in an Anchor CMS theme. Beside the posts list is a category list, which I am intending to use as sorting filters for the blog posts. The category list has been output as follows, based on the Anchor Docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<div class="filter" data-filter=".category-<?php echo $id; ?>"><?php echo $category; ?></div>
<?php endforeach; ?>
I then have to include a corresponding class in each article or post within that list, which would look like this:
<?php if(has_posts()): ?>
<?php $i = 0; while(posts()): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endwhile; ?>
<?php endif; ?>
Notice that the article class category-[num] corresponds with the data-filter on the div "filter". This is what allows sorting.
However any way I try to do this I am getting either doubled up posts or just not working. I had tried using a foreach statement as seen in the docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endforeach; ?>
but this makes posts double up, I assume because it is within a while loop?
In the database, categories and posts are in two separate tables, however the category ID is included in the posts table. I have looked for a way to echo this e.g. article_category_id but with no success so far.
How can I include the category ID in the posts list?
Okay, found the answer to my own question.
This was a simple solution actually, once I thought about it. The various function references in Anchor CMS such as article_category etc can be defined by the user by accessing anchor > functions > articles.php.
Within this file, and based on existing functions such as
function article_category() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->title;
}
}
I created a new function for the category ID. It looks like this:
function article_category_id() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->id;
}
}
and then echoed this out in the posts loops:
<?php echo article_category_id(); ?>
Simple as that!
So in a nutshell, you have two options for displaying Contacts in Joomla:
Show all Joomla Contact Categories.
Show all Joomla Contacts in a single Category.
I want to use the first option, but merge a list underneath each Category showing the list of contacts within that category, and a link to their profile.
The simplest way I thought of this was to edit a template override of the file com_contact/categories/default_items.php
I found a point where I want the list to appear, and then copied and pasted the code from the Category view (that generates the list of contacts).
<ul>
<?php // Add list of contacts for each category
foreach ($this->items as $i => $item) : ?>
<li>
<a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
<?php echo $item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
But I am assuming I can't just copy and paste, as there needs to be an extra node added to $this->items.
At the moment, no list is being generated, just the <ul> outside the foreach loop.. but also interestingly, the <li> and the <a> IS being generated.. but linking to the current page I'm on (Probably because $item->slug is still being seen as the category).
So can anyone point me in the right direction as to how to reference the contacts within a category? All I'm after is the name and the slug/URL.
UPDATE:
I saw this in the same file (default_items.php) and although I realise it's referring to child categories... would this be a place to start for the actual contacts within the categories?
<?php if (count($item->getChildren()) > 0) :?>
<div class="collapse fade" id="category-<?php echo $item->id;?>">
<?php
$this->items[$item->id] = $item->getChildren();
$this->parent = $item;
$this->maxLevelcat--;
echo $this->loadTemplate('items');
$this->parent = $item->getParent();
$this->maxLevelcat++;
?>
</div>
<?php endif; ?>
BUMP - Does anyone have any experience with this? Or being able to call individual contacts when viewing a category? How are they linked?
For Category view in file default_children.php after tag <li... add code:
<?php
// Get Category Model data
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', $child->id);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
?>
For Custom Fields add this after previus code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
foreach($contacts as $contactItem) {
$currentContFields[] = FieldsHelper::getFields('com_contact.contact', $contactItem, true);
}
I'am trying to create a menu using wp_list_pages and I want the end result to look like this:
parent ( no ancestor ) <-- This one should be hidden
- Children "Some Title"( clicked )
- Children1 to "Some Title"
- Children2 to "Some Title"
- Children "Some other title" ( When clicking on this, children to "Somte Title" will hide and children to "Some other title" show )
I am using the following code, and I cant figure out how to modify it to give me the result I want.
<?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);
//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=1&child_of=".$post->ID);
}
if ($sidelinks) { ?>
<?php echo the_title(); ?>
<ul>
<?php //links in <li> tags
echo $sidelinks;
?>
</ul>
<?php } ?>
The example above gives me a layout looking like this and it shows the parent with no ancestor and not the sibling to the clicked Children:
parent ( no ancestor )
- Children "Some Title"( clicked )
- Children1 to "Some Title"
- Children2 to "Some Title"
Appreciate any help!
You can use exclude_tree and depth parameters of wp_list_pages()
See the Wordpress Codex for more info: http://codex.wordpress.org/Function_Reference/wp_list_pages
I have a navigation structure like this:
Home
About
The Team
Our Mission
Locations
Chicago
New York
Los Angeles
Services
Contact Us
If I'm looking at the About page, I want the sidebar to display:
The Team
Our Mission
If I'm looking at the Services page, I want the sidebar to display:
Home
About
Locations
Services
Contact Us
This is the code I'm currently using. How can I modify it so that it works the way I want?
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
}
$children = get_pages('child_of='.$parent);
if( count( $children ) > 0 ) {
?>
<ul>
<?php wp_list_pages ("&title_li=&child_of=$parent"); ?>
</ul>
<?php } ?>
this should either do it outright or at least show you how:
when viewing a Page that has children (or is a child) it displays only children of that parent.
When visiting main page, all top level pages are listed in the sidebar.
When visiting a top level page with no children, all top level pages are listed.
When visiting a top level page with children, just the children pages, and descendant pages, are listed.
When visiting a child page, just the children, and descendant pages, of that parent, are listed.
<?php
$output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
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=' );
if ($children) {
$output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
}
}
echo $output;
?>
from: http://codex.wordpress.org/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage
This method takes advantage of the echo=0 parameter, which returns the results as a string to a variable. The if ($children) test now works because wp_list_pages() will return empty if it finds no matches.