最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Loading the same WP_Query in two different wordpress .php templates

programmeradmin0浏览0评论

I'm successfully was able to load this WP_Query to my Wordpress template-full-width-home.php (page One) file:

template-full-width-home.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; ?>

BUT when I try to load the same WP_Query into another template-full-width-home.php (page Two), I'm not able to get any results (nothing loads as it did on 'page One'). Any insight would be greatly appreciated!

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_Query of template-full-width-home-es.php template

<?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; ?>

I'm successfully was able to load this WP_Query to my Wordpress template-full-width-home.php (page One) file:

template-full-width-home.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; ?>

BUT when I try to load the same WP_Query into another template-full-width-home.php (page Two), I'm not able to get any results (nothing loads as it did on 'page One'). Any insight would be greatly appreciated!

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_Query of template-full-width-home-es.php template

<?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; ?>
Share Improve this question asked May 12, 2020 at 22:21 user2647510user2647510 132 bronze badges 7
  • Are you getting "No News" output on the new page you have created? Can I have the link of that page? – made2popular Commented May 12, 2020 at 22:25
  • Not sure why its not displaying tbh – user2647510 Commented May 12, 2020 at 22:26
  • In the last section of code, i.e. "WP_Query of template-full-width-home-es.php template" you have used $the_query and $the_query1, maybe that's the issue. Do make it the same. – made2popular Commented May 12, 2020 at 22:33
  • ya - i originally had them the same but changed them to see if that was the reason why it wasnt working. I've since changed them so that they're the same but still nothing works – user2647510 Commented May 12, 2020 at 22:38
  • Have you assigned the page template to that particular page? And how are you translating the page? Or is it a whole different site that the English version? – made2popular Commented May 12, 2020 at 22:43
 |  Show 2 more comments

1 Answer 1

Reset to default 0

Please replace this section of code to debug the issue.

$the_query1 = new WP_Query( array(
'posts_per_page' => 3,
'category_name' => '360 Themes!'
)); 
print_r($the_query1);
发布评论

评论列表(0)

  1. 暂无评论