标题

时间:2017-02-15 19:23:22

标签: php css wordpress advanced-custom-fields shortcode

希望你们中的一些人可以帮助我,因为我真的输了!这很难解释,但在这里。我最近刚接触ACF(高级自定义领域),所以请原谅我。

设置:

1。我在我的functions.php文件中创建了一个新的短代码(称为[bier])

`    function bierfunction() {
require_once trailingslashit( REDGOTGRILL_THEME_DIR ) . 'bier-page.php';
}
add_shortcode('bier','bierfunction');`

2. 然后我创建了php文件" bier-page.php"如上所述进口。并添加了一些电话:

`    <?php $bier_maand = new WP_Query(array(
'post_type' => 'bier_maand'
)); ?>`

`    <div class="mprm-row">
      <?php while($bier_maand->have_posts()) : $bier_maand->the_post();?>     </div>`

`     <div class="mprm-category-content">
                     <h2 class="mprm-title"><?php the_title(); ?></h2>
                             <p class="mprm-category-description">
                            <?php the_field('beschrijving_bier'); ?></p>
                        </div>`

3. 我希望我可以在任何页面中使用短代码,它会插入我创建的php页面!好吧它有效!真的很好甚至......除了一件事!

问题:我创建的短代码显示在我标题的最顶部。不管我做什么!!或者无论我放在它上面!它将继续显示在页眉下方页面的顶部。

结论:我要把自己扔到窗外!我究竟做错了什么??这是一个CSS样式问题吗?因为我觉得这与ACF电话有关:
    <?php $bier_maand = new WP_Query(array( 'post_type' => 'bier_maand' )); ?>

任何帮助将不胜感激!!!!

先谢谢!

2 个答案:

答案 0 :(得分:1)

你关闭了,@ jh1711正在走上正轨。您的短代码显示在while( have_posts() ) : the_post();来电的最顶部,因为它打印您的标记,而不是返回作为变量显示在短代码的重点。短代码正在运行,但它并没有在正确的地方吐出来。您需要将其更改为:

<?php
function bierfunction(){
  // No need for WP_Query - use get_posts when you can
  $bier_maand = get_posts( array(
    'post_type' => 'bier_maand'
  ) );
  $ret = '<div class="mprm-row">';
  foreach( $bier_maand as $bier ){
    $ret .= '<div class="mprm-category-content"><h2 class="mprm-title">';
    // Not the_title() which prints the title. Use get_the_title() to pass it to your variable.
    $ret .= get_the_title( $bier->ID );
    $ret .='</h2><p class="mprm-category-description">';
    // Don't use the_field() which prints the field value. Use get_field() to pass the value to your variable;
    $ret .= get_the_field( 'beschrijving_bier', $bier->ID ); 
    $ret .= '</p></div>';
  }
  $ret .= '</div>';
  // Return the value of the above output so WordPress puts in on screen at the location of your shortcode.
  return $ret;
}
add_shortcode( 'bier', 'bierfunction' );

答案 1 :(得分:0)

Afaik,您必须从短代码功能中返回html。如果您包含/要求它,它将被放置在某个地方。对于你的例子(未经测试!!):

function bierfunction() {
  $bier_maand = new WP_Query(array(
    'post_type' => 'bier_maand'
  ));

$ret = '<div class="mprm-row">';
while($bier_maand->have_posts()) $ret.=$bier_maand->the_post();
$ret.='</div>';

$ret.='<div class="mprm-category-content"><h2 class="mprm-title">';
$ret.=the_title(); 
$ret.='</h2><p class="mprm-category-description">';
$ret.=php the_field('beschrijving_bier'); 
$ret.='</p></div>';
return $ret;
}
add_shortcode('bier','bierfunction');`