首页 文章

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

提问于
浏览
1

我已经使用此代码将自定义内容添加到我的所有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');

问题是帖子 Headers 和类别没有显示所以我得到的基本上“是在类别中发布的壁纸” .

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

这是适用于使用特定插件创建的一些帖子的代码,但我希望它全球化到网站上的所有帖子

//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.

';

1 回答

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

相关问题