サポート » 使い方全般 » プラグインを使わずにCategoryページで単一記事の投稿内にある外部画像のUR

  • 解決済 bc

    (@bcures)


    WordPressをご支援されている皆様。
    お世話になっております。

    投稿記事(single.php)の中に表示させている外部画像(直リンク専用のバナー等)を、category.phpやtag.php等で、一覧表示させる際に、画像をサムネイル化してリンクとシングルページへのリンクと一緒にリスト表示したいと考えております。

    http://ja.forums.wordpress.org/topic/3134?replies=12
    http://ja.forums.wordpress.org/topic/4806?replies=10
    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
    http://ja.forums.wordpress.org/topic/4827?replies=5

    <?php foreach($posts as $post): setup_postdata($post); ?>
    
    	<h3>
    	<?php
    		$dat = get_posts('post_type=attachment&orderby=menu_order&order=ASC&numberposts=1&post_parent='.get_the_ID());
    		if (count($dat)> 0) {
    		$image = wp_get_attachment_image_src($dat[0]->ID);
    		$desc = '<img src="'.$image[0].'" width="100px" height="100px" alt="" /></a>';
    		// 記事出力
    		echo '<a href="' . get_permalink() . '" title="' . the_title('', '', false) . '">';
    		echo $desc;} ?>
    
    	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>&nbsp;::&nbsp;<?php the_time('Y年n月'); ?></h3>
    
    	<?php endforeach; ?>

    上記のサイトを参考に、カスタマイズしているのですが、ページが真っ白になったり、HTMLに<img>コードがうまく挿入できなかったりしております。

    単一記事に貼っている画像は、WordPressの管理画面にある「メディア」から登録したものではなく、直リンク専用のバナーや、別サーバーにある画像を直接「絶対パス」形式で呼び出しています。

    なるべくCustom Field Template pluginなどのプラグインを使わないで、投稿記事内にある外部画像のURLを取得し、カテゴリーページ等でリスト表示させたいと考えております。

    loop-category.phpを新規に作成し、ソースコードは下記のようになっております。(一部抜粋)

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h3 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
    <div class="thumbnail-image">
    
    ●●ここに外部URLの画像のサムネイルを表示したいと考えています。
    
    </div>
    <?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
    <div class="entry-summary">
    <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
    <?php endif; ?>
    
    <div class="entry-utility">
    <?php if ( count( get_the_category() ) ) : ?>
    							<p class="entry-content-category"><?php printf( __( 'Category :  %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    </p>							<?php endif; ?>
    <?php
    					$tags_list = get_the_tag_list( '', ', ' );
    					if ( $tags_list ):
    				?>
    							<p class="entry-content-tag"><?php printf( __( 'Tag :  %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
    </p>							<div class="entry-meta">
    Date : <?php the_time('Y/m/d H:i:s', '', ''); ?>
    </div><!-- .entry-meta -->
    
    <?php endif; ?>

    どなたか、解決策をご存じでしたら、ご伝授頂けますでしょうか。
    御忙しい中、たいへんお手数ですが、どうぞよろしくお願い申し上げます。

    テーマは、 Twentytenを使用し、カスタマイズしております。
    サーバーは、さくらのレンタルサーバー。
    WordPressは最新バージョンを使用しております。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • attachment をいくら探ってもダメじゃないですかね?
    この場合、正規表現などで、<img ~~ /> を拾うしか無いような気がします。

    また、投稿に規則性が無いとロジックがそうとう複雑かも・・・
    かならず画像は一つしかない、または複数あったとしても最初の1つのみ対象とする、またはいくつあっても、全てを取得するなど。

    こんにちは、

    プラグインを使わないで、投稿記事内にある外部画像のURLを取得し、カテゴリーページ等でリスト表示させたいと考えております。

    WordPressの質問というよりPHPの質問だと思います、PHPの課題として検索すると答えに近づきやすいかもしれません

    例えば、http://www.phppro.jp/qa/2376#9555 は、外部のhtmlをスクレイピングするという話ですが、the_content等から画像を取得するヒントになるのではないかと思います。

    トピック投稿者 bc

    (@bcures)

    kvexさん、nobitaさんのご意見を参考にしつつ、いろいろなサイトを見て回っておりましたが、結果、無事、サムネイルを表示することができました。

    参考にさせて頂いたサイトのURLは、
    http://www.route315.jp/2009/07/wordpress-custom-field-photo-entry.html
    です。

    fanctions.phpに、

    function catch_that_image() {
    
        global $post, $posts;
        $first_img = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    
        $first_img = $matches [1] [0];
    
        if(empty($first_img)){ //Defines a default image
            $first_img = "/images/default.jpg";
        }
        return $first_img;
    }

    と記述し、サムネイルを表示させたい場所に

    <img src="<?php echo catch_that_image(); ?>" />

    と記述致しました。

    これで、アーカイブのページも、新着情報を表示するトップページのサイドバーにおいても、無事、投稿の中にある画像のサムネイルを表示させることができるようになりました。

    試行錯誤していたため、お礼のコメントをするまでにお時間がかかりましたこと、お詫び致します。
    貴重なお時間を割き、適切なアドバイスを頂きまして、本当にありがとうございました。
    また、phpのことで、
    http://www.phppro.jp/qa/2376#9555
    のサイトは非常に参考になります。
    ブックマークに追加させて頂きました。
    また、なにかカスタマイズが必要になると思った際には、上記のサイトも含め、参考にさせて頂きます。

    ありがとうございました。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「プラグインを使わずにCategoryページで単一記事の投稿内にある外部画像のUR」には新たに返信することはできません。