How to add title attribute when a page has a particular template - wordpress

in my admin page i can change the default template page to another custom template.
What i need is:
In my nav header menu i have many links and i have to set title attribute of a tag when the a tag point to a page that will be rendered with my custom template and not with the default one. Example:
<li><a hreh=".." title="myCustom">link1</a></li> //this title will be redirected with my custom template
<li><a hreh="..">link2</a></li> //this title will be redirected with default template
<li><a hreh="..">link3</a></li> //this title will be redirected with default template
<li><a hreh=".." title="myCustom">link4</a></li> //this title will be redirected with my custom template
If i open header.php that links are created by:
<?php $params = array(
'theme_location' =>'primary',
'limit' => 5,
'format' => 'custom',
'link_before' => '<span>',
'link_after' => '</span>' );
wp_nav_menu($params);
?>
How can i check if link is rendered by default template or by mine one?

I'm not sure what you are trying to do but about the title thing, you can easily add that at backend... Appearance > Menus. A sample image below..
I'm not sure if this will help but have you tried reading Creating Your Own Page Templates?
So that when you create a page, you can choose what template to use for that certain page. Like this image below,

If your theme is using the function body_class, like <body <?php body_class(); ?>>, your Html is already printing what you need to target elements only in that page:
This page is using the template file: /twentytwelve/page-templates/front-page.php.
You see there's a StackOverflow class there ;), and that's by:
add_filter('body_class', 'body_class_so_14302462');
function body_class_so_14302462( $classes )
{
global $post;
if( 'page' != $post->post_type )
return $classes;
$classes[] = 'STACKOVERFLOW';
return $classes;
}
Instead of adding a class to the array, you could completely override it using $classes = 'custom-and-only-class';, and also do all kind of checks to fine tune the output.

Related

How to display posts and pages on a WordPress tag page?

Adding tags to WordPress pages
I try to display both posts and pages that are tagged with the same tag when I'm on a WordPress tag page. I'm using some custom code to be able to add the tag functionality to WordPress pages. Adding a tag to a page works and I can also display the tag on the page when I'm using <?php the_tags(); /> in a page template.
I'm using this code (in my child theme functions.php) to register the tag functionality to pages:
// Add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
add_action('init', 'tags_support_all');
The issue - Pages with tags are not being displayed on the tag page
I can't seem to find a way to get these pages displayed on the tag page. It only shows the posts that are tagged with the same tag. No pages. I use the code below to try and update the pre_get_posts query by setting the post_type to 'any'. It's an (old) snippet that I found on Google why I was searching for a solution. I'm not sure if this is still the way to go, but I couldn't find any working alternatives.
// Display all post types on tag pages
function tags_support_query($wp_query) {
if ($wp_query->get('tag')) {
$wp_query->set('post_type', 'any');
}
}
add_action('pre_get_posts', 'tags_support_query');
Any ideas on how to get this working?
Some extra information:
WordPress version 5.7.2.
I'm using a theme and (custom) child theme combination. The theme uses the Gutenberg editor.
The above code snippets are added to the child theme functions.php
There's no specific tag.php or archive.php. The tag page uses the default index.php to display the tag page. This index.php is also used on categories, archives, etc.
Create a new file that you can use for all Tags and call it tag.php. This will allow all Tag related archive pages to be rendered by this file. Other categories and archives will remain untouched.
In this new file copy all contents of index.php but add the following code in the top of the file.
/**
* Get the Term ID of the current term.
*/
$term = get_queried_object();
$term_id = $term->term_id;
/**
* Get all posts and pages with the current term.
*/
$args = array(
'post_type' => array('post', 'page'),
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => [$term_id]
)
)
);
/**
* Create a new query.
*/
$query = new WP_Query($args);
Then replace all occurences where the following occurs:
if ( have_posts() ) {
// replace with
if ( $query->have_posts() ) {
,
while ( have_posts() ) {
// replace with
while ( $query->have_posts() ) {
and
the_post();
// replace with
$query->the_post();
Then after the closing bracket of the while loop, add wp_reset_postdata(); to ensure that the post data related to the page has been reset. (otherwise the latest post in the loop will be considered the post of the page).
This should (at least) give you access to the posts and pages that have the post_tag term that corresponds with the page.

How to display specific css styles wp_nav_menu()

I am really new to wordpress. I have a custom menu that I would like to only display specific menu item when the users browser lands on a specific page.
wp_nav_menu(array('theme_location' => 'menu-main'));
Is there a way to have wp_nav_menu() hook to only display specific menu items that have specific css class items?
If I understand your question well, Yes, there is a way. You can use conditionals. PHP if statements. WordPress allows for you check the page title, ID, category before you execute a command e.g
if( is_page( array( 'about-us', 'contact', 'management' ) ) {
//either in about us, or contact, or management page is in view
//execute something
} else {
//none of the page about us, contact or management is in view
//execute something
}
And you can add a class/ID to the wp_nav_menu
So joining the two ideas, you can have:
// When Page 42 (ID) is being displayed.
if ( is_page( 42 ) ) {
wp_nav_menu(array('menu_id' => 'pines', 'menu_class' => 'pnav'));
} elseif( is_page( array( 'about-us', 'contact', 'management' ) ) {
wp_nav_menu(array('menu_id' => 'bananas', 'menu_class' => 'nav'));
} else {
wp_nav_menu(array('menu_id' => 'main-id', 'menu_class' => 'main-nav'));
}
Having added unique IDs and classes, you can style this in the way you want.
Reference https://codex.wordpress.org/Conditional_Tags
Consider searching for existing plugins prior writing custom functions. For your specific feature, I would use the Page Specific Menu Items plugin.
If you would like to add to an existing function, consider using the body_class filter. With this function, you would be able to add a css class to your body element, then write css that changes how your menu appears for any specific page.

Display a list of child pages as a menu, inside a parent page in WordPress

I'm building a WordPress site and I need to create a menu made of child pages inside a parent page. So, I've achieved this correctly in 2 ways:
Building a custom menu and adding it inside a widget
Following this tutorial:
http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/
What I'm trying to get is smth similar as in the left side of this site: http://optinmonster.com/how-it-works/actionable-insights/
But I'm having a problem, cause when I click on the menu elements I created, they open up as pages of their own. While I want them to show up on the parent page... The idea is that I want different content to display in the parent page based on the menu items I click on. What should I do?? Is there a plugin for this??
P.S, I believe that I haven't yet found a solution for this problem because I'm googling it wrong, so is there a definite name for the problem I'm having??
I would really appreciate some help.
<?php
if ($post->post_parent) {
$page = $post->post_parent;
} else {
$page = $post->ID;
}
$children = wp_list_pages(array(
'child_of' => $page,
'echo' => '0',
'title_li' => ''
));
if ($children) {
echo "<ul>\n".$children."</ul>\n";
}
?>

WP: Display a specific custom post type list (archive) and that post content (single) within a custom page template

Here's what I have.
I have a custom page template that I use for a parent page and all child pages created under that parent. This template dynamically displays the parent, "About", and all child pages, "Contact", "Terms", "Privacy", etc. on the left navigation side bar and cooresponding content as the user clicks around.
I have a custom post type, "Press", and the client wants this as a new menu choice on said left navigation side bar i.e.; "About", "Press Releases", "Contact" . . . and so on . . .
I assume I need to inject some logic into the custom page template that currently has the basic query for "page" content. I'm guessing the logic would be something like "Get me some normal page content BUT if the user clicks on "Press Releases" I'll engage another alternate query that pings the CPT "Press", grab that post_id and display the content accordingly.
Am I off track here or just having a really bad brain fart today?
Did you try adding the custom post type to the wp_list arguments?
<?php wp_list_pages( $args ); ?>
<?php $args = array(
'child_of' => 0, //parent page
'exclude' => '',
'include' => '', //parent page
'post_type' => 'page, Press' //include custom post type
); ?>

wp_nav_menu not working on category pages on wordpress

I created a couple of custom menus on wordpress using the appearance menu.
I want to display the right menu according to the page that I'm viewing. I wrote the script below in the header.php file in the seems to be working fine. It's pulling the correct menu according to the page that I'm visiting with the exception of the category pages.
When I'm on a category page, the wp_nav_menu function falls back to the fallback function, indicating that the menu doesn't exist?!
I was looking around and the solution that kept coming up was adding the following code but it doesnt seem to work.
<?php
wp_nav_menu('container_class=menu-header&theme_location=primary');
?>
Here is the full code that I added to the header.php file:
<?php
switch( $master_page ) {
case 'about':
wp_nav_menu(array(
'menu' => 'about',
'fallback_cb' => 'get_cat',
));
break;
case 'offer':
wp_nav_menu(array(
'menu' => 'offer',
'fallback_cb' => 'get_cat',
));
break;
}
?>
I'm really frustrated at this point. Any ideas?
I don't see where you are assigning a value to $master_page, so at this point it is just an empty variable.
You need to look into the is_page() and is_category() functions in WordPress and use if statements rather than a switch.
<?php
if( is_page( 'about' ) ) {
wp_nav_menu(array(
'menu' => 'about',
'fallback_cb' => 'get_cat',
));
} else {
...
}
?>

Resources