K
K
K. A.2016-12-21 09:57:10
Programming
K. A., 2016-12-21 09:57:10

How to correctly implement links to posts in a modal window?

I'm pulling a template on WP, I ran into such a problem: the fact is that all posts open in a modal window (I use Magnific Popup). Accordingly, if I want to send a link to a specific post, nothing will come of it. in the address bar I have the root address of the site. Temporarily solved the problem using JX: I
set the parameter to links to posts rel="<?php $post->ID; ?>", and added the following code to the JS file:

$('.grid_item a').click(function(){
        location.hash = $(this).attr('rel');
});
var required_uri = location.href;
var post_id = required_uri.split('#')[1]; 
if(empty(post_id) == false)
{
    $.magnificPopup.open({
        items: {
                src: '/?p='+post_id,
                type: 'ajax'
        },
        mainClass: 'mfp-fade', 
        gallery:{
            enabled:true
        }
    });
}

But it's not exactly the right decision. I partially solved the problem - a function that pushes the address into the address bar without reloading:
function setLocation(curLoc){
    try {
      history.pushState(null, null, curLoc);
      return;
    } catch(e) {}
    location.hash = '#' + curLoc;
}

Doesn't quite work correctly in older browsers, but with that later. Now the problem is this: if I use this function, the real address of the record is substituted. But if I refresh the page, then WP will load it not in the modal window, but on the page according to the single.php template, and it has the following. view:
<?php 
$thumbnail_attributes = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
?>
<div class="single_post">
  <div class="post_image">
    <img src="<? echo $thumbnail_attributes[0]; ?>" />
  </div>
  <div class="post_header">
    <h1><?php the_title(); ?></h1>
  </div>
  <div class="post_text">
    <?php while (have_posts()): the_post();?>
    <?php the_content();?>
  </div>
    <?php
      if ( comments_open() || get_comments_number() ) : ?>
        <div class="post_comment">
          <?php comments_template(); ?>
        </div>
      <?php endif; ?>
    ?>
  <?php endwhile; ?>
</div>

It is no coincidence that there are no get_header()and get_footer()- I don’t need styles and JS in the modal window again, and if I leave them, then I have a layering of window opening events ...
The question is: how do I get all requests for posts (site.ru/ 2016/12/21/post-tittle/) open in a modal window on the main page? In which direction to dig mana?)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question