I'm convering a html one page to wordpress but I can't seem to get the title for each section to return the right vaule.
This code is form page.php which displays the one page:
<?php
// Template Name: One Page Layout
get_header();
?>
<?php
// Main Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
// GET HEADER
get_template_part( 'front-header');
// PROMOTION SECTION
get_template_part( 'front');
// LATEST WORK
get_template_part( 'lwork');
// LATEST POSTS
get_template_part( 'lpost');
// ABOUT ME
get_template_part( 'about');
// CONTACT
get_template_part( 'contact');
endwhile; endif; wp_reset_query();
get_footer();
?>
Here is the title I want to show (lpost.php):
<!-- section heading -->
<section class="section-heading">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</section>
What am I missing out on??
You need to put loop above the section
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="section-heading">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</section>
<?php endwhile; endif; wp_reset_query(); ?>
Your code should be:
<?php
query_posts(array('post_type' => 'post'));
// Main Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
you can use this code : get_title()
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="section-heading">
<h2><?php get_title(); ?></h2>
<?php the_content(); ?>
</section>
<?php endwhile; endif; wp_reset_query(); ?>
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 -->
So I want to return a Wordpress loop that returns just one category, so currently I have something like this
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This works great and does exactly what I want it to do
But I want the category number to be set by an ACF, so I have that set up and it's returning my value just as a string on the site, all good again, so now my code looks like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
What I want to do is something like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('"cat=", the_field("category_number")'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
I don't think I'm going about this the right way and can't seem to find an answer for this as I don't think I'm looking for the right thing .. if someone could point me in the right direction on this I'd be really grateful
Use get_the_field instead of the_field in query posts method something like this
query_posts('cat=get_the_field("category_number")');
note:- the_field() is echo everything which field contain.
get_the_field is only get the field not echo it.
Hope it will help you :)
Have you tried something along these line?
https://wordpress.stackexchange.com/questions/127940/use-advance-custom-field-inside-query-post-command
<?php query_posts('cat=<?php the_field("category_number"); ?>'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Or Maybe something like this?
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I need to style the first post in the loop differently from the rest in a wordpress theme template. Is there a specific wordpress function to check for this or do I just need to set a "first" flag like the below?
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $first = !isset( $first ) ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php endwhile; ?>
The function I'm looking for would replace the $first = ! isset( $first) check. Does this function exist in WordPress?
Try this
<?php $Inc=0; ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $Inc==0 ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php $Inc++; ?>
<?php endwhile; ?>
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(); ?>