4

これは私を絶対に狂わせています。私はCSSの経験があまりないので、CSSが単純なものであることを望んでいます。

「TheMorningAfter」をテーマにしたWordpress2.9.2を実行しています。

ページの中央にキャプションを並べて中央に配置した2枚の小さな写真を表示したい投稿を書き込もうとしています。

画像を表示するために使用しているHTMLコードは次のとおりです。

[caption align="alignnone" width="150" caption="Protein rest"]
<a href="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest.jpg">
<img title="Mash during protein rest" src="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest-150x144.jpg" alt="Mash during protein rest" width="150" height="144" />
</a>[/caption]
[caption align="alignnone" width="143" caption="Saccharification rest" captionalign="center"]
<a href="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest.jpg">
<img  title="Mash during saccharification rest" src="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest-143x150.jpg" alt="Mash during saccharification rest" width="143" height="150" />
</a>[/caption]

キャプションの整列に「aligncenter」と「alignleft」を使用してみました。「alignleft」を使用すると、画像は完全に整列しますが、ページの左端まで表示されます。「aligncenter」を使用すると、写真は中央に表示されますが、積み重ねられています。

私の最初の考えは、以下を使用して画像をdivでラップすることでした。

<div style="text-align:center;">image code</div>

しかし、それは機能しません。さて、そのように中央に配置されたdivでラップし、[caption]タグを省略すると、機能しますが、キャプションが必要です。これらのキャプションタグは、Wordpressによってクラスwp-captionの独自のdivに変換されます。また、親を中心としたdivラッパー内の独自のdivで各個別の画像をラップしてみました。

style.cssの関連部分は次のとおりです。他に情報が必要な場合はお知らせください。サポートが必要な場合は、最寄りの橋からのジャンプを延期します。

ありがとう!!

Style.css:

.aligncenter, div.aligncenter { display: block; margin: 14px auto; }
.alignleft { float: left; margin: 0 14px 10px 0; }
.alignright { float: right; margin: 0 0 10px 14px; }
.wp-caption { border: 1px solid #ddd; text-align: center; background-color: #f3f3f3; padding-top: 4px; /* optional rounded corners for browsers that support it */ -moz-border-radius: 3px; -khtml-border-radius: 3px;  -webkit-border-radius: 3px; border-radius: 3px; }
.wp-caption img { margin: 0; padding: 0; border: 0 none; }
.wp-caption p.wp-caption-text { font-size: 11px; line-height: 14px; padding: 5px 4px 5px 5px; margin: 0; }

PS-Wordpressで利用できるギャラリー機能を知っていますが、それを避けたいので、divでラップしてもキット全体が中央に移動しない理由を理解したいと思います。

最後に、完全を期すために、上記のdivラッパーと画像コードを使用してロードしたときのページのソースを次に示します(Wordpressがキャプションタグをどのように変換するかを確認できます)。

<div style="text-align:center;">
<div class="wp-caption alignnone" style="width: 160px">
<a href="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest.jpg">
<img title="Mash during protein rest" src="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest-150x144.jpg" alt="Mash during protein rest" width="150" height="144" />
</a>
<p class="wp-caption-text" style="text-align:center">Protein rest</p>
</div>
<div class="wp-caption alignnone" style="width: 153px">
<a href="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest.jpg">
<img  title="Mash during saccharification rest" src="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest-143x150.jpg" alt="Mash during saccharification rest" width="143" height="150" />
</a>
<p class="wp-caption-text" style="text-align:center">Saccharification rest</p>
</div>
</div>
4

4 に答える 4

5

あなたが探しているかどうかはわかりません

  1. キャプションとHTMLコードを使用したWordpressエディターによるクイックフィックス
  2. 手作業でキャプションコードを再作成するWordpresshtmlEditorによるクイックフィックス
  3. 関数.phpファイルとwp-includes/media.phpのadd_shortcode()による恒久的な修正
  4. すべてのキャプションに影響を与える可能性のあるCSSによる恒久的な修正。

私はあなたのコードを可能な限り手つかずのままにして1を与えています

<div style="text-align:center;">
// This should align the entire block [it worked for me at least]
    <div style="display:inline-block;">
        <div style="float:left;">
        // Takes care of the centering down the middle
            [caption align="aligncenter" width="150" caption="Protein rest"]
<a href="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest.jpg">
<img title="Mash during protein rest" src="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest-150x144.jpg" alt="Mash during protein rest" width="150" height="144" />
</a>[/caption]
        //Your caption code above with alignnone changed to aligncenter
        </div>
        <div style="float:left;">
            [caption align="aligncenter" width="143" caption="Saccharification rest" captionalign="center"]
<a href="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest.jpg">
<img  title="Mash during saccharification rest" src="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest-143x150.jpg" alt="Mash during saccharification rest" width="143" height="150" />
</a>[/caption]
        </div>
    </div> // End the Block Div
</div> // End the Center Div

[編集:あなたが自分自身に答えたのを見ただけです-.-とにかく投稿します:D]

于 2010-05-19T22:22:31.723 に答える
1

あなたが抱えている可能性のある問題は、divコンテナが自動サイズ設定ボックスのように機能することです。したがって、中央に揃えるように指示すると、divコンテナに設定された幅を指定し、画像をdivコンテナ内に配置し、divをページの中央に配置すると、(ページフローのために)下に伸びる必要があることが自動的に認識されます。それはあなたが望むことをします。コードに基づいて、これはhtmlでどのように表示されるかを示す小さなスニペットです。

<center>
<div width="300">
<a href="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest.jpg">
<img title="Mash during protein rest" src="http://www.mysite.com/wp-content/uploads/2008/06/protein-rest-150x144.jpg" alt="Mash during protein rest" width="150" height="144" />
</a>
<a href="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest.jpg">
<img  title="Mash during saccharification rest" src="http://www.mysite.com/wp-content/uploads/2008/06/saccharification-rest-143x150.jpg" alt="Mash during saccharification rest" width="143" height="150" />
</a>
</div>
</center>

あなたがCSSを探していたのは知っていますが、それはもう少し時間がかかり、私は仕事でドアから出て行っています。

これがお役に立てば幸いです。-ゲイル

于 2010-05-19T20:32:51.310 に答える
0

さて、多くのことをいじった後、私はついにそれを機能させました。

次のようにいくつかの新しいCSS要素を作成しました。

.stackwrapper {max-width: 311px;margin: 0 auto;}
#div1 {width: 162px;float: left;}
#div2 {margin-left: 162px;}

これは私の当面の要求を満たすためにコード化されただけなので、幅は私が取り組んでいた投稿用にハードコード化されていることを理解してください。基本的に、一方の画像の幅は150ピクセル、もう一方の画像の幅は137ピクセルです。キャプションコードは、それぞれに5pxのパディングと1pxの境界線を追加するため、並べて表示する画像の合計幅は311pxです(画像の幅+画像あたり10pxのL&Rパディング+画像あたり2pxのL&R境界線)。

ラッパーはこの幅に設定されており、margin:0 autoはそれを中央に配置しているようです(理由は正確にはわかりませんが...誰かが私を手がかりにできますか?)

次に、image1をdiv1(image + padding + border width)でラップし、image2をdiv2(margin-leftを最初の画像の幅に設定)でラップします。

うまく機能しているようです。画像の幅を指定してその場でhtmlを生成できるように、ワードプレスのショートコードでコーディングする必要があります。

ゲイルのおかげで、幅の設定に沿って考えさせられました。

于 2010-05-19T22:18:38.053 に答える
0

自分の投稿に2回答えるのが適切なスタックエチケットかどうかはわかりませんが(!)、他の人に役立つ場合に備えて、このコードを公開したいと思いました。

これがカスタム関数(Wordpressテーマのfunction.phpファイルに貼り付け)とそれに付随するCSS要素で、ショートコードを使用して2つの画像を並べて配置し、左、右、または中央に配置できます(デフォルトは中央の場合配置は指定されていません)。

2つの画像用にコード化されていますが、より多くの画像を処理するように簡単に変更できます。また、画像で[caption]タグを使用する必要があります。これは、私が必要としていたもの(および文字列が分割されるもの)ですが、さまざまな状況に対処するために後で具体化する可能性があります。

基本的に、新しい[side_by_side]タグ間で画像コードを解析し、[/ caption]で分割して、各要素が1つの画像である配列を作成します。幅を解析し(各画像タグにはキャプションと実際の画像の2つの幅が指定されているため、どちらが大きいかを確認し、それを使用します)、適切なサイズのdivを使用してコードを生成します。

明らかに、これは「すべてをキャッチ」するソリューションではないか、プラグインとしてアップロードします。それは私の非常に特定のニーズに合わせてコーディングされましたが、少しの再コーディングで特定のニーズに合わせて変更できます。私の祝福とともに使用してください、私はそれがあなたのウェブサイトに何をするかもしれないかについて責任を負いません:) PS-正規表現を使用することは一般的にhtmlを解析するのに悪い形であることを知っています、しかし私の場合それは私がしなかったような小さな小規模な仕事でしたそれが問題になると思います。誰かが私に知らせて最適化/再コーディングしたいのなら、私は今日これを行うのに十分なPHPを自分自身に教えました、決して専門家ではありません...

/* CUSTOM FUNCTION ADDED BY JIM - 5/20/2010 TO ALLOW CENTERING SIDE-BY-SIDE IMAGES */

add_shortcode('side_by_side', 'side_by_side');

function side_by_side($attr, $content = null) {

extract(shortcode_atts(array(
    'align' => 'center'
), $attr));

$images = explode( "[/caption]", $content, -1);
foreach($images as &$image)
{
    $image = $image.'[/caption]';
}

$img = array();
foreach($images as $img_tag)
{
preg_match_all('/width=\"(.*?)\"/i', $img_tag, $img[$img_tag]);
}

$widths = array();

for ($i = 0; $i < count($images); $i++)
{
    $firstval = $img[($images[$i])][1][0];
    $secondval = $img[($images[$i])][1][1];
    if ($firstval >= secondval)
        $widths[$i]=$firstval;
    else
        $widths[$i]=$secondval;
}

return '<div class="main-img-wrap wrap' . esc_attr($align) . '" style="max-width: ' . (24 + (int) $widths[0] + (int) $widths[1]) . 'px"><div class="sub-img-wrap" style="width: '
. (12 + (int) $widths[0]) . 'px">' . do_shortcode( $images[0] ) . '</div><div style="margin-left: ' . (12 + (int) $widths[0]) . '">' . do_shortcode( $images[1] ) . '</div></div>';
}

そしてCSS要素:

/* centered side-by-side image wrappers, added by Jim 5/20/2010 */

.main-img-wrap {} /* left blank for future style enhancements */
.sub-img-wrap {float: left;}
.wrapcenter {margin: 0 auto;}
.wrapright {float: right; margin: 0 0 10px 14px;}
.wrapleft { float: left; margin: 0 14px 10px 0; }
于 2010-05-20T19:22:08.937 に答える