簡體   English   中英

將自定義內容添加到wordpress帖子中,但未顯示$ title之類的變量

[英]Adding custom content to wordpress posts, with variables such as $title not showing

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

問題是帖子標題和類別沒有顯示,所以我得到的基本上是“是類別中張貼的牆紙”。

如何修改代碼,以便將帖子標題和類別添加到說明中?

這是適用於使用特定插件創建的某些帖子的代碼,但我希望將其全球化到網站上的所有帖子

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

自從我在wordpress中工作以來已經有一段時間了,但是嘗試var在簡碼中轉儲以下內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM