如何使用Wordpress从Post Link获取精选图片? [英] How to get Featured image from the Post Link using Wordpress?

查看:104
本文介绍了如何使用Wordpress从Post Link获取精选图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的wordpress项目中,我有一组内页链接。所有这些页面都有特色图片。这些指向内页的链接是使用wordpress菜单功能生成的。

In my wordpress project, I have set of links for inner pages. And all these pages have featured images. These links to inner pages are generated using wordpress menu feature.

我想要做的是,当有人点击这些链接,然后不加载整个页面并使用Ajax调用时,我想获取该页面的特色图像在该URL上并在同一页面上显示该图像。我已经使用jQuery创建了一个代码,用于将URL读入变量,以便我们可以某种方式使用它们。

What I want to do is, when someone clicks on these links, then without loading the full page and by using Ajax call, I want to fetch featured image of that Page on that URL and display that image on same page. I have already created a code using jQuery to read the URLs into a variable, so that we can use them some way.

推荐答案

假设您知道如何运行AJAX调用,您将需要.php文件输出特色图像的路径。 Wordpress内置函数来检索特色图像,但棘手的部分是能够通过AJAX使用这些函数。

Assuming you know how to run the AJAX call, you'll want your .php file to output the path to the featured image. Wordpress has built in functions to retrieve the featured image, but the tricky part is being able to use those functions via AJAX.

你会希望你的php文件看起来像什么像这样

You'll want your php file to look something like this

<?php//get-image.php 
//file that gets called from $.post(), $.get(), or $.ajax() function
//first load up wordpress
require_once('../../../../wp-load.php');//assuming get-image.php resides in /themes/yourtheme/ajax/
//get the post id
$post_id = url_to_postid($url_sent_from_ajax);
//now get the featured image
$image_url = get_the_post_thumbnail($post_id , 'post-thumbnail' );
echo $image_url;
?>

然后你的ajax返回功能可以用图像URL做它想要的东西

Then your ajax return function can do what it wants with image URL

希望这有助于

这篇关于如何使用Wordpress从Post Link获取精选图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆