Show parent page content on a child page - Wordpress - php

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.

Related

Result of get_permalink(0);

I'm new to wordpress and I'm following a tutorial right now but I don't understand wordpresses behaviour. I'm trying to change the title of a sidebar which lists the parentpage and it's childpages, this works fine but I don't understand why $parentID = wp_get_post_parent_id(get_the_ID()); echo get_permalink($parentID); works even on the parent page, I echoed the result on the parent page and it returns 0 since the parent page doesnt have a parent, so why does this still work? Why does get_permalink(0); get me to the parent page if I press the button?
get_permalink()is the function that gives you the link to the post/page inside the loop you are currently. Since you are not having parent page for the current page get_permalink is not able to get you to the parent page as nothing exist so in this case it will rediret you to the same page.

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 list child pages of custom post type

Im stuck on this and any help would be appreciated.
Say there is a parent page called "Stuff". And Stuff has several pages under it.
Stuff
more stuff
more stuff1
more stuff2
It has no children
Other with children
etc
etc 2
So when you click on "more stuff". That page would list all the pages under stuff ('more stuff, morestuff1, morestuff2').
Or if you click on "etc" you would see "etc, etc2"
Any thoughts or help would be appreciated. I'm using the Types plugin and have hierarchical setting turned on already.
You can use the wp_list_pages function. In http://codex.wordpress.org/Function_Reference/wp_list_pages, scroll down to the section with sub-heading "List subpages even if on a subpage" to see an example. Just make sure to pass the post_type parameter like this and include the code inside the loop of your single template file:
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&post_type=<post_type>");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&post_type=<post_type>");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php }

WordPress - How to get IDs for all pages in the current menu?

I am building a one page scrolling wordpress theme, and I want to be able to create a simple loop that will basically grab the page id's that are in the current menu, then spit out the content.
I already know how to spit out the content for a page, given the id, but I don't know what the proper way to get all the ids from the current menu is.
Any suggestions?
Figured it out:
$menuItems = wp_get_nav_menu_items('main-menu');
foreach($menuItems as $page) {
$post = get_post($page->object_id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}

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

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.

Categories