向自定义文章标题切换类显示相关的文章内容(_content)


Show related post_content to a custom-post title toggle class

我创建了一个自定义的帖子类型,并将标题作为切换按钮加载到我的首页上。现在我想确保togglediv的内容始终与我单击的按钮相关。我试图通过简单地加载帖子内容来实现,但我认为我需要将帖子id作为变量或其他东西放入循环中。我根本不知道该怎么做。我是一个绝对的PHP初学者,希望有人能给我一个提示吗?

相关网站:www.mzk.ernst-werbeagentur.de

我的代码:

<?php 
$query = new WP_Query( array( 'post_type' => 'praxen' ) );
if ( $query->have_posts() ) : ?>
<div class="container-fluid wrapper-slider-head">
<div class="container-fluid slider-head fullscreen" id="img-start">
    <img src="http://mzk.ernst-werbeagentur.de/wp-content/uploads/2015/10/background-start.jpg" width='1920' height='1080'>
</div>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>   
    <div class="container-fluid slider-head fullscreen" id="img-<?php echo $countimg; ?>" style="display:none;">
    <?php echo get_the_post_thumbnail( $post_id, 'full', array( 'class' => 'img-' . $countimg ) ); ?>
    </div>
<?php 
$countimg++;
endwhile; wp_reset_postdata(); ?>
</div>
<?php endif; ?>

<?php 
$query = new WP_Query( array( 'post_type' => 'praxen' ) );
if ( $query->have_posts() ) : ?>
<div class="container links-praxen">
<div class="row">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>   
    <div class="col-sm-4"><div <?php post_class( 'jumbotron p' . $countpost ); ?>><div class="hovertogglecont"><div class="hovertogglecontinside">
    <h2><?php the_title(); ?></h2>
    <p><?php echo get_post_meta($post->ID, 'toggletitel', true); ?></p>
    </div></div></div></div>
<?php 
$countpost++; endwhile; wp_reset_postdata(); ?>

<?php endif; ?>

<div class="container-fluid wrapper-praxisinfo" style="display:none;"> <!-- This div should toggle and show the related post-content by clicking on a link from the loop above. -->
<div class="container">
    <div class="close-post-content"><i class="fa fa-times" style="cursor:pointer; color:#fff;"></i></div>
    <div class="post-content">
        <?php 
        $query = new WP_Query( array( 'post_type' => 'praxen' ) );
        if ( $query->have_posts() ) : ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>   
                <?php the_content(); ?>
            <?php 
            endwhile; wp_reset_postdata(); ?>
        <?php endif; ?>
    </div>
</div>
</div>

我想在第三个循环中显示相关的帖子。

干杯,非常感谢,David

一旦加载页面内容,就无法在单击JavaScript按钮后使用某些参数重新运行php。您需要使用AJAX调用从一些新的PHP文件(例如post-content.PHP)中获取post-content,并将其作为参数,然后将其放置在包装器praxisinfo中。

相反,我建议像现在这样输出所有帖子,并隐藏那些你不想用JavaScript看到的帖子。

<div class="container">
    <div class="close-post-content"><i class="fa fa-times" style="cursor:pointer; color:#fff;"></i></div>
    <div class="post-content">
        <?php 
        $query = new WP_Query( array( 'post_type' => 'praxen' ) );
        if ( $query->have_posts() ) : ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>   
                <div class="post-id-<?php the_ID();?>">
                <?php the_content(); ?>
                </div>
            <?php 
            endwhile; wp_reset_postdata(); ?>
        <?php endif; ?>
    </div>
</div>
</div>

在这里,每个post都封装在一个div中,post ID作为一个类。你只需要隐藏所有帖子,点击按钮就会显示ID点击的帖子。