将自定义内容添加到wordpress帖子,其中包含$ title等变量

时间:2014-09-24 01:32:07

标签: php wordpress

我已使用此代码向我的所有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');

问题是帖子标题和类别没有显示所以我得到的基本上是&#34;是在类别中发布的壁纸&#34;。

如何修改代码以便将帖子标题和类别添加到说明中?

以下是适用于使用特定插件创建的某些帖子的代码,但我希望将其全球化为网站上的所有帖子

        //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 />';                     

1 个答案:

答案 0 :(得分:1)

自从我在wordpress工作以来,已经有一段时间了,但尝试在短代码中删除以下内容。

$GLOBALS['post']

你应该能够得到你想做的事......

$GLOBALS['post']->post_name

在上面的代码中对此进行测试,请执行以下操作。

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');