本文介绍了使用不同的模板在Div中加载Wordpress Post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有图片库的页面.每个图像都被链接以在画廊上方的div中打开一个wordpress帖子.我已经在工作,但是它会加载整个页面,包括页眉和页脚.

I have a page with an image gallery. Each image is linked to open a wordpress post in a div above the gallery. I have that working, but it loads the whole page including header and footer.

我需要使用自定义页面模板来仅显示我需要的页面部分.例如,从div中加载的页面中删除页眉和页脚.我知道如何创建模板,但无法弄清楚如何在div中获取帖子以使用该页面模板.

What I need is to use a custom page template to display only the parts of the page I need. Like remove the header and footer for example from the page that loads in the div. I know how to create a template, but can't figure out how to get the post in the div to use that page template.

这是我的设置方式.

标头中的脚本:

<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){
    $.ajaxSetup({cache:false});
    $("a.ajax").click(function(){
        var post_url = $(this).attr("href");
        $("#Div-That-Loads-Post").html("loading...");
        $("#Div-That-Loads-Post").load(post_url);
     return false;
    });
});
</script>

图库链接:

<div class="ngg-gallery-thumbnail" >

<a href="../<?php echo $image->ngg_custom_fields["Link"]; ?>"

class="ajax"

title="<?php echo $image->description ?>"

<?php echo $image->thumbcode ?>  >

<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image-> thumbnailURL ?>" <?php echo $image->size ?> />

</a>

</div>

该页面上帖子/画廊上方的div:

Div above Post/Gallery on that page:

<div id="Div-That-Loads-Post">

</div>

就像我说的那样,它会将页面加载到div中.如何指定在div中加载的模板?我无法更改原始帖子模板,因为我希望该模板在其原始页面上保持原样.

Like I said it loads the page in the div. How can I go about specifying the template it loads in the div? I can't change the original post template because I want that to stay intact on its original page.

对于是否有更好的方法来实现相同的体验也很感兴趣.

Also interested if there is any better way to go about achieving this same experience.

任何帮助表示赞赏.谢谢.

Any help appreciated. Thanks.

推荐答案

评论帮助我走上了正确的道路,但必须解决其他一些问题才能完成.谢谢您的帮助.如果有人需要以这种方式将url更改为id,我就是这样做的.

Comment helped me get on the right track but had to solve a few other problems to accomplish. Thanks for the help. This is how I did it if anyone needs to change url to id in this way.

图片链接:

-class告诉脚本,这是链接.您将把所有的帖子都放在href上.

-class tells script this is the link. You will put the slug of post for href.

<a href="SlugOfPostGoesHere" class="ajax" </a>

在同一页面上划分,该div将从脚本加载自定义php页面:

Div on same page, this div will load custom php page from script:

<div id="tabs">

</div>

标题中的脚本:

-从href中拉出子弹并创建变量.(在这种情况下为postslug)

-Pulls slug from href and creates variable.(postslug in this case)

-在加载时在div #tabs中加载正在加载...".

-Loads "loading..." in div #tabs while loading.

-在同一页面的div #tabs中加载自定义php页面,并将变量附加到url.(在浏览器中看不到)

-Loads custom php page in the div #tabs on that same page with the variable appended to url.(you won't see this in browser)

-滚动到页面顶部.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){
    $.ajaxSetup({cache:false});
    $("a.ajax").click(function(){
        var postslug = $(this).attr("href")
        $("#tabs").html("loading...");
        $("#tabs").load("../custom-page.php?slugid=" + postslug);
        $("html, body").animate({ scrollTop: 0 }, 600);
        return false;
    });
});
</script>

然后在您的自定义PHP页面上,您需要输入:

And then on your custom PHP Page you need to put:

<?php require( "wp-load.php" ); ?>

(或该wordpress页面的任何路径,以便您可以使用wp函数)

(or whatever the path is to that wordpress page so you can use wp functions)

此代码还可以从您在URL中传递的段中获取帖子的ID:

Also this code to get the ID of the post from the slug that you passed in the URL:

-第一行从url中的变量slugid获取帖子,并将其设置为变量$ slug.

-First Line gets post slug from variable slugid in url and sets it to variable $slug.

-第二行从子弹中创建完整的URL.

-Second line creates full URL from slug.

-第三行为您提供了来自url的帖子ID.

-Third line gives you post ID from url.

<?php
$slug = $_GET['slugid'];
$url = ("http://www.yourwebsite.com/" .$slug);
$postid = url_to_postid($url);
?>

现在,您可以对页面进行编码,以使用帖子ID将所需的内容加载到div中.

Now you can code page to use post ID to load the content you want in the div.

这篇关于使用不同的模板在Div中加载Wordpress Post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:31
查看更多