I'm really confused about how this works, I have looked in the Wordpress documentation but I can never find anything simple there anyway. I need 3 loops in my main index page, each loop will be based on one category and will need to grab just the latest post from that category.
I've got it working fine, but I'm just wondering if it is the right way of doing it? Obviously it works, but is it going to cause me any problems doing it this way? Is there a proper way of doing it?
//loop 1
<div class="large-4 columns">
<?php query_posts( 'category_name=stories&posts_per_page=1' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
//loop 2
<div class="large-4 columns">
<?php query_posts( 'category_name=pictures&posts_per_page=1' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
//loop 3
<div class="large-4 columns">
<?php query_posts( 'category_name=videos&posts_per_page=1' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
Ignore the HTML as I haven't formatted it yet. Any help please? Thanks
Yes there are possible problems with this.. You are modifying the original wordpress query. You should ignore using query_posts. You better use one of the following.
1.) get_posts ref
2.) custom wp query ref
Change
<?php query_posts( 'category_name=stories&posts_per_page=1' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
TO
<div class="large-4 columns">
<?php
$the_query = new WP_Query( 'category_name=stories&posts_per_page=1' );
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
the_content();
}
/* Restore original Post Data */
wp_reset_postdata();
/* Added */
?>
</div>
Related
I have created a group field in ACF to display on posts, pages and custom post type. Please see below screenshots.
And here's the code I'm trying for front-end.
<?php $footerCTA = get_field('footer_cta');
echo $footerCTA['title']; ?>
The code above doesn't output anything on the front-end. Am I missing here something?
Thanks.
try this:
if( have_rows('footer_cta') ):
while( have_rows('footer_cta') ) : the_row();
?>
<p><?php the_sub_field('title'); ?></p>
<?php
endwhile;
endif;
?>
Try using.
echo the_field('footer_cta');
Other Way.
You can do this by adding a second parameter to the get_field or the_field functions. This second parameter will contain the correct ID which can be found using get_option('page_for_posts') like so
<h1><?php the_field('footer_cta', get_option('page_for_posts')); ?></h1>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
page-webinars.php:
<?php
/*
Template Name: Webinars
*/
?>
<?php
$loop = new WP_Query(array('post_type' => array('webinars')));
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
?>
<?php get_template_part('loop-webinars'); ?>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata(); ?>
loop-webinars.php:
<?php the_title(); ?>
single-webinars.php:
<h1><?php the_title(); ?></h1>
<div>
<?php
the_post();
the_content();
?>
</div>
Looks like everything's correct. Page displays necessary template, but single not working.
You forget to use WordPress loop.
Try with this code in your single-webinars.php file.
<?php
// Start the loop.
while ( have_posts() ) : the_post();
?>
<?php the_title('<h1>', '</h1>'); ?>
<div>
<?php the_content(); ?>
</div>
<?php // End of the loop.
endwhile;
?>
It had just to "reactivate" theme...
I have some categories like about-us, services etc and want to show different templates for these categories. I have read from wp site to make files name like category-slug.php where slug may be about-us or services. I made these files but they didn't worked for me. Instead of these templates the index file displays the posts. I want to display posts from these categories in custom files.
Can any one tell me how to do this?
Are you looking for something like this?
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Now I've done this a couple other times with no problem, I have a main page using home.php and the blog as another page using "blog-home.php" blog template with all the right code to grab the posts but it's not displaying. The only difference is I've added a custom portfolio post field to the functions, would this be effecting it or could it be something else? I can access a post from the home page under latest post putting the code below but that's it.
<?php query_posts("post_per_page=1"); the_post(); ?>
<p><?php the_excerpt(); ?></p>
<?php wp_reset_query(); ?></div>
*UPDATE: I've tried another code but now it is only displaying the blog page as a post. *
<?php
/*
Template Name: Blog Home
*/
?>
<?php get_header(); ?>
<div id="contentwrapper">
<?php query_posts( array ( 'category_name' => 'Blog', 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="blogentry">
<h4><?php the_title(); ?> </h4>
<?php the_content(); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Maybe if you use
$posts = get_posts(array('numberposts' => 1));
global $post;
$post = $posts[0];
the_excerpt();
instead
query_posts();
is never a good idea change the global query if get_posts dosn't work for you try with WP_Query()
Please at first let me explain my question. I use wordpress to create web sites for flash games, so I don't have certain page for post's. I add each game by
<code>
post-new.php?post_type=game
</code>
and u can see it's not the regular post for wordpress.
I try to use this code from codex:
<code>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$i = 0;
$loop = new WP_Query(array('post_type' => 'game', 'post_per_page' => 5 ));
while ($loop->post_type()) : $loop->game();
?>
</code>
<code>
<?php if ( in_category('') ) { ?>
<div class="post-cat-three">
<?php } else { ?>
<div class="post">
<?php } ?>
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div class="entry">
<p>Category: <?php single_cat_title(); ?></p>
</div>
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</code>
I think it really have to works for posts, but in such case I try to change post for games, try many ways, but don't sucseed yet.
Could anyone tell me what I have change in this code?
i think that promblem in the begining with 'have post' and 'the loop'.
Thanks.
I hope this will help. This is from my WordPress custom post type (loop):
<?php query_posts('post_type=clients&showposts=1000');
if (have_posts()) : while (have_posts()) : the_post();
$nameofclient = get_post_meta($post->ID,'name_of_client',true);
$clientcompany = get_post_meta($post->ID,'company_of_client',true);?>
<div <?php post_class();?> id="ka-<?php the_ID(); ?>">
<h2 class="categorytitle"><?php the_title(); ?></h2>
<?php the_content(); ?><p class="ats_autors">/ <?php if($nameofclient): echo '<span class="client">'.$nameofclient.'</span>'; endif; if($clientcompany): if($nameofclient){echo ', ';} echo '<span class="client-company">'.$clientcompany.'</span>'; endif; ?></p></div><?php endwhile; endif;wp_reset_query();?>
Just found one mistake: in_category('') MUST be filled with category ID in slug. http://codex.wordpress.org/Function_Reference/in_category#Parameters
Correct would be in_category('some-game-cat-slug')
plus worth to read http://new2wp.com/noob/query_posts-wp_query-differences/