0

インストールしたプラグイン WordPress の人気のある投稿を変更するのに問題があります。

「image_facebook」として入力したカスタム フィールドからサムネイルを取得するオプションがあります。しかし、サムネイルは表示されていません。

コードを確認しているときに、img src が画像 URL を返すのではなく、投稿 ID を持っていることがわかりました。

"<img src="5438" width="135" height="135" alt="Alt text" border="0" />"

http://wordpress.org/plugins/advanced-custom-fields/をインストールした別のプラグインに問題を絞り込みました。アクティブな間は、「the_field ()」を使用してカスタム フィールドのコンテンツを取得する必要があります。通常のWordPressの "get_post_meta"

the_field() 関数を使用するには、WordPress の人気のある投稿ファイルで以下のコードを編集する必要があります。WordPress-popular-posts.php のコードには次のように書かれています:-

                    // POST THUMBNAIL
                if ($instance['thumbnail']['active'] && $this->thumb) {

                    $tbWidth = $instance['thumbnail']['width'];
                    $tbHeight = $instance['thumbnail']['height'];

                    $thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";

                    if ( $this->user_ops['tools']['thumbnail']['source'] == "custom_field" ) { // get image from custom field

                        $path = get_post_meta($p->id, $this->user_ops['tools']['thumbnail']['field'], true);

                        if ( $path != "" ) {

                            if ( $this->user_ops['tools']['thumbnail']['resize'] ) {

                                $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );

                            } else {
                                $thumb .= "<img src=\"{$path}\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"{$title}\" border=\"0\" />";
                            }

                        } else {
                            $thumb .= "<img src=\"". $this->default_thumbnail ."\" alt=\"{$title}\" border=\"0\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" />";
                        }

                    } else { // get image from post / Featured Image
                        $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
                    }

                    //$thumb .= "</a>";
                }

私のテーマ ファイルでは、次のコードを使用して画像の URL を取得できます。

<img src="<?php echo get_field('image_facebook'); ?>" alt="<?php the_title(); ?>" class="postImg" />

この関数を上記のプラグイン コードに入れるのを手伝ってください。

アップデート

以下のコードでは、画像 URL が取得されますが、人気のある 10 件の投稿すべてに対して同じ画像 URL が取得されます。

                    // POST THUMBNAIL
                if ($instance['thumbnail']['active'] && $this->thumb) {
                    $my_image = get_field('image_facebook');
                    $my_title = get_the_title();
                    $tbWidth = $instance['thumbnail']['width'];
                    $tbHeight = $instance['thumbnail']['height'];

                    $thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";

                    if ( $this->user_ops['tools']['thumbnail']['source'] == "custom_field" ) { // get image from custom field

                        $path = get_post_meta($p->id, $this->user_ops['tools']['thumbnail']['field'], true);

                        if ( $path != "" ) {

                            if ( $this->user_ops['tools']['thumbnail']['resize'] ) {

                                $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );

                            } else {
                                //$thumb .= "<img src=\"{$path}\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"{$title}\" border=\"0\" />";
                                $thumb .= "<img src=\"" . $my_image . "\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"" . $my_title . "\" border=\"0\" />";
                                }

                        } else {
                            $thumb .= "<img src=\"". $this->default_thumbnail ."\" alt=\"{$title}\" border=\"0\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" />";
                        }

                    } else { // get image from post / Featured Image
                        $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
                    }

                    //$thumb .= "</a>";
                }
4

1 に答える 1

0

<img src="5438" width="135" height="135" alt="Alt text" border="0" />

これが唯一の問題である場合は、ACF 画像フィールドが返す値を変更できます。現在はおそらく Image ID に設定されています。代わりに画像 URL に設定してみてください。ここを参照

それが役に立たない場合は、これを試してみます。あなたのプラグインが ACF とどのように相互作用するのか、私には理解できないことに注意してください。まず、変数を設定します:

$my_image = get_field('image_facebook');
$my_title = get_the_title();

$thumb .=次に、次のように、テストのために、すべてのインスタンスを機能している ACF コードに置き換えます。

$thumb .= "<img src=\"" . $my_image . "\" alt=\"" . $my_title . "\" class=\"postImg\" />";
于 2013-11-13T09:34:10.810 に答える