我試圖在無限滾動的wordpress站點的每個第7個帖子之間插入谷歌廣告。我搜索並找到了結果,但這並不適合我,因爲我使用的WordPress主題並不複雜。我發現結果是index.php文件<?php endwhile; ?>
後添加以下代碼在主頁和類別wordpress的每第7個帖子之間插入廣告
<?php $postnum++; if($postnum%5 == 0) { ?> YOUR AD CODE HERE <?php } ?>
,但在我的情況下,循環不同於一般wordpress主題不同。我所擁有的循環就是這樣,我想知道如何在我的情況下實現上述代碼。
循環代碼
static function loop($template) {
global $wp_query;
echo '<input type="hidden" id="query-' . $template . '" value="' . urlencode(json_encode($wp_query -> query)) . '" />';
if (count($wp_query->posts) > 0) {
if(self::is_grid($template)){
?>
<div class="loop-container-view grid">
<?php self::loop_switch($template , 1); ?>
</div>
<?php
}else{
?>
<div class="loop-container-view list" id="contloop">
<?php self::loop_switch($template , 0); ?>
</div>
<?php
}
get_template_part('pagination');
} else {
get_template_part('loop', '404');
}
}
然後調用這個代碼,我做到了一個短一點
static function get($post , $template = 'blog_page'){
$meta = meta::get_meta($post -> ID , 'settings');
if(isset($meta['safe'])){
if(meta::logic($post , 'settings' , 'safe')){
$classes = ' nsfw';
}else{
$classes = ' ';
}
}else{
$classes = ' ';
}
?>
<!-- post -->
<article id="post-<?php echo $post -> ID; ?>" <?php post_class('post ' . $classes , $post -> ID); ?>>
</article>
<?php
}
循環切換功能
static function loop_switch($template = '' , $grid = 1) {
global $wp_query;
if (!empty($template)) {
$ajax = false;
} else {
$query = array();
$template = isset($_POST['template']) && strlen($_POST['template']) ? $_POST['template'] : exit();
$query = isset($_POST['query']) && !empty($_POST['query']) ? (array)json_decode(urldecode($_POST['query'])) : exit();
$query['post_status'] = 'publish';
$wp_query = new WP_Query($query);
$grid = isset($_POST['grid']) ? (int)$_POST['grid'] : 1;
$ajax = true;
}
$template = str_replace(array('_hot' , '_new' , '_like') , '' , $template);
if($grid == 1){
$k = 1;
$i = 1;
$nr = $wp_query->post_count;
if (layout::get_length(0, $template) == 940) {
$div = 3;
} else {
$div = 2;
}
foreach ($wp_query->posts as $post) {
$wp_query->the_post();
if ($i == 1) {
if (($nr - $k) < $div) {
$classes = 'class="last"';
} else {
$classes = '';
}
echo '<div ' . $classes . '>';
}
self::grid($post, $template);
if ($i % $div == 0) {
echo '</div>';
$i = 0;
}
$i++;
$k++;
}
if ($i > 1) {
echo '</div>';
}
}else{
foreach($wp_query->posts as $index => $post) {
$wp_query->the_post();
if ($index > 0) {
?><!--<p class="delimiter"> </p>--><?php
}
self::get($post, $template);
}
}
if($ajax){
exit();
}
}
,後在這裏稱爲前置式page.php文件
<?php
$wp_query = new WP_Query(array('page_id' => options::get_value('front_page' , 'page')));
if($wp_query -> post_count > 0){
foreach($wp_query -> posts as $post){
$wp_query -> the_post();
$post_id = $post -> ID;
?>
<article id="post-<?php the_ID(); ?>" <?php post_class() ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- post meta top -->
<?php
if(meta::logic($post , 'settings' , 'meta')){
get_template_part('post-meta-top');
}
?>
</header>
<div class="entry-content">
<?php
/* if show featured image */
if(options::logic('blog_post' , 'show_featured')){
if(has_post_thumbnail ($post -> ID)){
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post -> ID) , 'full');
?>
<div class="featimg circle">
<div class="img">
<?php
ob_start();
ob_clean();
get_template_part('caption');
$caption = ob_get_clean();
?>
<a href="<?php echo $src[0]; ?>" title="<?php echo $caption; ?>" class="mosaic-overlay" rel="prettyPhoto-<?php echo $post -> ID; ?>"> </a>
<?php the_post_thumbnail('600x200'); ?>
<?php
if(strlen(trim($caption))){
?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php
}
?>
</div>
</div>
<?php
}
}
?>
</div>
<footer class="entry-footer">
<div class="share">
<?php get_template_part('social-sharing'); ?>
</div>
<div class="excerpt">
<?php the_content(); ?>
<?php wp_link_pages(); ?>
</div>
</footer>
</article>
<?php
}
}else{
/* not found page */
get_template_part('loop' , '404');
}
該位在哪裏'loop_switch'功能?是叫'get()'嗎? – RST 2015-02-23 18:40:38
@RST我用你想要的細節編輯了我的問題。如果你這樣做,它會真的幫助我。 – 2015-02-23 19:30:49