I am building this website where I have pages with child pages, when I click on the parent link in the navigation menu, I get redirected to that page. It will then display links to all its child pages on that page, my problem is that I want the parent page link in the navigation menu to always be highlighted even when I click on the child page link. So in the end both the parent page link AND the child page link should be highlighted.
I have tried some different things, but none seem to work, I can loop out all the child pages of the parent, but not make them highlighted.
<?php
if ( $post->post_parent ) :
$parent = $post->post_parent;
else :
$parent = $post->ID;
endif;
$childpages = get_pages('child_of=' . $parent . '&sort_column=menu_order');
?>
<div class="row pad60">
<ul class="category-nav column">
<?php foreach( $childpages as $childpage ) { ?>
<a href="<?= get_page_link( $childpage->ID ) ?>">
<li><div class="primary-btn"> <?= $childpage->post_title ?> </div></li>
</a>
<?php } // End foreach ?>
</ul>
</div>
This is what I am using right now, but I feel this isn't the right way to go in the first place..
Thank you!
WordPress has a build in CSS classes to target the menus. https://developer.wordpress.org/reference/functions/wp_nav_menu/#Menu_Item_CSS_Classes
// This will highlight all the current menus
.current-menu-item a {
color: blue;
}
/* This will highlight its parent, when you are in its child page. */
.current_page_parent a {
color: orange;
}
Related
I am trying to replace the text on my WordPress site page titles with an image. I am working locally with a child theme. I need to also be able to target each page separately as the images will be different.
It currently looks like:
I want to design some custom titles in Photoshop and save them as Jpg/Png and then replace the text.
I think i have identified the right part of the CSS
.hestia-title{
}
I have tried
.hestia-title{
font-size: 0px;
background-image:url(assets/img/quick_test.jpg);
}
This makes it look like this:
Could anyone point me in the right direction with this please? Thanks
I have now been able to get it looking like this:
Using this code:
.page-id-88 .page-header .hestia-title{
color:transparent;
background-image: url(assets/img/title_mock.png);
}
.page-header.header-small .hestia-title{
background-size:contain;
background-repeat: no-repeat;
}
.home .hestia-title{
display:none;
}
This is the page.php content
<?php
/**
* The template for displaying all single posts and attachments.
*
* #package Hestia
* #since Hestia 1.0
*/
get_header();
do_action( 'hestia_before_single_page_wrapper' );
?>
<div class="<?php echo hestia_layout(); ?>">
<?php
$class_to_add = '';
if ( class_exists( 'WooCommerce' ) && ! is_cart() ) {
$class_to_add = 'blog-post-wrapper';
}
?>
<div class="blog-post <?php esc_attr( $class_to_add ); ?>">
<div class="container">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'page' );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div>
</div>
<?php get_footer(); ?>
You can go to the page.php file of your active child theme find out <? the_title(); ?> function. You can commnet this code then page title will not come.
For the image, you can use the featured image, set featured image on each page. Now you can use this code to display image
if (has_post_thumbnail()) {
the_post_thumbnail('full'); // just the image
}
To hide page title from CSS use this code
.page h1.hestia-title {
font-size: 0;
}
I have this class for my menu: .site-header-menu
and I have this class for my content: .content
what should I do to catch .content when .site-header-menu is displayed?
I already tried this but it doesn't work:
.site-header-menu + .content{
opacity: 0;
}
Do you have some suggestion? This kind of things are possible using css?
I'm using Wordpress so this is my php struture in header.php. The idea is to hide the content of my page template when site-header-menu is open:
<?php if ( has_nav_menu( 'primary' ) || has_nav_menu( 'social' ) ) : ?>
<!--<button id="menu-toggle" class="menu-toggle"><?php _e( 'Menu', 'twentysixteen' ); ?></button>-->
<button id="menu-toggle" class="menu-toggle" data-title="<?php the_title(); ?>"><?php the_title(); ?></button>
<div id="site-header-menu" class="site-header-menu">
<?php if ( has_nav_menu( 'primary' ) ) : ?>
<nav id="site-navigation" class="main-navigation" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'twentysixteen' ); ?>" data-title= ''>
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'primary-menu',
) );
?>
</nav><!-- .main-navigation -->
<?php endif; ?>
How is .site-header-menu being displayed? Is a class being added to display the menu or is its display property being directly manipulated by JavaScript? There is no indication how you can tell if the menu is displayed?
.site-header-menu + .content{
opacity: 0;
}
The above rule, because of the plus sign, will only apply if the element with class content is immediately following the element with class site-header-menu in the DOM. For example:
<div class="site-header-menu">
... menu stuff ...
</div>
<div class="content">
... content stuff ...
</div>
This might be the problem, but without actually seeing the output HTML it's hard to say.
If you are using PHP or JS to toggle the visibility of the menu you could potentially add/remove an additional class to .content that will hide/show it based on the menu being displayed or not.
/* CSS rule to hide the .content element */
.content.hideMe {
visibility: none;
}
// pseudo-script click event handler (NOT REAL SCRIPT!)
function menuButtonClick() {
if (menuCurrentlyDisplayed) {
// menu is already displayed so hide it and show content
hideMenu;
removeClass(".content", "hideMe");
} else {
// menu is not yet displayed so show it and hide content
showMenu;
addClass(".content", "hideMe");
}
}
If you could post your output HTML, or set up a fiddle for us to take a look, it would be helpful.
I am trying to figure out how I can stretch my slider image to the full-page width, but have it also be responsive. I am running on Wordpress and I'm not opposed to using Javascript if necessary. Here is the site: http://cmattayers.com/b2bu/
This is my original mockup, if that helps: http://i.imgur.com/jCWPIsp.jpg
Pull the slider div at the end but inside just before last </div> of <div class="navbar-header"> it may be found in your header.php if you use a shortcode like [yourslidershortcode] in your page content you have to modify it before post into direct theme header.php put <?php echo do_shortcode( '[yourslidershortcode]' ); ?>
I found the in the site you mentioned
<div class="navbar-header">
</div><!-- end of #logo -->
<?php echo do_shortcode( '[yourslidershortcode]' ); ?>
</div>
If the slider show only on home page use below code instead of above
<div class="navbar-header">
</div><!-- end of #logo -->
<?php
if ( is_home() ) {
echo do_shortcode( '[yourslidershortcode]' );
}
?>
</div>
I'm using a lightbox plugin the uses rel="lightbox" to fire the lightbox in a gallery I'm creating using the Advanced Custom Fields pluin. All works great until I want to add a child div that is absolutely positioned in the box div. It's supposed to change opacity on hover and still have the lightbox fire.
My markup
<?php if(get_field('image')):
$attachment_id = get_field('image');
$size = "full";
$image = wp_get_attachment_image_src( $attachment_id, $size );
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
?>
<?php echo wp_get_attachment_image( $attachment_id, medium); ?>
<div class="hover-title">
<?php the_field('hover_title');?>
</div><!-- hover title -->
<?php endif; ?>
</div><!-- box -->
If I simply remove the "hover-title" lightbox works. But I want to use that :)
my css:
.box a {
margin:5px;
float:left;
position: relative;
overflow: hidden;
opacity: 1;
display: block;
}
is that the full code. if so then you are missing an opening <div>.
fyi your first two lines should read
$attachment_id = get_field('image');
if ( $attachment_id ) {
}
no need to call get_field() twice, it is a database call after all.
Good Luck
I have just built my first WordPress theme, the only thing I can't seem to work out is how can I tell WordPress to make menu links with the class active when it is on the page? This is the code I had for the static page:
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
<?php if(curPageName() == 'index.php'){ ?>
<li class="current">
Home
</li>
<?php } else{ ?>
<li>
Home
</li>
<?php } ?>
I have still got this code in the WordPress theme, but it is not working at the moment, I am thinking I could probably make it work as it must have something to do with the exact links, but I'd also like the built the menu in WordPress so that would require me to get rid of this code..
Anyone know what I can do or link me to something that will help me out?
The best way is to assign a page name to the body tag of your page identifying it. Then use php to assign an active class if the page matches.
CSS
a.active:hover {
color:#000;
border:1px solid #000;
background:#fff;
}
HTML
<body <?php $page = "one" ?>>
<div id="navbar">
<ul>
<li><a<?php if($page == 'one'): ?> class="active"<? endif ?> href="one.php">Page 1</a></li>
<li><a<?php if($page == 'two'): ?> class="active" <? endif ?> href="two.php">Page 2</a></li>
<li><a<?php if($page == 'three'): ?> class="active" <? endif ?> href="three.php">Page 3</a></li>
<li><a<?php if($page == 'four'): ?> class="active"<? endif ?> href="four.php">Page 4</a></li>
</ul>
</div>
In the above example page one will be active