What happens is that the active link is removed on refresh, what should I do so on refresh it checks which link is on and adds the class? I believe this should be done with js, any tips are helpful thanks in advance.
I have this setup for links in my php file:
<?php if (!is_page('main')){ ?>
<div class="shop-categories layoutcolor1">
<li>
<a href="<?php echo get_term_link( 21, 'product_cat' ); ?
>">
<p class="first"><?php echo get_cat_name( 21 ); ?> </p>
</a>
<a href="<?php echo get_term_link( 20, 'product_cat' ); ?
>">
<p class="second"><?php echo get_cat_name( 20 ); ?></p>
</a>
<a href="<?php echo get_term_link( 18, 'product_cat' ); ?
>">
<p class="third"><?php echo get_cat_name( 18 ); ?></p>
</a>
</li>
</div>
<?php } ?>
And I have js:
$('.shop-categories li a').click(function() {
$('a').removeClass('active');
$(this).addClass('active');
});
fixed with:
$('.shop-categories li a').filter(function() {
return $(this).prop('href') === browserUrl;
}).addClass('active');
Try this:
var cp_url= window.location.pathname;
$('.shop-categories li').find('a').each(function() {
$(this).toggleClass('active', $(this).attr('href') == cp_url);
})
I think you need this...
$('.shop-categories li a').click(function() {
$(this).addClass('active').siblings('a').removeClass('active');
});
Related
with wordpress i installed magnific popup, how can I view the popup directly, without the preview of the images? in the work I am doing I need that when I click on an image, the popup opens immediately! I have tried in a thousand ways but I cannot find the most suitable solution
I apologize for the delay, I have tried two methods, one is tied directly to the plugin so the code I have to make it work is the following:
(function ($) {
$('.gallery').each(function() {
$(this).magnificPopup({
delegate: 'a',
type: 'image',
gallery: {
enabled:true
}
});
});
}(jQuery));
it works but as previously said I would like you to be able to view the popup without the preview of the gallery!
the second option i used genesis custom block, the system works but can't find the images here is the code:
(function ($) {
$('a.btn-gallery').on('click', function(event) {
event.preventDefault();
var gallery = $(this).attr('href');
$(gallery).magnificPopup({
delegate: 'a',
type:'image',
gallery: {
enabled: true
}
}).magnificPopup('open');
});
}(jQuery));
.btn-gallery img{max-width: 100%;height: auto;padding: 30px;}
.hidden{overflow: hidden;display: none;visibility: hidden;}
<div class=" block-gallery <?php block_field('className'); ?>">
<div class="col-100 grid grid--center">
<div class="col-30">
<a href="#gallery-1" class="btn-gallery">
<?php $attachment_id = block_value( 'image-preview-1'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
</a>
</div>
</div>
<div id="gallery-1" class="hidden">
<a href="
<?php $attachment_id = block_value( 'image-1'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
"></a>
<a href="
<?php $attachment_id = block_value( 'image-2'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
"></a>
<a href="
<?php $attachment_id = block_value( 'image-3'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
"></a>
<a href="
<?php $attachment_id = block_value( 'image-4'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
"></a>
<a href="
<?php $attachment_id = block_value( 'image-5'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
"></a>
<a href="
<?php $attachment_id = block_value( 'image-6'); ?>
<?php echo wp_get_attachment_image( $attachment_id, 'New_quad' );?>
"></a>
</div>
</div>
this is the function that calls the block on wordpress:
<?php
use function Genesis\CustomBlocks\add_block;
use function Genesis\CustomBlocks\add_field;
/* gallery */
function register_gallery_block() {
add_block( 'gallery', array( 'icon' => 'waves' ) );
add_field( 'gallery', 'image-preview-1', array(
'control' => 'image'
) );
//gallery-1
add_field( 'gallery', 'image-1', array(
'control' => 'image'
) );
add_field( 'gallery', 'image-2', array(
'control' => 'image'
) );
add_field( 'gallery', 'image-3', array(
'control' => 'image'
) );
add_field( 'gallery', 'image-4', array(
'control' => 'image'
) );
add_field( 'gallery', 'image-5', array(
'control' => 'image'
) );
add_field( 'gallery', 'image-6', array(
'control' => 'image'
) );
add_action( 'genesis_custom_blocks_add_blocks', 'register_gallery_block' );
I got a a custom loop into my Wordpress custom blog page, and Im displaying all my posts into it, like that :
<section class="container blog-article-actu">
<div class="row">
<?php
$the_query = new WP_Query('showposts=-1');
while ($the_query->have_posts()) :
$the_query->the_post();
$catObj = get_the_category();
?>
<article class="blog-article-actu-article" style="background:url('<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?><?php echo $url ?>');">
<div class="blog-article-actu-article-top">
<h2><?php the_title(); ?></h2>
<div class="details-blog-article-actu">
<div class="blog-article-actu-date">
<span class="day"><?php the_time( 'd' ) ?></span><br>
<span class="month"><?php the_time( 'F' ) ?></span>
</div>
<a href="#" target="_blank"><p>
<?php
if (($catObj[0]->name == 'Actualités et évènements') OR ($catObj[1]->name == 'Actualités et évènements')) {
echo "<img src=\"XXX/actu-icon.png\" alt=\"Actualités et évènements\">";
} elseif (($catObj[0]->name == 'Témoignages') OR ($catObj[1]->name == 'Témoignages')) {
echo "<img src=\"XXX/chat-icon.png\" alt=\"Témoignages\">";
} elseif (($catObj[0]->name == 'Vidéos') OR ($catObj[1]->name == 'Vidéos')) {
echo "<img src=\"XXX/video-icon.png\" alt=\"Vidéos\">";
} else {
echo "";
}
?>
</p></a>
</div>
</div>
<div class="blog-article-actu-article-bottom-wrap">
<span class="blog-article-actu-article-excerpt"><?php the_excerpt();?></span>
<span class="blog-article-actu-article-bottom">En savoir <span>+</span></span>
<div class="social-blog-article-actu">
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-facebook" aria-hidden="true"></i>
</div>
</div>
</article>
<?php // End of the loop.
endwhile;
?>
</div>
</section>
Then I was asked to build something to sort them, by tags, dynamically, with a tag system like this one : https://codepen.io/Chaaampy/pen/gWOrvp
But Im a bit lost about that ... I thought about get the tags for each posts in my loop, and then add them as a CSS class, like that maybe I could show or hide some articles in JS ... Meh, I don't know, has someone got any ideas for me ?
Thanks ! :)
If anyone is interested, found a solution by building something like that : https://codepen.io/Chaaampy/pen/gWOrvp
$(function filter() {
var selectedClass = "";
$(".link").click(function(){
selectedClass = $(this).attr("data-rel");
$(".wrap").fadeTo(100, 0.1);
$(".wrap section").not("."+selectedClass).fadeOut().removeClass('scale_anm');
setTimeout(function() {
$("."+selectedClass).fadeIn().addClass('scale_anm');
$(".wrap").fadeTo(300, 1);
}, 300);
});
});
Note that I get the tags into Wordpress with get_the_tags, and then I add them as a class to the elements I want to show / hide
So on my wordpress blog and most other wordpress blogs, you can set a featured image. If you don't set a featured image I have it default to a background image that says "new update" however the default background image i have set is much less eye grabbing than the custom ones made for the post.
To solve the issue of posts with custom featured images getting more attention that those that have the default image - I would like to make it so all blog post titles with posts that do not have a featured image to change their color code.
For example something like..
My pseudo code: - I'm don't know jquery/javascript very well but I could probably figure out the basic javascript to make it work.
IF
('post > featured-image') = 'NONE';
THEN ('.post-list h1.entry-title a') = 'RED';
ELSE ('.post-list h1.entry-title a') = 'DEFAULT';
But that being said how would i even reference the POST not having a FEATURED IMAGE in wordpress via jquery or some function? I am open to any solutions!
Thanks so much for any help!
From the code you supplied your theme is already checking for the presence of a thumbnail on your posts using the wordpress function has_post_thumbnail. We can leverage this existing if:else statement to change a string value that we will apply to our h1 tags. You can find the string value under the variable $header_class_name.
First we default the value to "has-thumbnail" (Line 18)
Then we override the default to "no-thumbnail" in the event that the post has no thumbnail (Line 33)
Lastly, we apply the class to the h1 tag (Line 72)
PHP CODE
<?php
/**
* The template part for displaying content.
*
* #package azera-shop
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( apply_filters( 'azera_shop_content_post_class_filter','border-bottom-hover' ) ); ?> itemtype="http://schema.org/BlogPosting" itemtype="http://schema.org/BlogPosting">
<header class="entry-header">
<div class="post-img-wrap">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php
// default the $header_class_name to 'has-thumbnail'
$header_class_name = 'has-thumbnail';
if ( has_post_thumbnail() ) {
?>
<?php
$image_id = get_post_thumbnail_id();
$image_url_big = wp_get_attachment_image_src( $image_id,'azera-shop-post-thumbnail-big', true );
$image_url_mobile = wp_get_attachment_image_src( $image_id,'azera-shop-post-thumbnail-mobile', true );
?>
<picture itemscope itemprop="image">
<source media="(max-width: 600px)" srcset="<?php echo esc_url( $image_url_mobile[0] ); ?>">
<img src="<?php echo esc_url( $image_url_big[0] ); ?>" alt="<?php the_title_attribute(); ?>">
</picture>
<?php
} else {
// override the default $header_class_name in the case that there is no thumbnail
$header_class_name = 'no-thumbnail';
?>
<picture itemscope itemprop="image">
<source media="(max-width: 600px)" srcset="<?php echo azera_shop_get_file( '/images/no-thumbnail-mobile.jpg' );?> ">
<img src="<?php echo azera_shop_get_file( '/images/no-thumbnail.jpg' ); ?>" alt="<?php the_title_attribute(); ?>">
</picture>
<?php } ?>
</a>
<?php azera_shop_post_date_index_box_trigger(); ?>
</div>
<div class="entry-meta list-post-entry-meta">
<?php azera_shop_content_entry_meta_top_trigger(); ?>
<span itemscope itemprop="author" itemtype="http://schema.org/Person" class="entry-author post-author">
<span itemprop="name" class="entry-author author vcard">
<i class="fa fa-user" aria-hidden="true"></i><a itemprop="url" class="url fn n" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" rel="author"><?php the_author(); ?> </a>
</span>
</span>
<span class="posted-in entry-terms-categories">
<i class="fa fa-folder-open-o" aria-hidden="true"></i><?php _e( 'Posted in','azera-shop' ); ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( esc_html__( ', ', 'azera-shop' ) );
$pos = strpos( $categories_list, ',' );
if ( $pos ) {
echo substr( $categories_list, 0, $pos );
} else {
echo $categories_list;
}
?>
</span>
<a href="<?php comments_link(); ?>" class="post-comments">
<i class="fa fa-comment-o" aria-hidden="true"></i><?php comments_number( esc_html__( 'No comments','azera-shop' ), esc_html__( 'One comment','azera-shop' ), esc_html__( '% comments','azera-shop' ) ); ?>
</a>
</div><!-- .entry-meta -->
<?php
// add the $header_class_name value to the h1 (PS consider using a similarly styled h2)
the_title( sprintf( '<h1 class="entry-title '.$header_class_name.'" itemprop="headline">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php echo apply_filters( 'azera_shop_header_underline','<div class="colored-line-left"></div><div class="clearfix"></div>' ); ?>
</header><!-- .entry-header -->
<div itemprop="description" class="entry-content entry-summary">
<?php
$ismore = strpos( $post->post_content, '<!--more-->' );
if ( $ismore ) : the_content( sprintf( esc_html__( 'Read more %s …','azera-shop' ), '<span class="screen-reader-text">' . esc_html__( 'about ', 'azera-shop' ) . esc_html( get_the_title() ) . '</span>' ) );
else : the_excerpt();
endif;
?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'azera-shop' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
CSS
article header h1.no-thumbnail{
color:red;
}
You can then apply CSS by referencing either class name
on your single.php file, or the page you use to display the single blog post you need to to an if statement check if the post have a thumbnail then if it does printout the style, or else let the default style will take place
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
echo "<style type=\"text/css\">";
echo ".post-list h1.entry-title a{
color: red !important ; /*any color of your choice*/;
}
</style>";
}
else {
// Default style will take place
}
?>
Make sure that you add thumbnail support on your functions.php file.
To add thumbnail support on your functions.php just add add_theme_support( 'post-thumbnails' );
Replace this line
<header class="entry-header">
with
<header class="entry-header <?= has_post_thumbnail() ? 'my-hasfeatured-img' : '' ?>">
and then add CSS rule in style.css like:
.my-hasfeatured-img h1{
//your code
}
Hope this helps!
I want to apply a js I found here on stackoverflow to my navigation bar. This is aimed to autohide it after a moment of inactivity. But I dont know how to include and apply the code to my navigation bar.
This is how my html goes
<div class="basic-wrapper">
<a class="btn responsive-menu pull-right" data-toggle="collapse" data-target=".navbar-collapse">
<i class='icon-menu-1'></i>
</a>
<a class="navbar-brand" href="<?php echo home_url(); ?>">
<?php
if( get_option('custom_logo') ){
echo '<img src="'. get_option('custom_logo') .'" alt="" data-src="'. get_option('custom_logo') .'" data-ret="'. get_option('custom_logo_retina') .'" class="retina" />';
} else {
echo bloginfo('title');
echo '<br /><small>';
echo bloginfo('description');
echo '</small>';
}
?>
</a>
</div>
<div class="collapse navbar-collapse pull-right">
<?php
if ( has_nav_menu( 'primary' ) ){
wp_nav_menu(
array(
'theme_location' => 'primary',
'depth' => 3,
'container' => false,
'container_class' => false,
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'menu_id' => 'ebor-main-menu',
'walker' => new ebor_bootstrap_navwalker()
)
);
} else {
echo 'Set up a navigation menu now';
}
if( get_option("header_social_link_1") )
get_template_part('loop/loop','socialheader');
?>
</div>
</div><!--/.container-->
</div><!--/.nav-collapse -->
This is the code I found-
$("div").hide();
$("html").mousemove(function( event ) {
$("div").show();
myStopFunction();
myFunction();
});
function myFunction() {
myVar = setTimeout(function(){
$("div").hide();
}, 1000);
}
function myStopFunction() {
if(typeof myVar != 'undefined'){
clearTimeout(myVar);
}
}
If you are asign how to include an external javascript file, you need to do the following:
<script src="//path/to/external/js/file.js"></script>
As for the actual JS code, i dont think that will give you what you are looking for, as it will hide ALL DIVs on the page.
You'd want to replace that with something like. $("div#IDofMenuContatiner")...
How can I call this into body from header, I've tried all the ways on the internet and it just simply doesn't work, am I missing something obvious? How would you go about this?
<script type="text/javascript" src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.js"></script>
<script type="text/javascript" src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.stickem.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.container').stickem();
});
</script>
I should probably add that I have multiple instances of this running as I am applying the JavaScript inside content.php on WordPress. Is that the problem?
Here is my content.php file contents:
<div class="title-wrapper">
<?php the_title(); ?>
<a href="<?php the_permalink(); ?>#comments" title="<?php comments_number( 'No Comments', '1 Comment', '% Comments' ); ?>">
<div class="commentnumber"><?php comments_number( '0', '1', '%' ); ?></div>
</a>
</div>
<div class="container">
<div class="stickem-container">
<div class="thelinks stickem">
<div class="sharelinks">
<div class="sharepinterest">
<?php echo get_simple_local_avatar( $id_or_email, $size, $default, $alt ); ?>
</div>
<a href="http://www.facebook.com/sharer/sharer.php?s=100&p[url]=<?php the_permalink(); ?>&p[images][0]=http://www.otlcampaign.org/sites/default/files/journey-for-justice-mlk-memorial.jpg&p[title]=<?php the_title(); ?>&p[summary]=Click+to+enlarge">
<div class="sharefacebook"></div>
</a>
<a href="http://twitter.com/home?status=<?php the_title(); ?>+<?php the_permalink(); ?>">
<div class="sharetwitter"></div>
</a>
<div class="sharegoogle"></div>
</div>
</div>
<div class="post-wrapper">
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
</div>
</div><a>
Scripts should be enqueued with wp_enqueue_scripts in functions.php, not directly in other theme template files. Also, looks like the theme has jQuery bundled and that's doing_it_wrong()™.
Any Conditional Tag can be used to filter the enqueue in different pages.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_so_18774457' ) );
function enqueue_so_18774457()
{
if( is_single() )
{
wp_enqueue_script(
'stickem-js',
get_stylesheet_directory_uri() . '/assets/css/jstick/jquery.stickem.js',
array( 'jquery' ) // This enqueues jQuery as a dependency
);
}
}
And for small scripts, like $('.container').stickem();, this can be used:
add_action( 'wp_footer', 'footer_so_18774457' );
function footer_so_18774457()
{
if( !is_single() )
return;
echo "
<script type='text/javascript'>
jQuery(document).ready(function($) { $('.container').stickem(); });
</script>";
}