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

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

Related

Secondary menu not showing up in wordpress footer

I am a beginner trying to create my own wordpress theme.
I have two nav menus one for the header one for the footer but the secondary one for the footer is not showing up. My code is currently as shown below.
- Functions.php
function base_theme_setup(){
add_theme_support('menus');
register_nav_menu('primary','Primary Header Navigation');
register_nav_menu('secondary','Secondary Footer Navigation');
}
add_action ('init', 'base_theme_setup');
- footer.php
<footer>
<?php wp_nav_menu(array('theme_location'=>'secondary')); ?>
</footer>
<?php wp_footer (); ?>
</body>
</html>
Managed to solve it.
Had some useless undeleted code at the bottom of my index.php. Once deleted the problem went away.
So went from this
<?php
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<h3><?php the_title(); ?>
</h3>
<div class="thumbnail-img"><?php the_post_thumbnail('large'); ?></div>
<p><?php the_content(); ?></p>
<small>Posted on: <?php the_time('F j,Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category(); ?></small>
<hr>
<?php endwhile;
endif;
?>
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>
<?php get_footer();?>
to this
<?php
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_post_thumbnail('large'); ?>
<p><?php the_content(); ?></p>
<small>Posted on: <?php the_time('F j,Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category();?></small>
<hr>
<?php endwhile;
endif;
?>
<?php get_footer();?>
Thanks for everyones tips!

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

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

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]);
});

How do I exclude sub-pages from a page loop?

This is what I have so far. Unfortunately this grabs ALL my pages' titles and content including sub-pages. I only want to display top-level main pages. How can I do this?
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
*Note: I want to display both title and content for the pages. Not just title. Otherwise I would use:
<?php wp_list_pages('depth=1'); ?>
UPDATE: Following konsolenfreddy's advice I was able to loop through my pages properly. However a new problem arose. The content is being striped of its tags. Anyway I can keep them? Here's my new code:
<?php
$pages = get_pages('parent=0');
foreach ($pages as $pagg) {
$option .= $pagg->post_title;
$option .= $pagg->post_content;
echo $option;
}
?>
You can use get_pages() (see http://codex.wordpress.org/Function_Reference/get_pages ), it takes the same arguments as wp_list_pages()
Does this work?
<?php query_posts('post_type=page&post_parent='.$parent);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>

Categories