Displaying All Data from Custom Post Type Fields through Advanced Custom Fields - custom-post-type

I have looked all over Google, trying to figure this out. I've made some progress but still stuck. I'm pretty new to ACF and custom post types. I have a custom post type of Attorneys that I setup through WCK. That post type has a field group with field names of attorney_photo, attorney_name and attorney_areas_of_practice. With the code below, I can get the attorney_name and attorney_areas_of_practice (repeater field) to display, but not the attorney_photo. I have the info displaying correctly on each attorneys specific page, but I need this page to be a listing of all the attorneys. Not sure what I am doing wrong with the image part.
<?php get_header(); ?>
<?php
$args = array(
'posts_per_page' => 30,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'attorneys'
);
query_posts($args);
if ( have_posts() ) :
?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="attorney-preview">
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo; ?>" />
<p><strong><?php echo get_post_meta($post->ID,'attorney_name', true); ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<?php get_footer(); ?>

When you add image field through ACF plugin there are some options of return value. For example return value is: image object or return value is: image url, in your case return value might be selected as image object, not image url. That is why here with your code is being returned an array rather than only url. To get only image url from this array, please write as following a bit change:
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo['url']; ?>" />

Try this
<?php query_posts(array('posts_per_page' => 30,'order' => 'ASC','orderby' => 'title','post_type' => 'attorneys'));
if (have_posts()) : while (have_posts()) : the_post();
$attorney_photo = get_post_meta($post->ID, 'attorney_photo', true);
$attorney_name = get_post_meta($post->ID, 'attorney_name', true); ?>
<div class="attorney-preview">
<img src="<?php echo $attorney_photo; ?>" />
<p><strong><?php echo $attorney_name; ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>

Related

Get Post List Custom Post type Category Wise

I want to display all posts as list category wise of a custom post type.
for Ex.:
demo Cat 1
- Post of category demo 1
- Post of category demo 1
- Post of category demo 2
- Post of category name 2
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, //show all posts
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<?php if(has_post_thumbnail()) { ?>
<?php the_post_thumbnail(); ?>
<?php }
/* no post image so show a default img */
else { ?>
<img src="<?php bloginfo('template_url'); ?>/assets/img/default-img.png" alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" width="110" height="110" />
<?php } ?>
<?php echo get_the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
https://wordimpress.com/loop-through-categories-and-display-posts-within/

Pagination not working( using wp-paginate) i m using wordpress

here is my code, I am using wp-paginate plugin in wordpress. I have tried many thing like replacing twentyfourteen_paging_nav(); with
if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentyfourteen_paging_nav();
}
in archieve.php but still its not working.
<ul class="blog-list">
<?php
$blog = array(
'category' => '8', // post_id of category
'order' => 'Asc',
'posts_per_page' => 4,
'paged' => $paged
);
$blog_post = get_posts( $blog );
foreach ($blog_post as $post) : setup_postdata( $post );
$blog_img = $post->ID;
$blog_title = $post->post_title;
$blog_para = $post->post_content;
$blog_trimed=wp_trim_words($blog_para, 45);
$blog_name = get_post_meta($post->ID, 'author name', true);
?>
<li>
<div class="blog-content-left">
<?php echo get_the_post_thumbnail($blog_img, array(420,250)); ?>
</div>
<div class="blog-content-right">
<?php echo $blog_title; ?>
<p class="blog-date-area"><span class="blog-date"><?php echo get_the_date('M d, Y'); ?></span><span class="blog-date blog-crcl"><?php echo $blog_name; ?></span></p>
<div class="blog-para">
<p><?php echo $blog_trimed; ?></p>
</div>
<span class="read-more-btn-blue">
Read More
</span>
</div>
</li>
<?php endforeach;?>
</ul>
<?php echo wp_paginate(); ?>
Thanks for help in advance.
I didn't find any solutions, so I used pagednavi.

Get post_meta of one post type who is different of display post

I'm looking for to get "get_post_meta" from a post type which is different of the display post ?
here is my code , but this display the post_meta of all posts in the post type !
I think it could be compare the posts who have the same slug (post_name) , but I don't know how to do !
<?php $sectionscontact = new WP_query(array('post_type' => 'sections-contact', 'post_count'=>1));if ($sectionscontact->have_posts()): while ( $sectionscontact->have_posts() ) : $sectionscontact->the_post(); $telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;"> Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta(get_the_ID(), 'telephone', true); ?>
</div>
<?php } ?>
What I've done here is to tidy it up for you.
Also, it displays the post_meta for everything in this post type because you're using post_count as opposed to posts_per_page, which is the correct term to display some certain amount of posts in one query.
So, Instead of this:
<?php
$sectionscontact = new WP_query( array(
'post_type' => 'sections-contact',
'post_count' => 1
));
if ($sectionscontact->have_posts()):
while ( $sectionscontact->have_posts() ) :
$sectionscontact->the_post();
$telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;">Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta(get_the_id(), 'telephone', true); ?>
</div>
<?php } ?>
You want this:
<?php
$sectionscontact = new WP_query( array(
'post_type' => 'sections-contact',
'posts_per_page' => 1 //changed post_count to posts_per_page
));
if ($sectionscontact->have_posts()):
while ( $sectionscontact->have_posts() ) :
$sectionscontact->the_post();
$telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;">Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta($post->ID, 'telephone', true); // changed get_the_id() to $post->ID?>
</div>
<?php } ?>
Let me know if that helps.
More info on WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query
Thanks.

Wordpress Pagination for a custom page template and custom post type

Edited: Answered below.
I'm using the following query to paginate my posts on a Custom Page template that pulls a Custom Post type. this exact code works on Author.php, but this custom template Page, "Filmmaking" does not show the pagination. I have a simple, tried and true pagination function after my loop. Why would it work for author.php, but not for page-filmmaking.php?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$film_query = array(
'post_type' => 'filmmaking', // Custom Post Type
'paged' => $paged, // Set it up to be paged so you can use pagination
'posts_per_page' => 2, // How many to show per page
);
// The Query
$the_query = new WP_Query( $film_query );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
Answer: Get rid of if has_posts.
Working Code:
<?php
/**
* Template Name: pagination test
*/
get_header(); ?>
<?php //get_sidebar(); ?>
<div id="page-left">
<?php
$wp_query = new WP_Query();
$wp_query->query(array(
'post_type'=>'filmmaking',
'paged' => $paged,
'posts_per_page' => 2,
));
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php previous_posts_link('« Nyare') ?>
<?php next_posts_link(' Äldre »') ?>
</div><!-- page left -->
<?php get_footer(); ?>

PHPExcel reading xls file, get cell styling (font-weight, color, etc)

I am trying to read an excel file and render it as a html table. I would like to get the style that has been applied in excel to a cell and render it in html as well. For example, some cells may have text in BOLD, how do I get that information and use it in the most efficient way?
This is the code I have so far (I am trying out PHPExcel the first time so I am eager to hear any comments or improvements I can make to this):
if ($_GET["xls"]) {
require_once("classes/PHPExcel.php");
$objPHPExcel = PHPExcel_IOFactory::load( dirname(__FILE__) . "/demo.xls" );
$sheetData = $objPHPExcel->getSheetByName('Sheet1')->toArray(null,true,true,true);
?>
<?php if ( count($sheetData) < 0) : ?>
<table class="table striped">
<?php foreach( $sheetData as $y => $row ) : ?>
<tr>
<?php foreach ( $row as $x => $cell) : ?>
<?php if ( $x === "A" ) : ?>
<th><?php echo $cell; ?></th>
<?php else : ?>
<td><?php echo $cell; ?></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php
}
Why not take a look at PHPExcel's existing HTML Writer: that already handles merged cells, cell formatting (including borders), font styles, etc.
It seems that the following code works:
$objPHPExcel->getSheetByName('Sheet1')->getStyle("B13")->getFont()->getBold()

Resources