I need to display the content of child pages on a parent page in Wordpress (not posts). This works, displays the title of the child page, but I can not get the content to display. (last 2 lines)
global $post;
$args = array( 'numberposts' => 1, 'category_name' => 'foundation','post__in' => get_option( 'sticky_posts' ) );
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) {
$the_query->the_post();
?>
<article class="lead">
<h1><?php the_title(); ?></h1>
<section>
<?php the_content(); ?>
</section>
</article>
<?php
}
$newQuery = new WP_Query();
$all_pages = $newQuery->query(array('post_type' => 'page'));
$foundation = get_page_by_title('Foundation');
$foundation_children = get_page_children($foundation->ID, $all_pages);
query_posts('pagename=foundation');
while(have_posts()) {
the_post();
the_title();
the_content();
}
foreach($foundation_children as $tc) {
echo $tc->post_title; // this works
echo $tc->the_content; // this doesn't
}
The correct variable is post_content.
Try echo $tc->post_content;
Reference: http://codex.wordpress.org/Class_Reference/WP_Post
Related
on my WordPress front-page.php theme-page i use the WordPress Loop to show a number of Youtube Videos. I use acf to get the Youtube-URL and create the iframe from the url with the function convertYoutube. Now when i execute the loop each item is showing 5 videos (number of posts im querying) and each video, also the videos from the other posts. What am i doing wrong?
<?php get_header(); ?>
<main>
<div class="inner">
<div class="video-wrapper">
<?php
// video function
function convertYoutube($string) {
return preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
"<iframe src=\"//www.youtube.com/embed/$2?rel=0\" allowfullscreen></iframe>",
$string
);
}
// args
$args = array(
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => 5,
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="video">
<?php
$title = get_the_title();
echo $title;
$videoURL = get_field('video_url');
$videoEmbedCode = convertYoutube($videoURL);
echo $videoEmbedCode;
?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
</div>
</main>
I want to create custom page to show list title post by tag.
Example:
This title post by tag "handphone"
Title post
Title post
Title post
...etc
Any have code for this problem?
Create a template for your page
write the postcode below into the template
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>5, 'tag' => $brand_name);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
?>
after that assigne the template from admin end to your page and lets see it's wwork or not .
Create a php file in your theme directory. You can give any name. And use code something like this. Replate tag_name by your desire tag. And then create a page. Set the template. You will see the list.
<?php
/**
* Template Name: Title by Tag
*
*/
get_header();
$args=array('posts_per_page'=>5, 'tag' => 'tag_name');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Create page and add below code to your page
<?php
/**
* Template Name: Get title by tag
*
*/
get_header();
$tagname = 'handphone';
$wp_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => $tagname,
'posts_per_page' => -1
));
if ( have_posts() ) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I have this custom post type loop inside taxonomy.php file on wordpress working fine.
<div class="container">
<?php
$i = 1;
//added before to ensure it gets opened
echo '<div class="row">';
$wp_query = new WP_Query(array('post_type' => 'publica', 'posts_per_page' => 6));
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();
// post stuff...
?>
<div class="col s12 m4">
<div class="notboxes">
<?php the_post_thumbnail('medium', array('class' => 'responsive-img')); ?>
<?php get_the_term_list($id, $taxonomy, $before, $sep, $after) ?>
<span class="litletimebox"><?php the_time('H:i'); ?> | <?php echo get_the_term_list($post->ID, 'comision-publicaciones') ?></span>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
Leer Más
</div>
</div>
<?php
// if multiple of 3 close div and open a new div
if ($i % 3 == 0)
{
echo '<div style="clear:both"></div></div><div class="row">';
}
$i++;
endwhile;
endif;
//make sure open div is closed
echo '</div>';
?>
</div>
I bring the number of the taxonomy by using this
$queried_object = get_queried_object();
$ter_id = $queried_object->term_id;
But when I put the cat = $ter_id to filter the taxonomy
$wp_query = new WP_Query( array( 'post_type' => 'publica', 'posts_per_page' => 6, 'cat' => $ter_id));
It wont load on the page.
So I've been searching on the web and I found this loop that pulls all taxonomys with its posts.
<?php
$custom_terms = get_terms('comision-publicaciones');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'publica',
'tax_query' => array(
array(
'taxonomy' => 'comision-publicaciones',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post(); ?>
<?php echo get_the_title(); ?>
<?php endwhile;
}
}
?>
This works perfect in my need, but just need the posts from the taxonomy page im on, for example http://comisionpais.com/comision-publicaciones/salud/
Only load the post that have 'salud' marked.
This image shows all the loop
http://imgur.com/a/hSgmX
Why I pasted first code and last code, if it can help in something.
I'm working on a custom page template that display child pages in a parent page with pagination.
It is working, but the header, and footer are not properly displayed. But when I removed the PHP file of the custom page. The header and footer are back to normal.
What's wrong with my PHP code?
Here's the snippet:
<?php
$ids = array();
$pages = get_pages("child_of=".$post->ID);
if ($pages){
foreach ($pages as $page){
$ids[] = $page->ID;
}
}
$paged = (get_query_var("paged")) ? get_query_var("paged") : 1;
$args = array(
"paged" => $paged,
"post__in" => $ids,
"posts_per_page" => 5,
"post_type" => "page"
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
?>
<div>
<h3><?php the_title(); ?></h3>
<div><?php the_date(); ?></div>
<p><?php the_content(); ?></p>
</div>
<?php endwhile; ?>
<?php endif;
paging_nav();
wp_reset_query();
?>
I am working on a website that is based on Wordpress. My client wants to display the latest post snippet under the header on the main page. I was able to do that but instead of fetching the post's title in the loop it is fetching the page title and displaying it there.
Here is my code :
<?php
$postslist = get_posts('numberposts=1&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?><?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have also tried get_the_title(); but it did not work as well.
The SITE ITSELF
From wordpress codex
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
$recent_posts = wp_get_recent_posts(array('numberposts' => 1);
foreach( $recent_posts as $recent )
echo $recent["post_title"];
if you want to use the loop
$args = array('posts_per_page' => 1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?> <?php the_excerpt(); ?>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please use this:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
<div class="entry">
<h3><?php echo $recent['post_title'] ?></h3>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
<?php echo get_excerpt($recent['ID']); ?>
</div>
}
?>
It worked. There is a plugin called smart post lists that was not letting it work. Now it is working fine.
It was my mistake to not disable it and test this. My code is correct.