[英]Adding custom content to wordpress posts, with variables such as $title not showing
I have used this code to add custom content to all my wordpress posts. 我已使用此代码向所有wordpress帖子添加自定义内容。
function add_after_post_content($content) {
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$content .= '<strong>'. $title . '</strong> is a <strong>wallpaper</strong> posted in the ' . $cat_name . ' category.';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
The problem is that the post title and category are not showing so all I am getting is basically "is a wallpaper posted in the category". 问题是帖子标题和类别没有显示,所以我得到的基本上是“是类别中张贴的墙纸”。
How could I modify the code so that the post title and category are pulled to be added to the description? 如何修改代码,以便将帖子标题和类别添加到说明中?
Here is the code which works for some posts created using a specific plugin, but I would like it globalized to all posts on the site 这是适用于使用特定插件创建的某些帖子的代码,但我希望将其全球化到网站上的所有帖子
//Create Post
$user_id = get_current_user_id();
$imagePoster->dirName = time();
$wp_upload_dir = wp_upload_dir();
if(!empty($_FILES["file"]["tmp_name"]))
{
$filename = $_FILES["file"]["tmp_name"];
$originalFilename = $_FILES["file"]["name"];
$zipMessage = $imagePoster->unzip($filename , $originalFilename);
$images = $imagePoster->iterateDir($wp_upload_dir['basedir'].'/bulkimages-'.$imagePoster->dirName.'/');
}
else
{
$filename = $_POST['manualfile'];
$images = $imagePoster->iterateDir($wp_upload_dir['basedir'].'/'.$filename.'/');
$zipMessage = '';
}
$postCount = 0;
$titleExploded = explode(",", $titleList);
$linkExploded = explode(",", $linkList);
$initialInterval = $statusSplit[1];
$interval = $statusSplit[1];
foreach($images as $image)
{
if(get_option('create-posts-from-images-useimagename') == true)
{
if(get_option('create-posts-from-images-delimiter') != '')
{
$path_parts = pathinfo($image->getFilename());
$title = str_replace(get_option('create-posts-from-images-delimiter')," ",$path_parts['filename'] );
}
else
{
$path_parts = pathinfo($image->getFilename());
$title = $path_parts['filename'];
}
}
else
{
$title = $imagePoster->loopTitles($titleExploded, $postCount );
}
$link = $imagePoster->loopTitles($linkExploded, $postCount );
$cat_name = get_cat_name( $category );
$content = '<strong>'. $title . '</strong> is a <strong>Wallpaper</strong> posted in the ' . $cat_name . ' category.<br /><br />';
It's been a while since I worked in wordpress but try var dumping out the following in the shortcode. 自从我在wordpress中工作以来已经有一段时间了,但是尝试var在简码中转储以下内容。
$GLOBALS['post']
You should be able to get what you want doing something like... 您应该能够得到想要做的事情,例如...
$GLOBALS['post']->post_name
to test this in your code above do the following. 要在上面的代码中对此进行测试,请执行以下操作。
function add_after_post_content($content) {
global $post;
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$post_categories = wp_get_post_categories( $post->ID );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
foreach($cats as $c){
$content .= $c['name'];
}
$content .= 'categories';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.