integrate wordpress post in existing website - wordpress

I have this website i just completed and i want to integrate my existing wordpress with it.
Note on the homepage i want to pull posts from the wordpress database in and display it in short form as follows
image
title
excerpt
Also i want the full post content to display in another page blog page.
i.e
fetch posts content from DB
display content
All i want to know is that is there a way to just fetch this posts content from the Database and use it as i want on my custom pages ?

You can access WordPress outside of WordPress and execute a query like so:
<?php
// Bring in WordPress
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
// Setup your query
$args = array(
'numberposts' => 3, 'orderby' => 'date', 'order' => 'DESC', 'post_status'=>'publish'
// adjust as you need
);
// Execute your query
$posts = new WP_Query( $args );
if( $posts->have_posts() ) {
while( $posts->have_posts() ) {
// Loop through resulting posts
$posts->the_post();
if ( has_post_thumbnail( get_the_ID() ) ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumb-rectangle' );
}
// Now do something with your post
?>
<div class="pod">
<?php if( $thumbnail ) { ?><img src="<?php echo( $thumbnail[0] ); ?>" width="" height="" alt="<?php the_title(); ?>" /><?php } ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
<?php
}
}
Quick modification to display one post:
<?php
// Bring in WordPress
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
// Setup your query
$args = array(
'p' => __post_id_here__
// adjust as you need
);
// Execute your query
$posts = new WP_Query( $args );
if( $posts->have_posts() ) {
$posts->the_post();
if ( has_post_thumbnail( get_the_ID() ) ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumb-rectangle' );
}
// Now do something with your post
?>
<?php if( $thumbnail ) { ?><img src="<?php echo( $thumbnail[0] ); ?>" width="" height="" alt="<?php the_title(); ?>" /><?php } ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php
}

Related

ACF - How to exclude current page from acf relationship query

I have acf option page with the field Relationship. I display this block on many pages of the site. How can I exclude the current page from the list of displayed pages?
My code for Relationship:
<?php $coin_pages = get_field('sb_sector_pages_pages', 'option');
if( $coin_pages ):
foreach( $coin_pages as $post ) :
$permalink = get_permalink( $post->ID );
$thumbnail = get_the_post_thumbnail( $post->ID, 'full');
$title = get_the_title( $post->ID );
setup_postdata($post); ?>
<li class="coin-article__sidebar-list-item">
<a href="<?php echo esc_html( $permalink ); ?>" class="coin-article__sidebar-list-link">
<div class="coin-article__sidebar-coin-ico">
<?php echo $thumbnail; ?>
</div>
<span class="coin-article__sidebar-coin-title"><?php echo esc_html( $title ); ?></span>
</a>
</li>
<?php endforeach; ?>
I tried to add this to my functions.php but it doesn’t work for me:
add_filter('acf/fields/relationship/query/name=sb_sector_pages_pages', 'exclude_id', 10, 3);
function exclude_id ( $args, $field, $post ) {
$args['post__not_in'] = array( $post );
return $args;
}
How to exclude a current page from the ACF relationship query??

ACF loop for grid, get/display custom field

I'm using this piece of code in my Wordpress template:
<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<div class="col-xs-12 col-md-4"><article><div class="kartel"><a href="' . get_permalink($recent["ID"]) . '">';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'medium');
}
echo '</a></div><h3>' . $recent["post_title"].'</h3> ';
echo '<em>Doelgroep //</em>
<p>One-liner/super korte omschrijving</p>';
echo 'Tell me more ';
echo '</article></div>';
}
wp_reset_query();
?>
Thing is that I now want to add a custom field (let's say 'custom_field') that displays a custom excerpt underneath the thumbnail for the grid. I can get the usual fields (excerpt, title, etc.) but not the custom fields. For example the_field ('custom_field'); isn't working..
Any ideas/suggestions?
Hope to hear from you guys!
Filt
First of all change your approach to queries and Wordpress loops using the WP_Query class.
<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ){
$loop->the_post(); ?>
<div class="col-xs-12 col-md-4">
<article>
<div class="kartel">
<a href="<?php the_permalink(); ?>">
<?php if( has_post_thumbnail("medium") ) {
the_post_thumbnail( "medium" );
}
?>
</a>
</div>
<a href="<?php the_permalink(); ?>">
<?php the_title("<h3>","</h3>"); ?>
</a>
<em>Doelgroep //</em>
Tell me more
</article>
</div>
<?php }
}
wp_reset_postdata();
?>
Later, in the loop of your posts, you can recall the custom field using:
the_field ('custom'); //which prints the result on screen
$var = get_field ('custom'); //or echo get_field ('custom') which returns the content in a variable.
If you want to recall a specific custom field inserted in a post or page or custom post type, you must use the following syntax:
the_field ('custom', $ post_id);
get_field ('custom', $ post_id)
That's all :)

how to display featured images of all posts in a single page of wordpress blog

I am making a WordPress theme and now I want to display featured images of every posts in a page called gallery and should be sorted according to the category of the posts.
Add the following code to your template file and change as per your needs.
<?php
//loop through category
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
//loop through posts of category
$args=array(
'post_type' => 'post',
'posts_per_page' => -1,
'category__in' => array($category->term_id)
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: ' . $category->name.' </p> ';
foreach($posts as $post) {
setup_postdata($post);
// if only featured image set
if ( has_post_thumbnail() ) {
?>
<a href="<?php the_permalink() ?>"><img src="<?php the_post_thumbnail_url('full'); ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>" /><br /><?php the_title(); ?></p>
<?php
}
}
}
}
?>
The following code will display only the featured images (which are referred to as the_post_thumbnail()):
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_post_thumbnail();
} //end while
} //end if
?>

Wordpress List Child Pages like a Category

I'm trying to create a page in Wordpress that acts like a Category page listing the child pages. (like it would on the category page). I would like to add a custom field as the excerpt since pages don't have excerpts (unless there's a simple plugin suggestion for this)
I've been trying to merge a few examples, but not getting the correct end result.
<?php if ( is_page( 777 ) ) : ?>
<?php
$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<div class="entry-header"><h2 class="entry-title"><?php echo $page->post_title; ?></h2></div>
<?
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');
if ( $child_pages ) :
foreach ( $child_pages as $pageChild ) :
setup_postdata( $pageChild );
$thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail', array( 'class' => 'alignleft' ));
?>
<div class="child-thumb">
<a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark" title="<?= $pageChild->post_title ?>">
<?= $thumbnail ?>
</a>
</div>
<div class="entry-summary"><?php the_excerpt(); ?></div>
<? endforeach; endif; ?>
<?php } ?>
<?php endif; ?>
Found two simple plugins that do the trick.
https://wordpress.org/plugins/page-list/
https://wordpress.org/plugins/page-excerpt/

WordPress pagination using shortcode within custom post type single template

I have a custom post type setup to function like pages. I'm using a shortcode function to list a custom post type within one of these pages.
The shortcode works and using the WPNavi plugin it shows the pagination below the posts. The link to the next page shows: "/blog/page/2/" but when the link is clicked it takes you to the same page, not the next page of posts. Is it possible to use pagination within custom post type subpages?
function rmcc_post_listing_shortcode1( $atts ) {
ob_start();
global $post,
$paged;
$authorID = $post->post_author;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array(
'posts_per_page' => '3',
'post_type' => 'custom_type',
'author' => $authorID,
'paged' => $paged
) );
$thumb = '';
$width = (int) apply_filters( 'et_pb_index_blog_image_width', 200 );
$height = (int) apply_filters( 'et_pb_index_blog_image_height', 130 );
$titletext = get_the_title();
$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, true, 'Blogimage' );
$thumb = $thumbnail["thumb"];
?>
<div class="et_pb_posts portal-posts et_pb_bg_layout_light">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post clearfix' ); ?>>
<?php if(has_post_thumbnail()) { ?>
<a href="<?php the_permalink(); ?>" class="post-thumbnail">
<?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
</a>
<?php } ?>
<h3><?php the_title(); ?></h3>
<?php
et_divi_post_meta();
if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) )
truncate_post( 270 );
else
the_content();
?>
</article>
<?php endwhile;
wp_pagenavi( array( 'query' => $query ) );
endif;
wp_reset_query();
?>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
add_shortcode( 'list-posts', 'rmcc_post_listing_shortcode1' );

Resources