domPDF and ACF repeater fields - wordpress

I am using domPDF to generate PDFs on certain pages of my site
The problem I am having is that it won’t allow me to use repeater values (via any sort of loop), it doesn’t seem to want to generate them when the PDF is created.
If I call the repeater field itself as a variable, it returns ‘array’, so I assume it’s possible to get it working, somehow.
It appears the repeaters first need to be generated and then included (perhaps as a variable?), but I’m unsure of how to do this.
Does anyone have any idea of how I can get this to work?
This is my ACF repeater code:
<?php if( have_rows('team_contact_details') ): ?>
<div class="profile-item">
<ul class="list-contact">
<?php while( have_rows('team_contact_details') ): the_row(); ?>
<li>
<div class="list-contact-letter">
<?php the_sub_field("contact_label"); ?>
</div>
<div class="list-contact-detail">
<?php
if(get_sub_field('contact_link') == 'phone' ) {
echo '' . get_sub_field("phone_number") . '';
} else if(get_sub_field('contact_link') == 'email' ) {
echo '' . get_sub_field("email_address") . '';
}
?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
This is my domPDF code:
//Print PDF
require_once get_template_directory() . '/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$content = '
<html>
<head></head>
<body>
<div class="person">
<img src="' . get_field('page_image') . '"/>
</div>
<h1>' . get_the_title() . '</h1>
<div class="pdf-label">' . get_field('team_academic') . '</div>
<div class="pdf-label">' . get_field('team_position') . '</div>
<<<< Attempting to put repeater values here as unordered list >>>>
</body>
</html>
';
// instantiate and use the dompdf class
$dompdf = new Dompdf(array(
'enable_remote' => true
)
);
$dompdf->loadHtml($content);
// (Optional) Setup the paper size and orientation
$dompdf->set_paper( 'letter' , 'portrait' );
// Render the HTML as PDF
$dompdf->render();
$doctitle = get_the_title();
$dompdf->stream($doctitle);
Thanks

I got it. FYI, this was how:
if( have_rows('team_contact_details')) :
while(have_rows('team_contact_details')) : the_row();
$content .= '<h3>'. get_sub_field('contact_label') .'</h3>';
if(get_sub_field('contact_link') == 'phone') {
$content .= '<div>'. get_sub_field('phone_number') .'</div>';
} else {
$content .= '<div>'. get_sub_field('email_address') .'</div>';
}
endwhile;
endif;

Related

get_field do not giving output in advance custom field

This is my code but this is not giving any input
<?php
if(get_field('header'))
{
?>
<div class="about-us">
<div class="--header">
<img src="<?php echo get_template_directory_uri()?>/assets/images/laptop.jpg" alt="">
<div class="content">
<?php echo '<h2>' . the_field('heading') . '</h2>';
echo '<h4>' . get_field('description') . '</h4>';?>
</div>
</div>
</div>
<?php
}
?>
This is my custom filed that i created
the_field function echos value. so you don't have to use it in another echo function
please try this code and let me know if it works.
<?php
if ( get_field('header') ){
?>
<div class="about-us">
<div class="--header">
<img src="<?php echo get_template_directory_uri() ?>/assets/images/laptop.jpg" alt="">
<div class="content">
<?php echo '<h2>' . get_field('heading') . '</h2>';
echo '<h4>' . get_field('description') . '</h4>'; ?>
</div>
</div>
</div>
<?php
}else{
_e('the field header not founded','textdomain');
?>
If you're still not getting value from ACF, then you should get value like as below:
<?php
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
echo $value = get_field( 'my_field', $post_id );
?>
I hope it would help you out.
Thanks
You have written a wrong condition. There is no field for header it is just a group where you have created all custom fields. You can give a condition on the custom fields.
So, you need to write a condition like this.
<?php
if( get_field( 'heading' ) ) {
// Your Code
}
?>

Relevanssi - Duplicate Searches logged

I'm using (and loving) Relevanssi plugin for WordPress. Having a problem though with searches being logged twice in user searches.
Found this comment in the plugin comments about it being a problem with the template.
When digging around my search template, I just removed everything, then added things piecemeal until I had double results again.
This is the line in my search template that seems to be the trouble maker, but I don't necessarily see why.
<div style="background-image: url();"></div>
It makes 0 sense to me. If that bit of STATIC html is in the content-search.php template, I get double searches logged. If I remove it, or if all of the articles have get_the_post_thumbnail_urls, then I only get one search logged.
It seems to have something to do with style="background-image:url();" not having a value?
Here's the bigger picture:
search.php:
<?php
global $wp_query;
if ( $wp_query->found_posts > 12 ) {
$search_count = '1 - ' . $wp_query->post_count . ' of ' . $wp_query->found_posts;
} else {
$search_count = $wp_query->post_count;
}
?>
<div class="search-pg-header">
<?php include get_template_directory() . '/templates/partials/search-form.php'; ?>
<?php if ( get_search_query() !== '' ) { ?>
<h1>Showing <?php echo $search_count; ?> results for <?php echo get_search_query() ?></h1>
<?php } // end if ?>
</div>
<div class="posts-container">
<?php if ( have_posts() ):
while ( have_posts() ) : the_post();
get_template_part( 'templates/content', 'search' );
endwhile;
else: ?>
<p>There are no articles that matched your search.</p>
<?php endif; ?>
</div>
<?php if ( $wp_query->max_num_pages > 1 ) { ?>
<button>Show More</button>
<?php } // end if ?>
content-search.php:
<article data-link="<?php the_permalink(); ?>">
<div style="background-image: url(<?php the_post_thumbnail_url('card-white');?>);"></div>
<div>
<h2><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
<?php if ( has_excerpt() ) {?>
<p><?php echo get_the_excerpt(); ?></p>
<?php } //end if ?>
</div>
</article>
I did a fresh install of Wordpress and tested out this bizarre theory with 2017 theme. True enough, if I add <div style="background-image:url();"></div> to the template template-parts/post/content-excerpt.php then duplicate searches are logged.
If that div has an image: <div style="background-image:url(http://image.com);"></div> only one search is logged.
As Mikko Saari (Relevanssi) so kindly helped me with, this is actually unintended behavior from the browser.
It's definitely nice to at least have an explanation for this.
I was able to prevent this by just putting a check to see if there was a bkgd image before I rendered the inline style to the page, and now I know to be much more careful with this kind of thing in the future!
$style = ! empty( get_the_post_thumbnail_url($the_post, 'card-white') ) ?
'style="background-image: url(' . get_the_post_thumbnail_url($the_post, 'card-white') . ');"' : '';
<div class="article-card__img" <?php echo $style; ?>></div>

Get image of a post in Wordpress

I want to make a slide show in wordpress, I made a category for it and now I want to get images of that category for my slide show. Here is part of my code:
<div id="slide-show">
<ul>
<?php query_posts('cat=1'); while(have_posts()): the_post();?>
<li>
<a href="<?php the_permalink();?>">
<img src="<?php
$image=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
echo $image[0];?>" alt="<?php the_title();?>" /></a>
</li>
<?php endwhile; wp_reset_query();?>
</ul>
</div>
But it does not work. Can anybody help me please?
First of all, I'll repeat myself:
First of all, don't use query_posts.
Please, check:
When should you use WP_Query vs query_posts() vs get_posts()?
When to use WP_query(), query_posts() and pre_get_posts
In this case, I'd suggest building a function to create the desired output. You put it inside your theme functions.php file. Put it at the end of the file.
If there is any ?> as the last thing of a PHP file, remove it, it is not necessary and may break your site if there is any white space after it.
That said, our function will print exactly the Html you want and it is called like this in any theme template file (index.php, single.php, page.php, etc).
<?php get_gallery(4); ?>
The number 4 is the Category ID.
And this is the function, check the comments in the code:
function get_gallery( $id )
{
// Simple query
$posts = get_posts( array(
'category' => $id,
'post_status' => 'publish',
'post_type' => 'post',
) );
// Start building the Html code
$slide_show = '
<div id="slide-show">
<ul>';
// Iterate through the results
foreach ( $posts as $post )
{
// Assign value and test if it exists at the *same time*
if( $thumb = get_post_thumbnail_id( $post->ID ) )
{
$permalink = get_permalink( $post->ID );
$image = wp_get_attachment_image_src( $thumb );
// Build the Html elements
$slide_show .= '
<li>
<a href="' . $permalink . '">
<img src="'. $image[0] . '" alt="' . $post->post_title .'" />
</a>
</li>';
}
}
// Finish the Html
$slide_show .= '
</ul>
</div>
';
// Print the Html
echo $slide_show;
}
Result:

WordPress - Get posts in custom taxonomy category

I have a bit of a strange problem with my WP-query. I have a custom post type (portfolio), with a custom taxonomy called year. I have categories for each year, so what I want to do is display all posts for each year. The problem is, only 2012 works. Doesn't matter if I order the categories ASC/DESC - only 2012 works.
<section id="content">
<?php
$categories = get_categories('taxonomy=year&order=DESC');
foreach($categories as $category) : ?>
<article class="year">
<h2><?php echo $category->name ?></h2>
<div class="items">
<?php
$posts = get_posts('taxonomy=year&post_type=portfolio&year=' . $category->slug);
foreach($posts as $post) : ?>
<div class="item">
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category->slug . ']" >';
the_post_thumbnail('thumbnail');
echo '</a>';
?>
</div>
<?php endforeach; ?>
</div>
</article>
<?php
endforeach;
wp_reset_query();
?>
</section>
What am I doing wrong? To me, it seems right.. I've tried a bunch of different takes on this, everything from real querys to ridiculous sortings but I just can't get it right..
Thank you in advance!
I've solved it myself now, still not getting it 100% but it works at least.. There has got to be some smarter way of doing this, since im now looping trough all images for every term. Well, here's the code (get posts grouped by term from custom taxonomy).
<section id="content">
<?php
$categories = get_categories('taxonomy=year&order=DESC');
foreach($categories as $category) { ?>
<article class="year">
<h2><?php echo $category->name ?></h2>
<div class="items">
<?php
$args = array(
'post_type' => 'portfolio'
);
query_posts($args);
$count = 0;
while(have_posts()) : the_post();
$terms = get_the_terms( $post->ID, 'year' );
foreach ( $terms as $term ) {
$imgslug = $term->name;
}
if($imgslug == $category->name) {
if($count == 6) {
echo '<div class="expanded-items">';
}
?>
<div class="item">
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category->slug . ']" >';
the_post_thumbnail('thumbnail');
echo '</a>';
?>
</div>
<?php
}
$count++;
endwhile;
if($count >= 6) {
echo '</div>';
}
?>
</div>
<div class="expand">Visa fler</div>
</article>
<?php } ?>
</section>
That is with an expandable list, so it shows 6 from the start and then expands to show the rest of the items (jQuery).

block in header doesn't appears in drupal

i added a block with some information through sitebuilding->block->addblock
and selected its region as header
But this block doesn't displays in header....any idea?
I am using drupal6
The code of page.tpl.php is as follows
<div id="header">
<div id="header_logo">
<?php if (isset($secondary_links)) : ?>
<div id="secondary-menu">
<?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
</div>
<?php endif; ?>
<?php
// Prepare header
$site_fields = array();
if ($site_name) {
$site_fields[] = check_plain($site_name);
}
if ($site_slogan) {
$site_fields[] = check_plain($site_slogan);
}
$site_title = implode(' ', $site_fields);
if ($site_fields) {
$site_fields[0] = '<span>'. $site_fields[0] .'</span>';
}
$site_html = implode(' ', $site_fields);
if ($logo || $site_title) {
print '<h1><a href="'. check_url($front_page) .'" title="'. $site_title .'">';
if ($logo) {
print '<img src="'. check_url($logo) .'" alt="'. $site_title .'" id="logo" />';
}
print $site_html .'</a></h1>';
}
?>
</div>
<div id="primary-menu">
<?php if (isset($primary_links)) : ?>
<?php print $primary_links_tree; ?>
<?php endif; ?>
</div>
</div>
<!-- end header -->
I'm guessing header is defined in your theme .info file since you see it in the block page.
regions[header] = Header
But what I don't see is you printing the header region in the page template. If the region is named 'header', then you need to add this somewhere in your page template.
print $header;

Resources