I'm using this script to call a link to the parent page inside one of it's children pages:
<?php
$this_page = get_post($id);
$parent_id = $this_page->post_parent;
if ($parent_id) {
$parent = get_page($parent_id);
echo 'Return to '.get_the_title($parent->ID).'';}
?>
This sample works just fine here:
http://www.energytoolsint.com/clean-sweep/parent-test-call/
But won't parse here?
http://www.energytoolsint.com/healthcare-practitioners/
Same page template, one renders, the other doesn't.
Judging by the URL, that's because energytoolsint.com/healthcare-practitioners is a parent page. Granted, you could have changed the permalink, but the URL suggests it doesn't have a parent, hence the null value.
Go to the page in dashboard. Look on the far right under the publish date.
Related
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.
I have a photo gallery page that is using a single page in Wordpress.
The gallery display is dynamic, and relies on a URL parameter. The parameter itself is an integer of the relating wordpress page ID. (as that's the page it's about).
Here is an example of a gallery URL:
http://www.bellavou.co.uk/gallery/?pid=238
For SEO purposes, I'm trying to get away from every possible Gallery URL combination to have the page title 'Gallery', as is set in the Wordpress page settings.
I've been trying this, which has been mentioned a few times:
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
But as I'm also using Yoast SEO plugin, I think this is over-writing it (even though I uncheck 'Force title rewrite', and keep the page title field blank). Not sure why the above function doesn't seem to work.
Within the gallery page, I'm able to show the h1 title properly, passing PHP Variables into the code, but I want to do the same to the page title, but in a way that can be done directly on the page template.
Why is it that whenever I post a question, I seem to find the answer very soon afterwards?
I read a post saying that it helps to put a wp_title function before the wp_header call, which is what I tried, and that seems to work fine.
I can edit the page title just for an individual page using the PHP page template, and using this:
$procedure = isset($_GET['pid']) ? $_GET['pid'] : '';
add_filter( 'wp_title', 'wp_title_gallery', 100 );
function wp_title_gallery($title) {
global $procedure;
if(!$procedure) {
$title = the_title();
} else {
$title = the_title(get_the_title($procedure).' ');
}
return $title;
}
get_header();
$procedure gets the URL parameter, and then the function takes that ID, and gets the associated page from it, and then prints the title of the page, into the page title hook. Lastly, the WP header is called.
(Just incase anyone comes here looking for a solution)
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.
What I'm trying to do is convert my one page design in wordpress and i thought it would be nice to be able to edit, add and modify different part of the page in seperated pages. The one page website will be ordered by a menu (with id main).
Following the wp-codex i used get_template_part, it should work properly because it should:
Load a template part into a template (other than header, sidebar, footer)
get_header gets skipped but get_footer gets excecuted and the site is rendered incorrectly.
front-page.php
$pages = wp_get_nav_menu_items('main');
global $post;
foreach ($pages as $post) {
$post = get_post($post->object_id);
if($post->post_type != "page") continue;
setup_postdata( $post );
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? null : $post->post_name;
get_template_part('page', $postName);
}
wp_reset_postdata();
Is this a bug? Or did I do something wrong? What is the best way to accomplish this?
if your get_template_part() is not working, then the problem may be there is no file named as page.php in theme, check for this file.
For what it's worth, I often skip a some of the WP helpers and work more directly, but not rogue. Here is a snippet from my library :
/** Get core data for a WP post/page using ID
* #param $id : wp post/page ID
* #return array
*/
function wp_get_single_post_basic($id){
$post = get_post($id, ARRAY_A);
$ret = array();
$content = $post['post_content'];
// FILTER via WP
$content = apply_filters('the_content', $post['post_content']);
$ret['title']= $post['post_title'];;
$ret['content']= $content;
$ret['link'] = $post['post_name'];
$ret['edit_link'] = 'edit';
return $ret;
}
That function breaks down the content in a very easy to manage manner and generates the edit link (build bool for that in calling script). Also formats content.
So grab and sort the IDs you want and run this in the loop over IDs for your one page. Then all of the content will be isolated by page/post/whatever (custom taxonomy FTW).
If you had the IDs ready and sorted plus the html/css ready to go I could drop this function in and work your one page to complete in under an hour. For production line work this type of helper is great -- also perfect for sites where you want a specific post/page to be placed in some weird way in some weird place outside of the loop.
If you have a problem w/script let me know, I have not used it in a few months, wrote it a few years ago...
I think you have a problem using the global $post and storing also the $post variable in the foreach loop from the menu objects. I think probably that's the cause of the error with the template part calling.
I would recommend you to remove the global $post declaration, the reset_postdata and also the setup_postdata since you aren't using the global in your loop, you're just naming it.
You're getting the $post object from the menu so it appears that you don't need the global or the other functions that are mostly used when you want to use "Loop Style" template tags without a post_id, like the_permalink() or the_title().
I ended up copying the default page.php template as page-page.php and loaded it like this:
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? 'page' : $post->post_name;
Then in page.php just loaded page-page.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.