我的模板(wordpress)有问题.我想创建一个包含3列的投资组合页面,可以在我的投资组合页面中显示帖子(无需跳过新页面).我需要在每三个帖子后重复这三个帖子.我将"隐藏"类分配给我的重复帖子,当点击列时,类应设置为"阻止".我有一个代码:
<?php get_header(); ?>
<section> <div class="container container-bazar container-gallery"><?php
$array = array();
$count = 0;
$i = 0;
$args = array(
'posts_per_page' => -1,
'post_type' => 'gallery',
);
$gallery = new WP_Query( $args );
if($gallery->have_posts()) :
while($gallery->have_posts()) :
$gallery->the_post(); ?>
<div class="col-1 boxes<?php if( $count%3 == 0 ) { echo '-1'; }; $count++; ?>">
<div class="post" id="post-<?php the_ID(); ?>">
<figure class="indent-bot">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_post_thumbnail(array(380,220,true)); ?>
</a>
</figure>
<div class="col-1-content">
<strong class="title-3">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_title(); ?>
</a>
</strong>
<div class="entry">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_excerpt(); ?>
</a>
</div><!-- .entry -->
</div><!-- .col-1-content-->
</div><!-- .post -->
</div> <!-- .boxes -->
<?php endwhile; ?>
<?php while($gallery->have_posts()) :
$gallery->the_post();?>
<?php $imgaddr1 = get_post_meta($post->ID, 'imgaddr1', true);
$imgaddr2 = get_post_meta($post->ID, 'imgaddr2', true);
$imgssilka1 = get_post_meta($post->ID, 'imgssilka1', true);
$imgssilka2 = get_post_meta($post->ID, 'imgssilka2', true);
$namecolor1 = get_post_meta($post->ID, 'namecolor1', true);
$namecolor2 = get_post_meta($post->ID, 'namecolor2', true);
$numbercolor1 = get_post_meta($post->ID, 'numbercolor1', true);
$numbercolor2 = get_post_meta($post->ID, 'numbercolor2', true); ?>
</div>
<div class="full clearfix">
<div class="inner">
<figure class="indent-bot1">
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_post_thumbnail(array(960,690)); ?>
</a>
</figure>
<div class="row">
<div class="col-md-5">
<div class="inf-1">
<h4>??????????</h4>
</div>
<div class="inf-2">
<h5><?php the_title(); ?></h5>
<div class="desc">
<?php the_excerpt(); ?>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="col-md-7 border-left">
<div class="inf-1">
<h4>??????????? ?????</h4>
</div>
<div class="inf-2">
<ul>
<li class="first-child">
<a href="<?php echo $imgssilka1; ?>" class="img-block">
<img src="<?php echo $imgaddr1; ?>">
</a>
<div class="txt">
<strong><?php echo $namecolor1; ?></strong>
<span><?php echo $numbercolor1; ?></span>
</div>
</li>
<li class="last-child">
<a href="<?php echo $imgssilka2; ?>" class="img-block">
<img src="<?php echo $imgaddr2; ?>">
</a>
<div class="txt">
<strong><?php echo $namecolor2; ?></strong>
<span><?php echo $numbercolor2; ?></span>
</div>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</div><!-- .inner -->
</div>
<div class="container container-bazar container-gallery">
<?php endwhile;
else:
endif; ?>
</div><!-- .container -->
</section>
<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)
但此代码按顺序显示帖子.
这是一个非常不寻常的设置,引起了我的思考。有一种方法无需重新运行循环
\n\n您只需运行循环一次。我们将从查询中提取 posts 数组,并通过foreach。这就是我们开始做事的地方
我们需要将内容拆分,以便获得两个包含发布数据的块,并且需要将其保存到一个数组中以供稍后使用。为此,请构建两个串联的数据字符串(一个字符串包含第一个数据块,第二个字符串包含第二个数据块),这些字符串将保存在两个单独的变量中。
完成此操作后,我们需要添加 div 以形成包含三个帖子的帖子块,每个帖子都有一个唯一的类。这适用于两组字符串
现在我们需要计算新的数组键,以便我们可以构建一个新的已排序帖子数据数组,这样我们就有了一个帖子数据块的序列,其中包含来自字符串一的三个帖子,然后是一个包含来自字符串的相同三个帖子的帖子数据块的序列两个等
最后,因为我们的帖子数组仍然是混合的并且是无序的,所以我们将对数组进行排序,以便键是数字,然后我们可以使用最后一个foreach循环来输出我们的帖子数据
在发布代码之前请注意一两点
\n\n您需要修改类等以满足您的需求
代码尚未完全测试,但 div 块和排序按预期工作
我对代码进行了注释,以便更容易理解
最后,代码
\n\n$args = array(\n \'posts_per_page\' => -1,\n \'post_type\' => \'gallery\',\n);\n$gallery = new WP_Query( $args );\n\n// Check if we have posts before we continue\nif( $gallery->have_posts() ) {\n\n // Use the array of posts and a foreach loop to build out super array\n foreach ( $gallery->posts as $key=>$post ) {\n // Setup postdata so we can make use of template tags\n setup_postdata( $post );\n\n // Setup/define our first variable which will hold our first set of post data\n $output = \'\';\n\n // Open a new div on the first post and every 3rd one there after to hold three posts\n if ( $key%3 == 0 ) {\n // We will call this class "first-x" where x represents the block count\n $output .= \'<div class="first-\' . floor( $key / 3 ) . \'">\';\n }\n\n // Concatenate your first loop into a string to our first variable $output\n $output .= \'<div class="post" id="post-\' . $post->ID . \'">\n <figure class="indent-bot">\n <a href="\' . get_the_permalink() . \'" rel="nofollow">\n \' . get_the_post_thumbnail( $post->ID, array( 380,220,true ) ) . \'\n </a>\n </figure>\n <div class="col-1-content">\n <strong class="title-3">\n <a href="\' . get_the_permalink() . \'" rel="nofollow">\n \' . get_the_title() . \'\n </a>\n </strong>\n <div class="entry">\n <a href="\' . get_the_permalink() . \'" rel="nofollow">\n \' . get_the_excerpt() . \'\n </a>\n </div><!-- .entry -->\n </div><!-- .col-1-content-->\n </div><!-- .post -->\n </div> <!-- .boxes -->\';\n\n // Add our closing div after every third post or the last post if there is less than three\n if ( $key%3 == 2 || !array_key_exists( ( $key + 1 ), $gallery->posts ) ) {\n $output .= \'</div>\';\n }\n\n // Create our new array of post data split in two and use with new array keys\n $new_posts_array[floor( $key / 3 ) * 3 + $key] = $output;\n\n // Setup/define our second variable which will hold the second set of post data from our posts\n // This is the set that you would like to hide\n $output_1 = \'\';\n\n // Open a new div on the first post and every 3rd one there after to hold three posts\n if ( ( $key%3 ) == 0 ) {\n // This block of posts will use class "second-x" where x represents the block count\n $output_1 .= \'<div class="second-\' . floor( $key / 3 ) . \'">\';\n }\n\n $imgaddr1 = get_post_meta( $post->ID, \'imgaddr1\', true );\n $imgaddr2 = get_post_meta( $post->ID, \'imgaddr2\', true );\n $imgssilka1 = get_post_meta( $post->ID, \'imgssilka1\', true );\n $imgssilka2 = get_post_meta( $post->ID, \'imgssilka2\', true );\n $namecolor1 = get_post_meta( $post->ID, \'namecolor1\', true );\n $namecolor2 = get_post_meta( $post->ID, \'namecolor2\', true );\n $numbercolor1 = get_post_meta( $post->ID, \'numbercolor1\', true );\n $numbercolor2 = get_post_meta( $post->ID, \'numbercolor2\', true ); \n\n // Concatenate your second set of post data into a string to our second variable $output_1\n $output_1 .= \'<div class="full clearfix">\n <div class="inner">\n <figure class="indent-bot1">\n <a href="\' . get_the_permalink() . \'" rel="nofollow">\n \' . get_the_post_thumbnail( $post->ID, array( 960, 690 ) ) . \'\n </a>\n </figure>\n <div class="row">\n <div class="col-md-5">\n <div class="inf-1">\n <h4>\xd0\x98\xd0\xbd\xd1\x84\xd0\xbe\xd1\x80\xd0\xbc\xd0\xb0\xd1\x86\xd0\xb8\xd1\x8f</h4>\n </div>\n <div class="inf-2">\n <h5>\' . get_the_title() . \'</h5>\n <div class="desc">\n \' . get_the_excerpt() . \'\n </div>\n </div>\n <div class="clearfix"></div>\n </div>\n <div class="col-md-7 border-left">\n <div class="inf-1">\n <h4>\xd0\x9f\xd1\x80\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xb6\xd0\xb5\xd0\xbd\xd0\xbd\xd1\x8b\xd0\xb5 \xd0\xa6\xd0\xb2\xd0\xb5\xd1\x82\xd0\xb0</h4>\n </div>\n <div class="inf-2">\n <ul>\n <li class="first-child">\n <a href="\' . $imgssilka1 . \'" class="img-block">\n <img src="\' . $imgaddr1 . \'">\n </a>\n <div class="txt">\n <strong>\' . $namecolor1 . \'</strong>\n <span>\' . $numbercolor1 . \'</span>\n </div>\n </li>\n <li class="last-child">\n <a href="\' . $imgssilka2 . \'" class="img-block">\n <img src="\' . $imgaddr2 . \'">\n </a>\n <div class="txt">\n <strong>\' . $namecolor2 . \'</strong>\n <span>\' . $numbercolor2 . \'</span>\n </div>\n </li>\n </ul>\n </div>\n <div class="clearfix"></div>\n </div>\n </div>\n </div><!-- .inner -->\n </div>\';\n\n // Add our closing div after every third post or the last post if there is less than three\n if ( $key%3 == 2 || !array_key_exists( ( $key + 1 ), $gallery->posts ) ) {\n $output_1 .= \'</div>\';\n }\n\n // Create our new array of post data split in two and use with new array keys\n $new_posts_array[( floor( $key / 3 ) + 1 ) * 3 + $key] = $output_1; \n\n\n }\n wp_reset_postdata();\n\n // Sort our new array so that the keys are numerical again\n ksort( $new_posts_array );\n\n // Run a foreach loop to output our posts as we need. No need to modify anything here\n foreach ( $new_posts_array as $v )\n echo $v;\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1164 次 |
| 最近记录: |