Display custom field from child page Wordpress - php

I'm trying to display a custom field I've made with Advanced Custom fields called Suffix after the child page title, I´m really confused of how to accomplish this.
I've pasted the code a friend helped me with below that lists pages and childpages below.
Is it possible to do this?
<?php
$pageID = $sub_field_3 = get_sub_field('page_id');
$page = get_post($pageID);
echo $page->post_title;
?>
<ul>
<?php
// use wp_list_pages to display parent and all child pages all generations (a tree with parent)
$parent = $sub_field_3 = get_sub_field('page_id');
$args=array(
'child_of' => $parent
);
$pages = get_pages($args);
if ($pages) {
$pageids = array();
foreach ($pages as $page) {
$pageids[]= $page->ID;
$suffix = get_field('suffix', $page->ID);
}
$args=array(
'title_li' => ' ',
'include' => ',' . implode(",", $pageids)
);
wp_list_pages($args);
}
?>
</ul>

Got it, this is how I got it to work.
Thanks, K.
<?php
$parent = $sub_field_3 = get_sub_field('page_id');
$args=array(
'child_of' => $parent
);
$pages = get_pages($args);
if($pages): ?>
<li>
<ul>
<?php foreach($pages as $page): ?>
<li class="">
<a href="<?php echo get_permalink($page->ID);?>">
<?php echo $page->post_title; ?>
<?php
$suffix = get_field('name_suffix', $page->ID);
if($name_suffix):
?>
<span class="suffix"><?php echo $name_suffix; ?></span>
<?php endif; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>

Related

Nested foreach loop not functioning correctly. - wp

I have custom post types 'stockist' and 'product', the latter having a taxonomy 'range'. Products are assigned to stockists using an ACF Post Object.
I'm trying to loop through 'stockist', return the 'range' term from the post object and then list the products for each term. So far I can loop all stockists and return all range terms however the foreach loop that returns the product list doesn't appear to be looping as it only returns the first set of values for each stockist.
Can anyone see where I'm going wrong?
<?php
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown <?php the_title();?>-block">
<?php
$args = array( 'taxonomy' => 'range', 'hide_empty' => 0);
$categories = get_categories($args);
if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
<div class="col-1-5">
<h4>
<?php echo ($category->name) ;?>
</h4>
<ul class="stockist-block-products clearfix">
<?php $post_objects = get_field('stocked_range'); if( $post_objects ):
foreach( $post_objects as $post): $post_terms_array = get_the_terms($post, 'range'); $post_term_name = $post_terms_array[0]->slug;
setup_postdata($post);
if($post_term_name == $category->slug):?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endif;
endforeach;
endif;?>
</ul>
</div>
<?php endforeach; ?>
<?php endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
UPDATE
Replacing the inner loop with the following has resolved the issue:
<?php $post_objects = get_field('stocked_range');
if( $post_objects ): ?>
<ul class="stockist-block-products clearfix">
<?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug;
if($post_term_name == $category->slug):?>
<li>
<?php echo get_the_title($post_object->ID); ?>
<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
</li>
<?php endif; endforeach; ?>
</ul>
<?php endif;?>

exclude category from wp_count_posts()

I am trying to exclude a category from wp_count_posts(). I've found hundreds of posts that explain how to do a WP_Query for a specific post type and get a category and then exclude posts with specific terms or tags.
However not very publicized is how to to exclude a few categories from a specific post type. Once I do that I need to return a post count as well
See below what I am working with. Looking for an alternative method for doing what I have below that will allow for me to exclude 3 specific categories.
<?php
$count_posts = wp_count_posts('sponsors');
$published_posts = $count_posts->publish;
$count = $published_posts/7;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sponsors'
);
query_posts($args);
?>
<?php if( have_posts() ) : $i = 0; $f =7; $countr=0; $z = 0;?>
<div class="gallery-sponsors">
<strong class="title">Special Thanks To Our Sponsors</strong>
<div class="mask">
<div class="slideset">
<?php for ($k = 1; $k <= $count; $k++):
$countr = $countr+$f;?>
<?php if ($k==1):?>
<?php else:?>
<?php $i = $i+$f;?>
<?php endif;?>
<div class="slide">
<ul class="sponsors-list">
<?php $z=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $z++; if ($z<=$countr and $z>$i): ?>
<li>
<div class="img-hold">
<?php the_post_thumbnail( 'sponsors-gallery-home' ); ?><?php echo $z;?>
</div>
</li>
<?php endif;?>
<?php endwhile; ?>
</ul>
</div>
<?php endfor;?>
</div>
</div>
<nav class="pagination"><?php $k =0;?>
<ul>
<?php while ($q<$count-1): $q++;?>
<?php if ($q==1):?>
<li class="active"><?php echo $q;?></li>
<?php else:?>
<li><?php echo $q;?></li>
<?php endif;?>
<?php endwhile;?>
</ul>
</nav>
</div>
<?php endif;?>
<?php wp_reset_query(); ?>
Here's the fix. I had to add the following to the $args.
'cat' => '-71,-72,-73'
Pretty simple but these very little documentation on how to do this with wp_count_posts(). Here was my complete solution.
<?php
$count_posts = wp_count_posts('sponsors');
$published_posts = $count_posts->publish;
$count = $published_posts/7;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sponsors',
'cat' => '-71,-72,-73'
);
query_posts($args);
?>

How can i call hyperlink from my wordpress custom fields?

I can not call custom field data into href.
Code:
enter code here
<?php foreach($books as $book) {?>
<li><img src="<?php echo $book ?>" /></li><?php } ?>
If you have each book as an actual page in wordpress with a parent page of books you could loop through the pages like this:
<?php
$books = get_pages( array( 'child_of' => $post->ID, 'sort_order' => 'desc' ) );?>
<ul>
<?php foreach( $books as $book ) {?>
<li><?php echo $book->post_title; ?></li>
<?php } // End ForEach ?>
</ul>
In your question you have an image as the link so you could add a post thumbnail to the single book page and then display that as the image in the link by changing the standard code to this:
<?php
$books = get_pages( array( 'child_of' => $post->ID, 'sort_order' => 'desc' ) );?>
<ul>
<?php foreach( $books as $book ) {?>
<li><?php echo get_the_post_thumbnail($book->ID, 'thumbnail'); ?></li>
<?php } // End ForEach ?>
</ul>

Exclude a portfolio category from being displayed

I want to ask the Portfolio Page Template to exclude one portfolio category from being displayed in the portfolio. So when it shows the categories: All | Travel | Macro | Archive, etc. I need to exclude Archive (slug archive) from both the top filter and from the All feed, because I want to place that on a separate page called Archive.
How do I do that?
<?php
/*
Template Name: Portfolio number 1
*/
?>
<?php get_header(); ?>
<div class="container">
<div id="homecontent">
<ul id="portfolio-filter" class="filter clearfix">
<li class="active">All</li>
<?php
// Get the taxonomy
$terms = get_terms('categories');
$term_list = '';
// set a count to the amount of categories in our taxonomy
$count = count($terms);
// set a count value to 0
$i=0;
// test if the count has any categories
if ($count > 0) {
// break each of the categories into individual elements
foreach ($terms as $term) {
// increase the count by 1
$i++;
// rewrite the output for each category
$term_list .= '<li>' . $term->name . '</li>';
// if count is equal to i then output blank
if ($count != $i)
{
$term_list .= '';
}
else
{
$term_list .= '';
}
}
// print out each of the categories in our new format
echo $term_list;
}
?>
</ul>
<div style="clear: both;"></div>
<ul id="portfolio-list" class="filterable-grid clearfix centerrow filter-posts">
<?php
// Set the page to be pagination
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
// Query Out Database
$wpbp = new WP_Query(array( 'post_type' => 'myportfoliotype', 'posts_per_page' =>'99', 'paged' => $paged ) );
?>
<?php
// Begin The Loop
if ($wpbp->have_posts()) : while ($wpbp->have_posts()) : $wpbp->the_post();
?>
<?php
// Get The Taxonomy 'Filter' Categories "categories"
$terms = get_the_terms( get_the_ID(), 'categories' );
?>
<?php
$large_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_image[0];
$another_image_1 = get_post_meta($post->ID, 'themnific_image_1_url', true);
$video_input = get_post_meta($post->ID, 'themnific_video_url', true);
$price = get_post_meta($post->ID, 'themnific_item_price', true);
$ribbon = get_post_meta($post->ID, 'themnific_class', true);
?>
<li class="centerfourcol filter" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->slug)). ' '; } ?>">
<?php get_template_part('/includes/folio-types/home_carousel'); ?>
</li>
<?php $count++; // Increase the count by 1 ?>
<?php endwhile; endif; // END the Wordpress Loop ?>
<?php wp_reset_query(); // Reset the Query Loop?>
</ul>
<?php
/*
* Download WP_PageNavi Plugin at: http://wordpress.org/extend/plugins/wp-pagenavi/
* Page Navigation Will Appear If Plugin Installed or Fall Back To Default Pagination
*/
if(function_exists('wp_pagenavi'))
{
wp_pagenavi(array( 'query' => $wpbp ) );
wp_reset_postdata(); // avoid errors further down the page
}
?>
<div style="clear: both;"></div>
</div><!-- #homecontent -->
</div>
<?php get_footer(); ?>
I found the answer, the code below only allows one categorie to be displayed in the portfolio.
‘tax_query’ => array( array( ‘taxonomy’ => ‘categories’, ‘field’ => ‘slug’, ‘terms’ => ‘archive’, ‘operator’ => ‘IN’) ),

List Sibling Pages By Tag

I'm trying to make a list of all sibling (sub)pages of a parent page. That's easy enough.
But I've tagged each sibling child page, and I would like to organize my list like so:
Term 1
- Child/Sibling Page 1
- Child/Sibling Page 2
- Child/Sibling Page 4
Term 2
- Child/Sibling Page 3
Term 4
- Child/Sibling Page 5
- Child/Sibling Page 6
- Child/Sibling Page 7
I need this list to appear on both the parent page and each sibling child page. Here's what I have so far to list all the sibling pages:
<?php if($post->post_parent): ?>
<?php $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); ?>
<?php else: ?>
<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); ?>
<?php endif; ?>
<?php if ($children) { ?>
<ul class="subpage-list">
<?php echo $children; ?>
</ul>
<?php } ?>
I believe I may have come up with an answer. This code may be a little rough, but as far as I can tell it works as needed:
<?php
if($post->post_parent):
$postparent = $post->post_parent;
else:
$postparent = $post->ID;
endif;
$nextTagThumb='-1';
$tags = wp_get_post_tags($postparent);
foreach ($tags as $tag) :
if ($tags) {
$what_tag = $tags[($nextTagThumb+'1')]->term_id;
$tag_title = $tags[($nextTagThumb+'1')]->name;
echo '<div class="Saving_sare">'. "\n";
echo '<h4>'.$tag_title.'</h4>'. "\n";
echo '<ul>'. "\n";
$args=array(
'tag__in' => array($what_tag),
'post__not_in' => array($postparent),
'showposts'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile;
}
echo '</ul>'. "\n";
echo '</div>'. "\n";
wp_reset_query();
$nextTagThumb = ($nextTagThumb+1);
}
endforeach;
?>

Categories