2011-10-05 60 views
0

我想顯示的圖像(類coreimg)WordPress的效果上方(主頁和單)
coreimg尺寸:固定的(例如:680×240)
我知道我可以通過相對比較來做一些CSS技巧,但我想要一個更復雜的方法。我也嘗試get_first_image()方法westondeboer。但是,這個難點在於 -圖片標題以上:WordPress的

  • 無法獲取特定圖像(與類coreimg),它必須是第一個。
  • 它很難插入圖片自己的alttitle

所以我想要做的就是創建一個WP短代碼,這將是這樣的:

[coreimg SRC =「http://example.org/wp-content/uploads /2010/06/fly.jpg」標題= 「的風箏」]

或甚至下面的代碼就可以了:

<img src="http://example.org/wp-content/uploads/2010/06/fly.jpg" title="The flying kite" alt="The flying kite" class="coreimg"/> 

ù更新:問題只是通過php獲取圖像。而且我無法做到,所以我開始了,希望爲代碼啓動一個開始。
UPDATE2:
當前代碼single.php

<h1 class="entry-title"><?php the_title();?></h1><?php the_content(); ?> 

新代碼(應該是這樣):

<img src="<?php get_coreimg_url(); ?>" title="<?php get_coreimg_title(); ?>" alt="<?php get_coreimg_title(); ?>" class="headcoreimg"/> 
<h1 class="entry-title"><?php the_title();?></h1><?php the_content(); ?> 

現在在這裏我要get_coreimg_url()的和get_coreimg_title()的爲functions.php

代碼
+1

嗯,好的,你可以繼續... Seioursly,你的問題?你的解決方案不起作用?錯誤?目前還不是很清楚 –

+0

@DamienPirsy我已經更新了這個問題。 –

+0

目前還不清楚哪裏出了問題..我明白了,它不起作用,但是怎麼做?你沒有看到特定的輸出?你會得到錯誤?你得到錯誤的價值?你想要那些功能,或者他們已經在那裏,但沒有返回你想要的東西? –

回答

0

終於找到了解決辦法。 (:
謝謝大家的幫助..

所以基本上所有你需要做的是從你的single.php和/或index.php(默認主題刪除默認<h1 class="entry-title"><?php the_title();?></h1>

添加以下功能functions.php

function coreimg($atts) { 
    // Just add the below shortcode in the post. 
    // [cover src='http://localhost/wordpress/wp-content/uploads/2010/06/flyingkite.jpg' title='A flying Kite'] 
    global $post; 
    extract(shortcode_atts(array('src' => '', 'title' => ''), $atts)); 
    if($src == '') { 
    $src = get_bloginfo('template_directory') . '/rotate.php'; 
    } 
    if ($title == '') { 
    $title = "{$post->post_title}"; 
    } 
    $coverup = '<a href="'; 
    $coverup .= get_permalink($post->ID); 
    $coverup .= '" ' . "alt='{$title}' title='{$title}' class='corecover'><img src='"; 
    $coverup .= "{$src}' class='cocover' alt='{$title}' title='{$title}' /></a>"; 
    $before = '<h1 class="entry-title"><a href="' . get_permalink($post->ID) .'" title="' . "{$post->post_title}\">"; 
    $after = '</a></h1>'; 
    $output = "{$coverup}{$before}{$post->post_title}{$after}"; 

    return $output; 
} 
add_shortcode('cover', 'coreimg'); 

您可以從alistapartrandom.php文件。

它是做什麼的(特別)

如果默認情況下,您沒有爲該帖子指定任何特定圖片,它將隨機頁眉圖片旋轉,您可以在文件夾中上傳(並在random.php中指定) 。這個簡碼可能看起來像[cover title="The flying kite"]或只是[cover]。對於完整的網址短代碼,你可以輸入[cover src="../mykite.png"][cover src="../mykite.png" title="Hey, look a kite!"]

1

您可以修改get_first_image()中的代碼以查找標籤w是那個班,而不僅僅是第一張照片。你將不得不改變正則表達式來尋找class =「coreimg」或其他東西。

+0

獲取類'coreimg'的圖像所以我可以使用哪些正則表達式.. 。?對不起,但我是一個新手 –

+1

退房http://stackoverflow.com/questions/1989579/regular-expression-for-finding-class-names-in-html 也注意到關於使用正則表達式的HTML的警告。 – Jerry

+0

經過一些粗糙的研究 - 它使用正則表達式真的不安全?無論如何,但我只是離開這種方法。但非常感謝你的洞察力(: –