我想在WordPress循环中的帖子(如“赞助商”)之间添加一篇帖子(来自特定类别)。示例:
P P S
P P S P
P S P P
我怎样才能做到这一点?我是一个编码初学者,所以我不知道如何修改循环。有没有循环编码忍者可以提供解决方案?
请注意,下面是我的当前循环。它用于按价格或随机顺序对帖子进行排序:
指数php
<?php while (have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); the_meta ();
endwhile;
previous_posts_link();
next_posts_link();
?>
功能。phpfunction my_custom_query($query){
if ( $query->is_home() && $query->is_main_query() ) {
$sort= $_GET[\'sort\'];
if($sort == "A"){
$query->set( \'orderby\', \'rand\' );
$query->set( \'posts_per_page\', \'2\' );
}
if($sort == "B"){
$query->set( \'meta_key\', \'price\' );
$query->set( \'orderby\', \'meta_value_num\' );
$query->set( \'order\', \'DESC\' );
$query->set( \'posts_per_page\', \'2\' );
}
}
}
add_action( \'pre_get_posts\', \'my_custom_query\' );
Edit: Update
Birgire的插件有效!起初,我很难让插件在我的主题上工作。问题是我在索引中的循环中使用的这段代码。php(我使用它来调用自定义字段)。<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, \'price\', true);
wp_reset_query();
?>