Display WordPress posts on a separate page - php

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' );

Related

WP_Query - `category__not_in` not working when showing sticky posts

I'm trying to show the first 4 sticky posts that do not have the videos category and this is my current loop:
<?php $videos_cat_id = get_cat_ID('videos');
$args = array(
'post__in' => get_option('sticky_posts'),
'category__not_in' => $videos_cat_id,
'ignore_sticky_posts' => 1,
'posts_per_page' => 4,
);
$featured_loop = new WP_Query($args);
if ($featured_loop->have_posts()) :
while ($featured_loop->have_posts()) : $featured_loop->the_post(); ?>
<div class="col-sm-6 m-bottom p-left-none p-right-none">
<?php get_template_part('card-featured') ?>
</div>
<?php endwhile; wp_reset_postdata();
endif; ?>
But I'm still seeing posts with the videos category rendered. I'm not entirely sure why this loop isn't respecting category__not_in, any ideas?
Try this:
you may need to supply an array value in category__not_in. And since get_cat_ID() function returns string/int, you may need to this -> 'category__not_in' => array($videos_cat_id)

Load Child Content with template styling in parent template

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

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

How to Terminate the effect of function before the other starts in wordpress

I want to show few random posts at the end of each single post. I found this code for this purpose.
<div>
<h2>Random Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
</div>
But the problem is that when I use this code on my site, it also cause the random output of next and previous post links present at the bottom of each post for better navigation.
I want to know how to terminate the effect of random posts so that pre and next post links are displayed in their original order.
Try
<?php wp_reset_query(); ?>
More info here : http://codex.wordpress.org/Function_Reference/wp_reset_query

Categories