WordPress WPML - Get All Posts in Site Language on Custom Search - php

I'm working on a custom built English website that has WPML integrated in it to get the French translations for the entire site which also includes various custom post types, the main one I'm trying to get to work with it properly is the cpt "Products". So I already have the site translated but where my issue resides is with the custom search form & page that should only pull the cpt "Products".
Whenever I search for a product whether by ID or name, all products relating to the search is pulled as it should BUT is pulled for both languages. I only want it to show English results if it's on the English site and vice-versa for the French site but to no avail I can't get it to work.
CODE Snippet from Search Page:
if (have_posts()) {
$col_count = 1;
echo '
<div class="product-archive">
';
while(have_posts()) {
the_post();
if ($post->post_type == 'product') {
if (has_post_thumbnail($post->ID)) {
$thumbnail = get_the_post_thumbnail_url($post->ID);
} else {
$thumbnail = plugins_url('../images/gcp-no-thumbnail.jpg', __FILE__);
}
if ($col_count == 1) {
/*
echo '
<div class="product-row">
';
*/
}
echo '
<div class="col-sm-' . $column_size . ' product-category">
<a class="borderOutside" href="' . get_permalink($post->ID) . '">
<div class="borderInside">
<img src="' . $thumbnail . '" alt="' . $post->post_title . '">
<h2>' . $post->post_title . '</h2>
</div>
</a>
</div>
';
$col_count++;
if ($col_count > $columns) {
/*
echo '
</div>
';
*/
$col_count = 1;
}
}
}
echo '
</div>
';
}
?>
</article>

Related

How to get the number of pages of current wordpress post?

I do split the $post_content using <!--nextpage--> as separator, but maybe there is another method to obtain the number of pages of a single post because I'm doing it many times on large posts.
The WP_Post class doesn't have a member like $post_pages or something.
Use this for a link to previous posts with directional:
<?php previous_post_link(); ?>
Or this for a link to previous posts that without "<<":
<?php previous_post_link('<strong>%link</strong>'); ?>
The code needs to be inside the loop.
Or
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>

get_the_date with using get_adjacent_post wordpress

I want to show post date in get_adjacent_post. But get_the_date($prevpost->ID) shows post_id.
It can show thumbnail and title.
<?php
$prevpost = get_adjacent_post(true, '', true);
$nextpost = get_adjacent_post(true, '', false);
if( $prevpost or $nextpost ){
?>
<div class="cat_paging">
<div class="row">
<?php
if ( $prevpost ) {
echo '<div class="col-sm-6">
<div>Before</div>
<a href="' . get_permalink($prevpost->ID) . '">' .
get_the_post_thumbnail($prevpost->ID, 'thumbnail') . '</a><p>' .
get_the_title($prevpost->ID) . '</p><p>' . get_the_date($prevpost->ID) . '</p>
</div>';
} else {
echo '<div class="col-sm-6">TOP
</div>';
}
if ( $nextpost ) {
echo '<div class="col-sm-6">
<div>Next</div>
<a href="' . get_permalink($nextpost->ID) . '">' .
get_the_post_thumbnail($nextpost->ID, 'thumbnail') . '</a><p>' .
get_the_title($nextpost->ID) . '</p><p>' . get_the_date($nextpost->ID) . '</p>
</div>';
} else {
echo '<div class="col-sm-6">TOP
</div>';
}
?>
</div>
</div>
<?php } ?>
Somebody knows any idea, please teach me.
get_the_date() accepts two arguments, a format string and a post.
You're passing the post ID in as the first argument instead of the second.
Correct usage:
get_the_date( '', $prevpost )
One other point to note is that I'm passing in the post object rather than the ID. You could pass in an ID however the function would then have to retrieve the post object anyway.
Documentation: https://codex.wordpress.org/Function_Reference/get_the_date
use get_post_meta() instead get_the_date($nextpost->ID).Read official documentation of this function from this link

Wordpress Custom Post Type Loop in a Loop

I'm having a difficult time wrapping my head around an advanced loop in Wordpress and am looking for some direction.
Here's an image of what I'm trying to accomplish:
I'm using Bootstrap 3's tab component to display my content. I've created a custom post type called recipes. Each recipe will generate a new tab which displays the title. When you click on the tab it will display the content for the recipe. These display in "tab groups" of three. After three posts another new "tab group" is created.
Here's the code that I have so far:
<div class="container">
<h2>Our Recipes</h2>
<?php
$args = array(
'category_name' => 'dessert',
'orderby'=>'title','order'=>'ASC',
'post_type' => 'recipes'
);
$count = 0;
$loop = new get_posts($args);
$start =
while ( $loop->have_posts() ) : $loop->the_post();
if ($count % 3 == 0) {
echo '<div class="recipe-tabs">
<ul class="nav nav-tabs">';
}
echo '<li>
<a class="tab-link" href="#recipe-' . $post->ID . '" aria-controls="recipe-' . $post->ID . '" role="tab" data-toggle="tab" aria-expanded="true">'
. get_the_post_thumbnail() .
'</a>
</li>';
if ($count % 3 == 0) {
echo '</ul>
</div>';
}
// This needs to be loaded once
echo '<div class="tab-content"">';
// This data needs to be in a foreach loop
echo '<div role="tabpanel" class="tab-pane active" id="recipe-' . $post->ID . '">'
. get_the_content() .
'</div>';
// This needs to be loaded once
echo '</div>';
endwhile;
wp_reset_postdata();
?>
</div>
I'm able to loop through the tabs and generate them. Where I'm struggling is in creating the (single) content area for each group, and then looping through the tab content.
Please let me know if I've left out anything that would be helpful. And as always, thank you for your help!

PHP rendering two columns, but I need three columns

I have some php that creates two columns of website content categories with child articles from that category. I've tried messing with the code that inserts the after two posts are listed, but it's not working (because I don't know what I'm doing). The current php renders as follows:
<div class="row">
<div class="column col-half">...</div>
<div class="column col-half">...</div>
</div>
I'd like it to render as:
<div class="row">
<div class="column col-third">...</div>
<div class="column col-third">...</div>
<div class="column col-third">...</div>
</div>
Here is the theme's code that renders the HTML:
$st_categories = get_categories($st_hp_cat_args);
$st_categories = wp_list_filter($st_categories,array('parent'=>0));
if ($st_categories) {
foreach($st_categories as $st_category) {
$st_cat_counter++;
if ((!is_int($st_cat_counter / 2)) && $st_cat_counter != 1) {
echo '</div><div class="row">';
} elseif ($st_cat_counter == 1) {
echo '<div class="row">';
}
echo '<div class="column col-half '. $st_cat_counter.'">';
echo '<h3> <a href="' . get_category_link( $st_category->term_id ) . '" title="' . sprintf( __( 'View all posts in %s', 'framework' ), $st_category->name ) . '" ' . '>' . $st_category->name.'</a>';
if (of_get_option('st_hp_cat_counts') == '1') {
echo '<span class="cat-count">(' . $st_category->count.')</span>';
}
echo '</h3>';
Thanks in advance.
You need to change just a couple of things (I improved the code style in terms of lines and indentation as well):
$st_categories = get_categories($st_hp_cat_args);
$st_categories = wp_list_filter($st_categories,array('parent'=>0));
if ($st_categories) {
foreach($st_categories as $st_category) {
$st_cat_counter++;
if (1 === $st_cat_counter % 3 && $st_cat_counter !== 1) { // change 2 -> 3 and use mod operator %
echo '</div><div class="row">';
} elseif ($st_cat_counter == 1) {
echo '<div class="row">';
}
echo '<div class="column col-third '. $st_cat_counter.'">'; // half -> third
echo '<h3> <a href="' . get_category_link( $st_category->term_id ) . '" title="' . sprintf( __( 'View all posts in %s', 'framework' ), $st_category->name ) . '" ' . '>' . $st_category->name.'</a>';
if (of_get_option('st_hp_cat_counts') == '1') {
echo '<span class="cat-count">(' . $st_category->count.')</span>';
}
echo '</h3>';

Wordpress category page not returning array correctly

I am editing my category page. Using some custom fields I am defining an image. For each post within a category I want to add this custom image to an array which I am turning into a gallery of images. I'm using the below code, but for some reason when it comes to imploding the array all I get back is one image (which corresponds to the last post that's loaded in). I'm sure there is probably just something I've put in the wrong place but I just can't figure it out. Any help would be much appreciated.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$gallery_images = get_custom_field('catImage:to_array');
$thumbs_html = array();
foreach ($gallery_images as $galleryID) {
$attachment = get_post( $galleryID );
$description = get_custom_field('catIntro:do_shortcode'); //get pages introductory text
$caption = get_the_title(); //get page title
$button_html .= '<div id="description-button-' . $gallery_images_count . '" class="show-description-button">Show Caption</div>';
$description_html .= '<div id="description-' . $gallery_images_count . '" class="photoDescription" style="display:none;">' . $description . '</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" class="photoCaption" style="display:none;">' . $caption . '</div>';
$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src
$full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src
$thumbs_html[] = '<div class="indvlThumbCont"><img class="thumbImg" src="' . $thumb_img[0] .'"></div>';
$gallery_images_count++;
}//end forEach
//calculate the width of the thumbar
$widthPerImg = 157;
$thumbBarWidth = $gallery_images_count * $widthPerImg;
print $gallery_images_count;
?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<div id="thumbsBar">
<div id="left" class="scrollCtrl left desktopOnly"><span></span></div>
<div class="toHideScrollBar" id="toHideScrollBar">
<div class="moveCanvas" style="width:<?php echo $thumbBarWidth; ?>px;">
<?php echo implode("\n", $thumbs_html); ?>
</div>
</div>
<div id="right" class="scrollCtrl right desktopOnly"><span></span></div>
<span id="total_number_in_gallery " class="<?php echo $gallery_images_count; ?>"></span>
</div>
If you are using a theme like TwentyTwelve (which by default only displays one post on the category page) then that's where the issue is. You'll solve this by (if you are fine with modifying the main loop), adding this just before your code:
query_posts('showposts=y&cat=x');

Categories