在两个不同的WordPress.php模板中加载相同的WP_Query

时间:2020-05-12 作者:user2647510

我成功地将此WP\\U查询加载到我的Wordpress模板全宽主页。php(第一页)文件:

模板全宽主页。php

<?php
/*
Template Name: Full Width (No Sidebar) Homepage
*/

get_header(); ?>

<div class="content">
<div class="homeSlider">
<div class="containerSlide">
<h1 class="headHead">Heading</h1>
</div>
</div>  
<div class="inner-content grid-x grid-margin-x grid-padding-x home">
<main class="main small-12 medium-12 large-12 cell" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part( \'parts/loop\', \'page\' ); ?>
<?php endwhile; endif; ?>

<div class="grid-x grid-padding-x blogsHome">
<?php 
// the query
$the_query = new WP_Query( array(
\'posts_per_page\' => 3,
\'category_name\' => \'360 Themes!\'
)); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'thumbnail\' ); ?>
<div class="cell small-12 medium-6 large-4 singlePost">
<p><?php the_title(); ?></p>
<p><?php the_category(\', \'); ?></p>
<p><?php the_excerpt(); ?></p>
</div>
</div>
<script>
console.log("loaded");
</script>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?> 
<?php else : ?>
<p><?php __(\'No News\'); ?></p>
<?php endif; ?>
</div>
</main> <!-- end #main -->
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
<?php get_footer(); ?>

WP_Query of template-full-width-home.php

<?php 
// the query
$the_query = new WP_Query( array(
\'posts_per_page\' => 3,
\'category_name\' => \'360 Themes!\'
)); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query1->the_post(); ?>
 <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'thumbnail\' ); ?>
  <p><?php the_title(); ?></p>
  <p><?php the_category(\', \'); ?></p>
  <p><?php the_excerpt(); ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?> 
<?php else : ?>
<p><?php __(\'No News\'); ?></p>
<?php endif; ?>
但当我尝试将相同的WP\\u查询加载到另一个模板全宽主页时。php(第二页),我无法得到任何结果(没有像“第一页”那样加载任何内容)。如有任何见解,将不胜感激!

template-full-width-home-es.php
<?php
/*
Template Name: Full Width (No Sidebar) Homepage ES
*/

get_header(); ?>

<div class="content">
<div class="homeSlider">
<div class="containerSlide">
<h1 class="headHead">Heading</h1>
</div>
</div>  
<div class="inner-content grid-x grid-margin-x grid-padding-x home">
<main class="main small-12 medium-12 large-12 cell" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part( \'parts/loop\', \'page\' ); ?>
<?php endwhile; endif; ?>

<div class="grid-x grid-padding-x blogsHome">
<?php 
// the query
$the_query1 = new WP_Query( array(
\'posts_per_page\' => 3,
\'category_name\' => \'360 Themes!\'
)); 
?>
<?php if ( $the_query1->have_posts() ) : ?>
<?php while ( $the_query1->have_posts() ) : $the_query1->the_post(); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'thumbnail\' ); ?>
<div class="cell small-12 medium-6 large-4 singlePost">
<p><?php the_title(); ?></p>
<p><?php the_category(\', \'); ?></p>
<p><?php the_excerpt(); ?></p>
</div>
</div>
<script>
console.log("loaded");
</script>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?> 
<?php else : ?>
<p><?php __(\'No News\'); ?></p>
<?php endif; ?>
</div>
</main> <!-- end #main -->
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
<?php get_footer(); ?>
WP\\U模板全宽主页查询。php模板

<?php 
// the query
$the_query = new WP_Query( array(
\'posts_per_page\' => 3,
\'category_name\' => \'360 Themes!\'
)); 
?>
 <?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query1->the_post(); ?>
   <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'thumbnail\' ); ?>
    <p><?php the_title(); ?></p>
    <p><?php the_category(\', \'); ?></p>
    <p><?php the_excerpt(); ?></p>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?> 
 <?php else : ?>
 <p><?php __(\'No News\'); ?></p>
 <?php endif; ?>

1 个回复
最合适的回答,由SO网友:made2popular 整理而成

请替换此部分代码以调试此问题。

$the_query1 = new WP_Query( array(
\'posts_per_page\' => 3,
\'category_name\' => \'360 Themes!\'
)); 
print_r($the_query1);

相关推荐

如何使用php将html内容添加到自定义帖子类型的帖子中?

我正在尝试做一些我确信很基本的事情,但我不知道怎么做。设置如下:我使用CPT UI插件创建了一个名为board_docs, 我使用自定义字段插件为board\\u docs帖子类型创建了一个自定义字段。自定义字段允许您将文件添加为附件。添加此文件后,将创建一个新的永久链接(post)。当我点击永久链接时,页面是空的,只有页眉和页脚。How would I add an iframe (and other html content) to that page using php?我尝试在函数中使用循环hav