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>
Related
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;?>
I am using the following code to retrieve categories from a custom post type:
<?php
$taxonomy = 'treatment';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li>
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<h4><?php echo $term->name; ?></h4>
</a>
</li>
<?php } ?>
</ul>
<?php endif;?>
Now I want to be able to re-order these posts and have found a suitable plugin to do this. However, in order to make this work, I need to add 'orderby' => 'term_order' to the query.
Is this even possible with this loop? I have tried a loop like this within the above but it doesn't work. With the loop alone I cannot fetch the custom post categories:
<?php $loop = new WP_Query( array( 'post_type' => 'treatments', 'posts_per_page' => 1, 'orderby' => 'term_order' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Content here
<?php endwhile; wp_reset_query(); ?>
Place this given code inside the loop.And please replace "custom_taxonomy_name" with your custom taxonomy name which you have registered for custom post type
<?php $terms = get_the_terms(get_the_ID(),'custom_taxonomy_name' ); ?>
<?php foreach( $terms as $term): ?><br>
<span class="text-category">Category:<?php echo $term->name; ?></span>
<?php endforeach; ?>
I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>
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; ?>
I want to get my Category images into my WooCommerce loop below but the array that's created from my code below doesn't seem to collect the image URL.
PHP
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
<?php foreach($catTerms as $catTerm) : ?>
<ul>
<li><?php echo $catTerm->name; ?></li>
</ul>
<?php endforeach; ?>
Would someone be kind enough to assist?
Thanks
Try this,
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
<?php foreach($catTerms as $catTerm) :
$wthumbnail_id = get_woocommerce_term_meta( $catTerm->term_id,'thumbnail_id', true );
$wimage = wp_get_attachment_url( $wthumbnail_id );
?>
<ul>
<li><?php if($wimage!=""):?><img src="<?php echo $wimage?>"><?php endif;?><?php echo $catTerm->name; ?></li>
</ul>
<?php endforeach; ?>
Hope its works..
I think you have to put the UL tag out of the foreach to no repeat the UL for each category.