ACF - How to exclude current page from acf relationship query - wordpress

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??

Related

Get ACF Taxonomy selection in Block and display fields

Figured this would be pretty easy but I am running into an issue.
The website I am building, the client has a list of taxonomies that have an ACF Image Field and ACF Description field.
What they want to do is have a block where they can select certain ingredients from the Taxonomy Block, then have it render out formatted (on a page)(At this time it doesnt need to link to the actual category) but they want to do it this way so they dont need to update page by page when an ingredient changes description or image they can just change it in the taxonomy list.
Below is the code i am using to try and get it going from the docs, it wont render the name or original description it will render the slug but skips over the name but the slug is correct
I've been trying this with no luck, it just returns 3 li's which is correct but i can get a name or custom field to come through.
If i just the the_field('ingredients_selector'); I get the ID's just fine But for the life of me i can not get a term name or the ACF field attached to it/
$terms = get_field('ingredients_selector');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="ingredients-list">';
foreach ( $terms as $term ) {
echo '<li class="ingredients-list__item">' . $term->name . '</li>'; ?>
<p>Description: <?php the_field('description', $term); ?></p>
<p>Image: <?php the_field('image', $term); ?></p>
<?php }
echo '</ul>';
}
?>
I've also tried this way, this gives me same reuslt but the slug with work, it will skip term name again but "view all" will link at least
<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', $term); ?></p>
<p>Term Image: <?php the_field('image', $term); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
View all '<?php echo esc_html( $term->name ); ?>' posts
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Attached is my set up of the ACF fields
EDIT****
This was my solution
<?php
$tax = 'ingredients';
$terms = get_terms( $tax, $args = array(
'hide_empty' => false, // do not hide empty terms
));
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('image', 'ingredients_' . $term->term_id );
$description = get_field('description', 'ingredients_' . $term->term_id );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
echo $description;
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Try this and replace term_name_ with your actual term slug:
<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', 'term_name_'.$term->term_id); ?></p>
<p>Term Image: <?php the_field('image', 'term_name_'.$term->term_id); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
View all '<?php echo esc_html( $term->name ); ?>' posts
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
You can find more about this here.

how to use Custom post type dynamically

I have created a new template page and I am displaying custom post type in that page as follows,
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'astroalbums',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<?php $link = get_permalink($post->ID); ?>
<?php the_title( '<h3 class="entry-title">', '</h3>' );?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
My custom post type is "astroalbums" and I want to use it dynamically. I have 4 custom post types. I want to create new page in dashboard and assign the above page template i have created. and each page will call different custom post type.
It will be really great help
Thank you,
Trupti
You're retrieving the posts correctly, but it's seems that the problem is inside the foreach loop. As the default WordPress loop is not being used, you need to call functions which receive the post id as parameter or use the properties present in the $post object (which is an instance of the WP_POST class) in order to display the data.
One possible solution:
<?php
$posts = get_posts([
'post_type' => 'astroalbums',
'posts_per_page' => 1
]);
?>
<?php foreach( $posts as $post ): ?>
<?php $link = get_permalink( $post->ID ); ?>
<h3 class="entry-title">
<a href="<?php echo esc_url( $link ); ?>" rel="bookmark">
<?php echo get_the_title( $post->ID ); ?>
</a>
</h3>
<a href="<?php echo esc_url( $link ); ?>">
<?php echo get_the_post_thumbnail( $post->ID ); ?>
</a>
<?php endforeach; ?>

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

integrate wordpress post in existing website

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
}

Resources