Load Child Content with template styling in parent template - php

I cannot find a solution for my problem and I am pretty new to WordPress templates.
I want to load all the direct childs from my active page and load them together with the template which also uses one added image in acf. The idea is to have more than 1 static design for the loaded content.
This is the code I am using, but the_content() only loads the content field itself and not the template or the acf image.
I tried get_post but it doesn't work.
i would really appreciate if you can help me with that. :)
Best wishes,
Mike
<?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() ) :
while ( $parent->have_posts() ) : $parent->the_post();
the_content();
endwhile;
endif;
wp_reset_query();
?>

Your query is sound, but the issue is that you're not pulling any of the info in your loop.
The way WordPress works, is that on the individual template file that you are working with, you need to specify the specifics of the layout from those pages within this parent template.
How it is done is entirely dependant on the layout(s) of your child page(s), your template file structure (are you separating your templates into smaller, reusable elements (i.e. using get_template_part()), etc...
Essentially, you'll need to add into this template that you're working on, the additional content in the layout you require.
See the example below for a rough idea:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
?>
<?php if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<h3><?php the_title(); ?>
<div class="img-from-child">
<img src="<?php get_field('img_src_from_child'); ?>" />
</div>
<?php the_content(); ?>
<?php endwhile;
<?php endif; ?>
<?php wp_reset_query(); ?>

Related

Wordpress blog pagination

I have a posts loop in home.php:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
the_content();
}
wp_reset_postdata();
} ?>
How can I add pagination (numbers)? I looked for a tutorial. I tried a lot of functions but none worked.
Thank You
Should be possible using this code:
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
mid_size defines how many page numbers will be displayed on either side of the current page in that line.
For more details including individual texts for the previous/next page links see:
https://codex.wordpress.org/Function_Reference/the_posts_pagination

WordPress wp_pagenavi for CPT using custom wp_query() on another CPT single page

I know there were a lot of questions about how to use wp_pagenavi with custom wp_query() for CPTs, and I can handle it easily. But I've encountered a bit more complicated situation.
I have a CPT named 'project' and CPT named 'avtoritet_audiotape'. What I need to do - is to show all CPT's 'avtoritet_audiotape' using pagination, but on the single 'project' CPT - it's a kind of relation (for example while I am on page http://simpex/project/radioprogramma-avtoritet, where 'project' is my CPT name).
My code, which is working perfectly on any archive pages, or even single pages is:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
Information related to my single CPT instance 'project'
<?php endwhile; ?>
<?php $loop = new WP_Query(
array(
'post_type' => 'avtoritet_audiotape',
'posts_per_page' => 6,
'paged' => get_query_var('paged'),
'meta_key' => 'audiotape_date',
'orderby' => 'meta_value_num',
) );
?>
<?php if ( $loop->have_posts() ): ?>
#Some stuff
<?php wp_pagenavi( array( 'query' => $loop ) ); ?>
<?php endif; ?>
So, pagination shows pagination links correctly, but when trying to access http://simpex/project/radioprogramma-avtoritet/page/2 - it redirects to http://simpex/project/radioprogramma-avtoritet
Is it possible in anyway? Thank you
plz try this way.
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$postArray = array('post_type' => 'room', 'posts_per_page' => 5, 'order' => 'DESC', 'paged' => $paged);
$roomPost = new WP_Query($postArray);
global $post;
if ($roomPost->have_posts()) {
while ($roomPost->have_posts()) : $roomPost->the_post(); ?>
<div class="post_title"> <?php the_title(); ?></div>
<?php
endwhile;
wp_pagenavi(array('query' => $roomPost));
}

Wordpress orderby in query rendering same posts

I'm trying to loop through posts and generate a few different posts each time the page is hit. I've used this exact same process before and it's always worked fine, but for some reason on this site it's behaving differently. It will load random posts, but only the first time. In other words, it's loading them in a scattered order, but it loads the same posts in the same order every time. Instead, I want it loading completely different posts every the page is hit. I have no idea what's going on with it! I've tested plugins and themes already, no luck. Any ideas?
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 5
);
$wp_query = new WP_query($args);
while(have_posts() ) : the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
wp_reset_query();
?>
Any installed theme or plugin may have used orderby, so try to remove it first when you're using for your own query.
<?php
remove_all_filters('posts_orderby'); //Reset orderby filters
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 5
);
$wp_query = new WP_query($args);
while(have_posts() ) : the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
wp_reset_query();
?>

Wordpress displaying posts on different pages

I am creating a landing page (one page with many section) in WordPress, and I'm calling each section using get_template_part(). So I have something like this;
<?php get_template_part( 'content', 'reasons-to-purchase'); ?>
<?php get_template_part( 'content', 'testimonials'); ?>
<?php get_template_part( 'content', 'reasons-to-purchase'); ?>
Each content file then has a loop to return the posts that match its category, with posts_per_page set to one, as shown below (content-reasons-to-purchase.php);
<?php
$args = array(
'post_type' => 'section',
'category_name' => 'Reasons-To-Purchase',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query ( $args );
if (get_category_by_slug('Reasons-To-Purchase')->category_count > 0 ) {?>
<!-- Featured content image or slider. -->
<div class="container panel">
<?php if ( have_posts() ) : while ( $the_query->have_posts () ) : $the_query->the_post(); ?>
<?php the_content(); ?>
</div>
<?php } ?>
<?php wp_reset_query(); ?> <div class="clearfix"></div>
What I would like to is create as many as these sections in which ever order I like and the loop within each section pulls the next post assigned to that category. So for example in the first 'reasons-to-purchase' section the first post is pulled and then in the second 'reasons-to-purchase' the second post is called. At the moment I reset the loop in each file using 'wp_reset_query()' so it doesn't interfere with the next section which may be different. Basically the loop has to continue for the next similar named section without duplicating any posts..
Any ideas on how to do this or advice would be most appreciated.
You need to pass page parameter before calling get_template_part() but get_template_part() does not support the re-use of your variables, you need to use locate_template() and include()
$page=1;
include(locate_template('content-reasons-to-purchase.php'));
include(locate_template('content-testimonials.php'));
$page=2;
include(locate_template('content-reasons-to-purchase.php'));
$page=3;
include(locate_template('content-reasons-to-purchase.php'));/* and so on page 4,5,6... */
In you template make a slight change of code
if(empty($page)){
$page=1;
}
$args = array(
'post_type' => 'section',
'category_name' => 'Reasons-To-Purchase',
'posts_per_page' => 1,
'orderby' => 'date',
'paged'=>$page, /* <======= get page no from your file i.e locate_template(...) and use here*/
'order' => 'DESC'
);
Hope it makes sense

Display WordPress posts on a separate page

I've got a blog and a website; the two are separate things all together. For example, I have www.domainname.com as the main site, and the blog would be at www.domainname.com/blog/
It's a WordPress blog, however I don't want people looking at the WordPress front-end, so I would like to write a php function that would pull the posts from WordPress to a separate page on the main site.
The function I've got so far is as follows:
<div id="blogPosts">
<?php
require('../path1/path2/wp-blog-header.php');
?>
<?php
$posts = get_posts('numberposts=10&order=DESC&orderby=post_title');
foreach ($posts as $post) : start_wp();
?>
<h4 class="blogDate"> <? the_date(); ?> </h4>
<hr />
<h5 class="blogTitle"> <? the_title(); ?> </h5>
<p class="blogText"> <? the_excerpt() ?> </p>
<br />
<?php
endforeach;
?>
</div>
It will display the page fine, but it's not posting the posts to the page at all. Any ideas why it won't work?
first, put this before u include wp-blog-header.php
define('WP_USE_THEMES', false);
and i think you are looking wp_query not get_posts, if you want to use the loop functions
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
wp_reset_postdata();
Are you sure you are getting the posts? Try dumping your posts after you fetched them, like this:
var_dump($posts)
Does the $posts-variable contain anything? If not, you probably won't get the posts, because $wpdb (the database connection class) is still undefined.
Secondly, what happens when you try
<?php echo $post->post_date ?>
instead of
<? the_date(); ?>
It could be that wordpress doesn't realise you are in a loop and therefor the loop-functions (e.g. the_date, the_title, ...) won't work.
let me know if one of these things works.
Just from a quick search I see that usage of get_posts( $args ) expects $args to be an array with these optional values
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => ,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' => ,
'post_type' => 'post',
'post_mime_type' => ,
'post_parent' => ,
'post_status' => 'publish' );

Categories