admin管理员组文章数量:1278880
This is my site, we're remodeling an nonprofit's website for class (it has no affiliation with the actual organization). On this page: / there is supposed to be a loop that displays content from a specific category "News", with ID 3.
This is newsfeed.php:
$posts = query_posts($query_string.'&cat=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title();?></h1>
<p><?php the_content();?></p>
<?php
endwhile;
endif; ?>
</div>
This gets called by index.php in the following if-else:
<?php
if (is_page( 'home' ) ):
include('loop.php');
include('buttons.php');
include('recent-articles.php');
elseif (is_page('news')):
include('newsfeed.php');
dynamic_sidebar('home_right_1');
endif;
?>
<!-- <?php get_sidebar(); ?> -->
<?php get_footer(); ?>
Weirdly enough, although it's almost identical to what I'm doing on the homepage with recent-articles.php,
<div id="aboutwrap"><?php global $query_string;
$posts = query_posts($query_string.'&cat=2'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title();?></h1>
<?php the_content();
endwhile;
endif; ?>
</div>
newsfeed's content isn't appearing. In fact, it's specifically displaying the title of the page, "News", rather than the title of the only post in that category, which is "Covid Updates".
My professor is probably right that all these loops are making the site screwy, but why two near-identical loops wouldn't work on the same site is nagging me. Any thoughts?
This is my site, we're remodeling an nonprofit's website for class (it has no affiliation with the actual organization). On this page: https://dev-hopeproject.pantheonsite.io/news/ there is supposed to be a loop that displays content from a specific category "News", with ID 3.
This is newsfeed.php:
$posts = query_posts($query_string.'&cat=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title();?></h1>
<p><?php the_content();?></p>
<?php
endwhile;
endif; ?>
</div>
This gets called by index.php in the following if-else:
<?php
if (is_page( 'home' ) ):
include('loop.php');
include('buttons.php');
include('recent-articles.php');
elseif (is_page('news')):
include('newsfeed.php');
dynamic_sidebar('home_right_1');
endif;
?>
<!-- <?php get_sidebar(); ?> -->
<?php get_footer(); ?>
Weirdly enough, although it's almost identical to what I'm doing on the homepage with recent-articles.php,
<div id="aboutwrap"><?php global $query_string;
$posts = query_posts($query_string.'&cat=2'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title();?></h1>
<?php the_content();
endwhile;
endif; ?>
</div>
newsfeed's content isn't appearing. In fact, it's specifically displaying the title of the page, "News", rather than the title of the only post in that category, which is "Covid Updates".
My professor is probably right that all these loops are making the site screwy, but why two near-identical loops wouldn't work on the same site is nagging me. Any thoughts?
Share Improve this question asked Nov 18, 2021 at 20:01 Samantha BanaszakSamantha Banaszak 11 Answer
Reset to default 1Try using the below in your newfeed.php. I was able to test using query_post by passing an array of the category details it had. I've also noticed we have to reset the query since it will be used to grab the page news in its own have_post() / the_post() if not it will overwrite the first the_content() loop.
<?php
defined( 'ABSPATH' ) || die( "Can't access directly" );
$args = array(
'category_name' => 'covid-updates',
'posts_per_page' => -1, // Get all
// Catagory ID can be used instead of slug
// 'cat' => 5,
'order' => 'ASC'
);
query_posts( $args );
if (have_posts()) : while (have_posts()) : the_post();
?>
<h1><?php the_title();?></h1>
<p><?php the_content();?></p>
<?php
endwhile;
endif;
wp_reset_query(); // Destroys the previous query and sets up a new query.
?>
本文标签: theme developmentCategoryspecific loop not working
版权声明:本文标题:theme development - Category-specific loop not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741214838a2359847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论