How can I access (echo) a pages navigation label in Wordpress? - wordpress

This is a continuation in a way of this question:
Wordpress Navigation Label in Browser tab
but I am afraid I need more guidance then the previous poster.
I too want to use the navigation label in the WordPress menu, to display the parent navigation label (and not the parent title!) before the page's actual title.
The code that works for the parent title is this:
<h1 class="page-title"><strong><?php
// If child page , also include the parent title:
if($post->post_parent)
{$parent_title = get_the_title($post->post_parent);
echo '<span>'.$parent_title.' — </span><br />';
}
the_title(); ?></strong></h1>
I would like to swap out .$parent_title. for the parent-navigation-label. Is there a way to echo that label?
Disclaimer: I am not a programmer by choice - only when I have to, so please be gentle...
Edit: This is the navigation label I am talking about btw:

The label is stored in the post object as title:
$parent = get_post( $post->post_parent );
echo '<span>' . $parent->title . ' — </span><br />';

(Ok - If there is a way to include formatted code in the comments - please let me know!)
Building on #diggys answer I added this:
<h1 class="page-title"><strong><?php
// Display the parent nav label:
$parent = get_post( $post->post_parent );
echo '<span>' . $parent->title . ' — </span><br />';
the_title(); ?>
</strong></h1>
but the output is empty - apart from the the — character (-). I must be missing something obvious, right?

Related

How to change "Read more" button text on Wordpress posts

After updating plugins, my "Read more" button on posts changed it's name. In Latvian language it was "Lasīt vairāk", while in Russian "Читать дальше". It's just in plain English. The image of how it looks now is via this link. Plugin updates basically wiped out the padding and the name of the button.
Tried modifying functions.php with
// Replaces the excerpt "Read More" text by a link
function modify_read_more_link() {
return '<a class="read-article" href="' . get_permalink() . '">Your Read
More Link Text</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return '<a class="read-article" href="'. get_permalink($post->ID) . '">
Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
Tried loco translate and modifying internal translations of Elementor-related plugins. The closest I could get is that the elements is named "eael-post-elements-readmore-btn". Styling the element with CSS doesn't do anything. Padding or margin do not work. It's locked. Can anyone provide a hint?
The button should be in Latvian and Russian language, not in English.
PS. Figured that it is Elementor posts plugin related overriding functions.php and translators as well. At this moment, can't figure how to CSS this thing. Stays static.
Add this in functions.php:
function develop_custom_excerpt_more($more) {
global $post;
// edit here if you like
return '... <a class="excerpt-read-more" href="'. get_permalink( $post->ID ) . '" title="'. __( 'Read ', 'domain_name' ) . esc_attr( get_the_title( $post->ID ) ).'">'. __( 'Show more »', 'domain_name' ) .'</a>';
}
add_filter( 'excerpt_more', 'develop_custom_excerpt_more' );
Figured that it is Elementor posts plugin related overriding functions.php and translators as well. The eael-post-elements-readmore-btn was not changing in padding nor in margin because line-height which by default is set to 1 could not allow space for expansion.

Wordpress - Tag glossary with descriptions and contents

This is a rather complicated one, so I'm sort of expecting this to sink without a trace - but worth a go.
What I'm looking to do is have a Tags page which has a table of contents (tags list). When clicking on a tag in the table of contents, you would skip down the page to the relevant tag (using an anchor?). Here it would display the chosen tag name with its tag description and link to the tag page.
Any ideas on how I could implement this would be greatly appreciated. I've combed the Wordpress codex and Stackoverflow and not found much documentation on echoing tag lists in this way.
Thanks in advance anyone who can help!
You can use the get_tags functions to get all the tags, make a ul with them with links to their slugs(or anything unique) and then display them with something like this (slightly modified version of the codex example)
$tags = get_tags();
$html = '<div id='anchor_{$tag->slug}' class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;

Parallax wordpress theme. How can i get dynamically the page ID's to display their own sub-menus?

I'm creating a wordpress theme with parallax features and html5, the home has all the main pages and two different kind of navigations.
Main one, is the navigation for parent pages. So I have About, Projects and Contact.
But each one of this pages has child pages. About page has one child that is below it, but as i cannot access from the main menu, each page that has childs or subpages has the second type of menu.
Also projects, has different pages: "Projects" (main), "Design" (child), Consulting (child), etc.
The second menu that is placed on the right of the page as rounded buttons lists the parent "about" and the child "about 2".
I already figured it out a way to list those and to make the links behave with scroll to (parallax behavior)
But, the way that i have hardcodes the parent page. What i would like to do is to find a way to get dinamically the page ID so it won't be hardcoded, but as the theme works as a only one page site, it's making me get troubles.
Here is the code that i have, could anyone help me to find a way so the code is going to identify the ID of each current page to list the main and the children pages ?
As you see $parent = 13; is hardcoded, and also it includes $pages = get_pages('hierarchical=0&include=13') ; with the id hardcoded too. So all the pages are showing the same. I know i can create a conditional with is_page for each hardcoding it, but the idea is to make it dynamic.
Any help will be great!
THanks,
<?php
$parent = 13;
$args= array(
'parent' => $parent,
'hierarchical' => 0
);
$pages = get_pages('hierarchical=0&include=13') ;
foreach ( $pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li>' . $page->post_title . '</li>';
}
$child_page_pages = get_pages($args);
foreach ( $child_page_pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li>' . $page->post_title . '</li>';
}
?>
To get post parent page ID's you can use get_post_ancestors( $post ) it should return the parent ID.
$ReturnParents = get_post_ancestors($post->ID);
Another method you can try is the global $post.
global $post;
global $wp_query;
$thePostID = $wp_query->post->ID;
$myParent = $post->post_parent;
echo "I am post ". $thePostID . " and my parent is " . $myParent;

Drupal 7 - Menu to UL

all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.
I have my main menu and it's corresponding block in a Drupal site I am building. Like all other Drupal menus it contains a bunch of links to various parts of the site. I can assign it's block to a region and the menu links come out all nice and formatted with a title thing and little bullet points. The problem though is that I am making a custom theme for this website and I need to be able to work with the links without all the cruft added, preferably in something simple like an ul.
Is there any function that takes a menu and produces an ul containing all the links?
Maybe there is some way you can reduce the menu's block to just an ul.
I have been experimenting with theme_menu_tree(...) and theme(...) to no avail.
Thank you!
I find you can do most changes through CSS such as setting <H2> titles to display: none and setting <LI> tags to float: left for a horizontal navbar.
But... if you want to build your own menu from the Drupal data, here is some code from a site I'm working on. It builds a two-level menu. I'm sure you could simplify this code even further if you need.
//----------- primary menu (horizontal with drop-downs) -------------------------
$params = array('max_depth' => 3);
$menu = menu_build_tree('main-menu', $params);
$variables['menu'] = $menu;
$html = '<ul>';
foreach($menu as $item_menu) { //for each main element
$isSecondLevel = isset($item_menu['below']) && !empty($item_menu['below']);
if ($isSecondLevel) {
$html.= '<li>';
} else {
$html.= '<li class="sg">';
}
$html.= '<a class="topLevel" href="'.url($item_menu['link']['link_path']).'">';
$html .= $item_menu['link']['link_title'];
$html .= '</a>';
//is there any sub elements to display
if ($isSecondLevel) {
$html.= '<ul>';
foreach($item_menu['below'] as $item_submenu) { //for each sub element
$isThirdLevel=isset($item_submenu['below']) && ! empty($item_submenu['below']) ? 'main-menu-third_level' : '';
$html.= '<li>';
$html.= '<a href="'.url($item_submenu['link']['link_path']).'">';
$html.= $item_submenu['link']['link_title'];
$html.= '</a>';
$html.= '</li>';
}
$html.= '</ul>';
}
$html.= '</li>';
}
$html.= '</ul>';
$variables['main_menu_html'] = $html;
This code was placed inside function pinkribbon_process_page(&$variables) in template.php. Menu is printed in the template by calling <?php echo $main_menu_html ?>
Simon.
P.S. Others, please feel free to edit this code for clarity/simplicity.
I advice you to use
menu_tree_output
like this:
print render(menu_tree_output(menu_build_tree('main-menu', $parameters)));
You could call menu_build_tree and look at it's output and build a ul from it. However, despite the default menu output having loads of "cruft" it is a ul and should be themeable with CSS.
If you really want to build the menu yourself, I would reverse engineer another module that does so like Nice Menus

Drupal 6: Printing Unadulterated Primary Links and all children

How in the WORLD is possible? I swear, I've read the equivalent of 3 encyclopedias to no avail. I've tried solutions within regions, page.tpl.php and blocks. None of them give me what I need... and I know there are so many other people that need this too!
I've come to the conclusion that I want to print out the menu within my page.tpl.php ... so no block solutions, please.
I want to be able to loop through the primary menu links (AND children) and rewrite the output so that there's no default Drupal class tagging. The closest I've found is this example:
<?php if (is_array($primary_links)) : ?>
<ul id="sliding-navigation">
<?php foreach ($primary_links as $link): ?>
<li class="sliding-element"><?php
$href = $link['href'] == "<front>" ? base_path() : base_path() . drupal_get_path_alias($link['href']);
print "<a href='" . $href . "'>" . $link['title'] . "</a>";
?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
As you can see, links are being reprinted with a custom UL and LI class ... that's GREAT! However, no children are being printed. How would I extend this code so that all children are a part of the list? NOTE: I don't want the children to only appear on their parent page, they must be present all the time. Otherwise, the drop-down menu I have planned is useless.
I sincerely thank you in advance to lessening my gargantuan headache!
It's hard to affect the output once it's got as far as the page.tpl - you might do better looking for template.php functions.
This is one I used to alter the classes of my primary links:
function primary_links_add_icons() {
$links = menu_primary_links();
$level_tmp = explode('-', key($links));
$level = $level_tmp[0];
$output = "<ul class=\"links-$level\">\n";
if ($links) {
foreach ($links as $link) {
$link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
$output .= '<li class="sublevel">' . $link .'</li>';
};
$output .= '</ul>';
}
return $output;
}
And then in page.tpl.php I just called it like this:
<?php if ($primary_links) :?>
<?php print '<div id="menu">'; ?>
<?php print primary_links_add_icons(); ?>
<?php print '</div>'; ?>
<?php endif;?>
I had to add a <span> to my links for styling, so I overrode theme_links() in includes/theme.inc
You can copy the function to your template.php, rename it to yourthemename_links(), and modify it as needed.
This function outputs the ul, li tags, the drupal_attributes, classes of 'first', 'last', 'active', etc, and affects the menus throughout the site.
You may also want to check out the functions in includes/menu.inc, including theme_menu_local_tasks() and menu_local_tasks(), if you need to output the primary and secondary differently.
MarkLNH

Resources