On a page in WordPress, I want to display all childrens of that page. This works at the moment like this:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
What I want is that the loop counts my posts and prints the number in the div, starting with 1. For example:
<div class="child1">
Title of first child
</div>
<div class="child2">
Title of second child
</div>
<div class="child3">
Title of third child
</div>
What are your suggestions?
Just create a $count variable and increment it each time through the loop.
<?php $count = 1; ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page child<?php echo $count++; ?>">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
Related
In this site (given the link below), I show a series of articles. I would like for each of these articles to write a number below. For example: if there are 5 items, each of them will have 1 - 2 - 3 - 4 - 5 at the bottom
I tried to do in multiple ways, with jquery, with a javascript loop but in this case it gives me an error because the variable is rewritten for each article.
<?php /** * Template name: 01. Pagina master */ ?>
<?php get_header(); ?>
<div class="col-sm-12 works" id="content">
<div class="row grid">
<!-- MAIN-->
<main id="main">
<section id="fs-container"
style="background-image:url(http://www.domuspoint.it/wp-content/uploads/2019/08/pergole_domus_point_-8.jpg);">
<?php
$loop = new WP_Query( array( 'post_type'=> 'work', 'posts_per_page' => -1, 'post_parent' => 0 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
?>
<div class="media-project-hover" data-background="<?php echo $backgroundImg[0]; ?>">
<div class="bg-overlay"></div>
<div class="media__body">
<h5 class="media__sub-title title-sub">SOLE</h5>
<h3 class="media__title">
<?php the_title(); ?>
</h3>
<a class="link" href="<?php the_permalink(); ?>">vedi</a>
</div>
<span class="media__number" id="bello">5</span>
</div>
<?php
endwhile; wp_reset_query();
?>
</section>
</main>
<!-- END MAIN-->
</div>
</div>
<?php get_footer(); ?>
https://pastebin.com/KbdzNiZF
What about using a counter, and incrementing it after each loop iteration?
<?php
$loop = new WP_Query( array( 'post_type'=> 'work', 'posts_per_page' => -1, 'post_parent' => 0 ) );
counter = 1; //the counter
while ( $loop->have_posts() ) :
$loop->the_post();
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
?>
<span class="media__number" id="bello"><?= $counter ?></span>
<?php
$counter += 1; //increment counter
endwhile; wp_reset_query();
?>
So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
I am using the following to wrap through a list of posts as I want to display them within divs.
Despite using
global $post;
$currentPage = $post->ID;
and
'post_parent' => $currentPage,
Which has worked fine elsewhere. I can't get the page to only show children and grandchildren of this page.
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'post_parent' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
<?php foreach (array_chunk($posts, 1, true) as $posts) : ?>
<div class="column small-4 medium-4 large-4">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
I am using the code within a custom template.
I have also tried
<?php
global $post;
$currentPage = $post->ID;
$args=array(
'child_of' => $currentPage,
'post_type' => 'page'
);
$my_query = null;
$my_query = new WP_Query($args);
echo $currentPage;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
<?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
<?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
<div class="column small-12 medium-6 large-4 box">
<div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
<?php if( $img ): ?>
<img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
<?php endif; ?>
<span><?php the_title(); ?></span>
</a>
</div>
</div>
<?php endwhile; } ?>
<?php wp_reset_query();?>
but this lists the pages I want followed by what appears to be 10 other random pages from the site's root though it doesn't list every page from the site.
Originally posted this solution as a comment, because I wasn't sure it was the only change that was necessary. Turned out it is, so here's the solution:
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'child_of' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
<?php foreach (array_chunk($posts, 1, true) as $posts) : ?>
<div class="column small-4 medium-4 large-4">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The function get_pages doesn't seem to have post_parent as a valid argument. So you need to use child_of instead.
Reference to Codex
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'child_of ' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
https://codex.wordpress.org/Function_Reference/get_pages
To list All sub pages of current page
<?php
global $post;
$currentPage = $post->ID;
$args=array(
'post_parent' => $currentPage,//change
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
echo $currentPage;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
<?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
<?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
<div class="column small-12 medium-6 large-4 box">
<div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
<?php if( $img ): ?>
<img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
<?php endif; ?>
<span><?php the_title(); ?></span>
</a>
</div>
</div>
<?php endwhile; } ?>
<?php wp_reset_query();?>
only change on code
$args=array(
'post_parent' => $currentPage,//change
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
which display all child pages of current page with their Menu order
About Us
Contact Us
Gallery
Testimonial
Blog
I want to add a Wordpress loop for a specific category in a post template that exculdes the current post.
I was suggested to use:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
But I'm having trouble getting it to work.
My loops currently looks like this.
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Try this code.
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Use
'post__not_in' => array($post->ID)
The two code blocks are using two different techniques for a Wordpress custom loop... the first modifies the global query, and the second creates a new custom query. I've outlined both below with your loop template.
Example with suggested code, global query:
Loop through the global $wp_query object in the loop code:
<div class="video">
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p
</div>
Example with original code, custom query:
Loop through the custom query, adding 'post__not_in':
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Sorry if my original answer was unclear, I initially thought you were combining the two code blocks.
I added a code in my php and i don't know what is wrong, it shows only 2 posts but I got 10 posts in my WordPress can someone check my code?
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'appetizers',
'post_status' => 'publish'
);
$Loop = new WP_Query( $args );
?>
<div class="row">
<?php while ( $Loop->have_posts() ) : $Loop->the_post();?>
<a href="<? the_permalink(); ?>">
<div class="col-sm-3">
<?php the_post_thumbnail(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php the_field('price'); ?>
</div>
</a>
<?php endwhile; ?>
</div>
add 'posts_per_page' => -1 in $args.
May be it is picking up the number of posts from the 'Setting -> Reading' option in back end.