Wordpress - output automated secondary navigation on posts as well as pages - php

I currently have a script that outputs a secondary navigation based on the site menus, however I can't think of anyway to link a post to a page so that the posts can still show a secondary navigation, is this even possible? Here is the current code i'm using to output my secondary navigation:
<?php
$secondAncestor = count($post->ancestors) -1; //figure out what level of navigation we are on, subtract one because we don't want to consider the top-level
if($post->post_parent!=0) //if the page is not a top-level category
{
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li class="sidebarlist">';
//the following lists children of second level ancestor of the current page.
wp_list_pages("title_li=&child_of=".$post->ancestors[$secondAncestor]."& sort_column=menu_order&echo=1");
echo '</li>';
}
else //if the page is a top-level category
{
//listing only the child pages of the current section
$children= wp_list_pages("title_li=&child_of=".$post->ID."& sort_column=menu_order&echo=0");
if($children) //this will stop it from displaying a section heading if there are no elements in the section (for example on the home page)
{
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li>';
echo $children;
echo '</li>';
}
}
echo '</ul></nav>';
?>

Pages are heirarchical, so they can have parents and children.
Posts are flat, and related by categories and tags.
In order to relate a post to a page, I think you're going to need to use a custom field on your posts. You could call it Parent Page ID, and then in your sidebar code, add a custom query that checks for posts whose Parent Page ID = the current page's id.

Check out Flexi Pages Widget. This will add a highly configurable subpage menu to your sidebar, which you can embed on posts & pages.

Related

Contao: Frontend - Get theme section of another page - not the current page

I would need to reach a custom section by page id (or PageModel Object), but I don't find a way to get the FrontendTemplate. I would like to use it in a dropdown navigation to echo a custom section of the hovered (parent) page.
If somebody will search for, the answer is:
<?php
$articles = \ArticleModel::findPublishedByPidAndColumn($id, $column_name);
echo static::getArticle($articles[0]); // for ex. the first article
?>

WordPress - displaying posts from a category on the same page

I have a page named "Shop". On that page I display a custom menu, which contains links to categories like "Books", "Shoes" etc. Whenever I click one of those links/categories it takes me to a relevant category page, for example /category/books/.
I do not want it to redirect me to a category page, I want it to display posts on the same page ("Shop") that the menu item was clicked on. The only problem I have encountered trying to accomplish this, is the fact that I do not know how to change the custom menu behavior. I don't want it to redirect me, but instead send a GET value to the same page ("Shop"). Then the shop page would take that GET value and display relevant posts.
I know how to display posts from a category etc. I just don't know how to change the behavior of the custom menu.
Could anybody help me? I would be grateful.
Original Answer:
Use wp_get_nav_menu_items:
$menu_slug = 'YOUR_MENU_SLUG';
$menu_id = get_nav_menu_locations()[$menu_slug];
$menu_items = wp_get_nav_menu_items($menu_id);
foreach($menu_items as $item){
if($item->object == 'category'){
print('<p>Title: ' . $item->title . '<br>ID: ' . $item->object_id . '</p>');
}
}
wp_get_nav_menu_items returns an array of menu items. You get several information on the item, like type (post, category, ...), id, title and so on.
See http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items for a full list of properties.
Hope this helps :)
Update:
Assuming your permalink setting is set to default, the following code will print a modified menu that uses GET.
$menu = wp_nav_menu(array(
'theme_location'=>'YOUR_MENU_LOCATION',
'echo'=>0,
));
$new_url = $_SERVER['SCRIPT_NAME'] . '?shop_page=$1';
$menu = preg_replace('/href="[^"]*cat=(.+)"/', 'href="'.$new_url.'"', $menu);
print($menu);
The regex may not be the best as it ignores other GET values and I'm not experienced on them. If your permalink setting is different and every time you change it, you will have to edit the regex.

Show parent page content on a child page - Wordpress

I have been trying for the past couple days to show the content from a parent page on a child page, but I wasn’t able to do so. Most of the codes I found were to show child page content on a parent page and had a loop.
What I’m looking to do is just grab the text from the parent page and display it on the child page, no loops needed.
Any help is appreciated.
Thank you.
use post_parent
<?php
$parent_id = $post->post_parent;
$parent_post = get_post($parent_id);
$parent_content = $parent_post->post_content;
echo $parent_content;
?>
Don't forget to use wp_reset_query(); in order to get the current page content.

How can I get the list of custom wordpress menu contents to be used in an if statement?

I am adding a sub menu to an "About us" section and would like the sub menu to appear on all of its sub sections as well.
Instead of creating an if statement with each subpage added to the if statement manually is there a way to display the contents of the custom list so the if statement updates itself when new sections are added to the sub menu?
eg
$about_sub_menu_contents= something snazzy to display the menu contents;
if($about_sub_menu_contents){
wp_nav_menu('menu=about_sub_menu');
}
I want to avoid having to add every subpage to the if statement for the menu to appear on that page.
<?php
$ancestors = get_post_ancestors($post);
if (is_page(123) || in_array(123,$ancestors)) {
wp_nav_menu('menu=about_sub_menu');
}
?>
Where 123 is the post-ID of the parent page (the "About us", in your case).

News System issue (Php and Mysql)

I'm creating a news system for my website. What I have is a main page {index.php} (where all the articles are shown) and an article page (article.php)
What I'm having trouble with is having content selected on the article page.
When you click on the title on the index.php I want it to go to the article.php and show that articles information (currently achieved through get articleID)...
When you click the category of the article, I want it to go to article.php and show ALL the articles with the same category
How can I set up my code to accomplish this?
Currently I have for the article.php page
if( isset($_GET['category']) ){
$subject= $_GET['category'];
} else {
$subject= $_GET['id'];
}
$STH = $DBH->query('SELECT * FROM articles WHERE category="$subject"');
while($row = $STH->fetch()) {
echo $row['content'];
}
For the index.php page I call the category through this
echo '<a class="post-type" href="articles.php?category='.$row['category'].'">'. $row['category'] .'</a>';
Well You're missing a bit of thing here. Actually you need three pages, not two. One page is index.php which actually shows up all the latest content (articles). Other one page is to show single article (article.php), the third page is articles.php or category.php, this page will show the articles like index.php page but of particular category.
Note: You can manage with two pages as well but you'll have to manage your article.php for two different layouts, one is single article and other is article list like index.php page. So i suggest to have articles.php or category.php as a seperate page.

Categories