WordPress custom theme, Why is my content not showing on page - php

My pages are not showing any content on them other then the header and footer I think its something to do with the way i am laying out my but don't really know.
index.php and page.php
<?php get_header(); ?>
<?php if( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endif; ?>
<?php get_footer(); ?>

You forgot while statement.
Here's how your code should look:
<?php get_header(); ?>
<?php if( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>

Related

Display posts only from one category (function modification)

I would like to display post from only one category. How should I change this function?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post','paged' => $paged);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'post-format/content', get_post_format() );
endwhile;
else:
get_template_part( 'post-format/content', 'none' );
endif;
?>
<?php
/*
Template Name: Boats'n'Hoes
*/
?>
<?php get_header(); ?>
<div id="main">
<div id="content" class="narrowcolumn">
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
reference :- https://wordpress.org/support/topic/display-the-posts-of-one-category-in-a-page-solved
Something I created for a client was a template that lists posts from a category that has the same name as the page.
So, create a Page called "funnies" if you want to list all posts in the "technology" category.
Oh, and the original content of the Page is displayed too, if you need an introductory text.
<?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(); ?>
Pass this with $args
'category_name' => 'cat-name',

Manually show specific posts on homepage

To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});

Wordpress loop for static front page template not working

I have a static wordpress front page. I want the code that works on my regular blog template to work on the homepage too.
Currently, it only shows one post. The code in the main index.php is this...
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
content.php calls this....
<div class="col-md-4">
<?php the_excerpt(); ?>
</div><!-- /col-md-4 -->
This works great while on the blog page. But i can't get this exact same content on to the static home page. I know I need a different query for homepage but no idea what that is. Any help appreciated!
Your code looks right to me, you can try to reset the loop and query the posts again.
<?php
rewind_posts();
query_posts( $args );
while ( have_posts() ) : the_post();
?>
<!-- Do stuff... -->
<?php endwhile; ?>
global $query_string;
query_posts($query_string.'&post_type=post&posts_per_page=15');
Adding this on top of your code should work. Try it out.

Whats the most efficient Way to Code this WordPress Loop(s)?

I'm just learning to write WP templates [with Pods] and I think there is a more efficient way to code the if/while loops in the example below...? ie: Chaining the while?
An explanation EDIT: and Code Example would be appreciated in helping my learning curve. Thnx.
<?php
/*
Template Name: myPage Template
*/
get_header();
$mypod = pods('my_events');
$mypod->find('name ASC');
?>
<div>
<?php while ( $mypod->fetch() ) : ?>
<?php
// set our variables
$snippet= $mypod->field('event_snippet');
?>
<p><?php echo $snippet; ?></p>
<?php endwhile; ?>
<?php if ( have_posts() ) : while ( have_posts() ) :the_post(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; endif; get_footer(); ?>
What I am thinking can be done simular to [but anything I try breaks]:
<?php
/*
Template Name: myPage Template
*/
get_header();
if ( have_posts() ) : while ( $mypod->fetch() ) : ( have_posts() ) :the_post();
$mypod = pods('my_events');
$mypod->find('name ASC');
$snippet= $mypod->field('event_snippet');
?>
<div>
<p><?php echo $snippet; ?></p>
<?php the_content(); ?>
</div>
<?php endwhile; endif; get_footer(); ?>

issue with post format in wordpress on a custom page template

Hey guys so I am trying to have a loop run to get all the post that are within a certain category on a page, for some reason nothing happens though?
Code :
<?php
/*
Template Name: djequip
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php /*query_posts('test');uncategorized*/
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Let me know if you can help, thanks a ton!
<?php /* Template Name: TEMPLATE NAME */ ?>
<?php get_header();?>
<?php query_posts('category_name=CATEGORY NAME'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
Is this what you mean?
the_excerpt
can be changed to
the_content
Also I'd just like to add that you can put divs in there around the WP PHP.

Categories