Is get_template_part() taking into account localization? - wordpress

I am using get_template_part() to dynamically insert wordpress pages. I have (m)qTranslate installed but I can not get any other language than the default one with get_template_part(). The __() and _e() are also not translated.
get_template_part() code:
$q_config['language'] = $_POST['lang'];
$post_id = $_POST['postId'];
$page_data = get_post( $post_id );
ob_start();
get_template_part( 'page-template/page', $page_data->post_name );
$output .= ob_get_clean();
ob_end_flush();
template code example:
<div>
<?php $the_query = new WP_Query( 'posts_per_page=50' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if (in_category( get_category_by_slug( 'designs' )->term_id )) {
$termsArray = get_the_terms( $post->ID, "category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
}
?>
<div class="<?php echo $termsString; ?> item">
<span class="fs-<?php echo $post->ID; ?> ?>"></span>
<div><?php the_title();?></div>
<div class="thumbmail">
<?php
$thumb_url_array = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
echo '<img src="'.$thumb_url.'"/>';
?>
</div>
<span class="description name"><?php _e( 'Click to see more', 'sp' ); ?></span>
</a>
</div>
<?php }
endwhile; ?>
</div>
<?php endif; ?>
</div>
In function.php I have:
load_child_theme_textdomain('sp', get_stylesheet_directory().'/languages');
Does anybody know if get_template_part() can get the localization and how?

I have just replaced the mqtranslate (deprecated) plugin with qtranslate-X and the translation is working fine again with get_template_part().

Related

Wordpress Category loop cannot retrieve ACF image gives Image Value: NULL

I am trying to get the image from a category but I cannot retrieve the image.
Today I already learned that I had to use
get_field('product', $term->taxonomy . '_' . $term->term_id);
to fetch content.
But when I use this same method to fetch an image URL from an ACF Field linked to my custom post type category, I do not recieve any values.
This is my code (the var_dump is included):
<?php
$args = array(
'post_type' => 'segments-overview',
'orderby' => 'date', // we will sort posts by date
);
$query = new WP_Query( $args );
$all_terms = [];
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
$terms = get_the_terms(get_the_ID(), 'category-segments-overview');
foreach($terms as $term) $all_terms[$term->term_id] = $term;
endwhile;
foreach($terms as $term):
?>
<div class="segments-card">
<div class="img">
<?php
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="content">
<div class="title"><?php echo $term->name; ?></div>
<!-- <?php print_r($term); ?> -->
<a class="button transparent" href="/segments/<?php echo $term->slug; ?>">
<?php echo __('View All','axia'); ?>
</a>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
else :
?>
<div class="no-posts-found">
<h2>There were no items found</h2>
<h3>Please try a different search</h3>
</div>
<?php
endif;
?>
I use this var_dump to see if everything is fetched:
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
echo '<pre>';
echo "Image field value:";
var_dump($image);
echo "Category field value:";
var_dump($term);
echo '</pre>';
The only thing is that I do not get the value from my image in my category that is made in ACF.
You can simply get value by passing term object as second parameters.
$image = get_field('image', $term);
Check the docs here: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

How to link a post to its category in WordPress

I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>

WordPress last_modified what was changed

Is it possible to find out if only the date/time of a post has been changed or something in the content? The the_modified_date() function covers both and I can't find a way to do a specific check.
The Background: I have a list of posts which will be published in the future and I want to have some feedback for the users if the date or time of an upcoming post changes.
This is my code so far:
<?php
$loop_ls = new WP_Query( $args_ls );
if($loop_ls->have_posts()) : while($loop_ls->have_posts()) : $loop_ls->the_post();
// set date/time variables
$live_date = get_the_date('j.n.');
$live_time = get_the_date('H:i');
?>
<?php if ( get_post_status() == 'future' ) : ?>
<div class="live live--upcoming">
<p><?php _e('Am', 'textdomain') ?> <span class="post-date"><?php echo $live_date; ?></span> <?php _e('um', 'textdomain') ?> <span class="post-time"><?php echo $live_time; ?></span> <?php _e('Uhr', 'textdomain') ?> <?php _e('zum Thema:', 'textdomain') ?>
<br/><span class="post-title"><?php echo get_the_title(); ?></span>
</p>
</div>
<?php elseif ( get_post_status() == 'publish' ) : ?>
<div class="live live--now">
<p><?php _e('Jetzt:', 'textdomain') ?> <span class="post-title"><?php echo get_the_title(); ?></span></p>
<a class="btn btn--default" href="<?php echo get_the_permalink(); ?>"><?php _e('zum LIVE Video', 'textdomain') ?></a>
</div>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
OK, I figured out a way to save a creation date for posts published in the future (code below). But I still have the problem, that I can't check if only a posts date/time is modified (not the content).
function wx_add_creation_date_to_live_videos( $post ) {
$post_id = get_the_ID();
$post_type = get_post_type($post_id);
if( $post_type == 'video' && has_term('live', 'video_category') ) {
// If this is a revision, do nothing.
if ( wp_is_post_revision( $post_id ) )
return;
$creation_date = get_the_modified_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'creation_date', $creation_date );
}
}
add_action( 'draft_to_future', 'wx_add_creation_date_to_live_videos', 10, 3 );
EDIT:
sometimes you just think too complicated. To check a change on post date/time I changed the code above to this
$orig_publish_date = get_the_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'orig_publish_date', $orig_publish_date );
And now I can compare the values of the orig_publish_date with the current publish_date. And that's it.

Display custom post type categories (terms) in loop

I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

How to list only subpages title and link in a wordpress page div

I have a webblog using wordpress i'm using wp_list_pages() function to get list its Sub Pages Links (excluding main page) in a list, I've tried the below codes but its returning all Pages Sub Page.
I also tried this Argument child_of but its not happning. Please suggest i have tried so many things...
<?php
$output = wp_list_pages('echo=0&depth=1&child_of=0');
if (is_page( )) {
$page = $post->ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
}
echo $output;
?>
This code will list the subpages and siblings of the current page:
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
}
else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div id="submenu">
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>
U can try this code this code may be useful to u
<?php
global $wpdb, $post;
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) :
foreach ( $child_pages as $pageChild ) :
setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle">
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark"
title="<?php echo $pageChild->post_title; ?>">
<?php echo $pageChild->post_title; ?>
</a>
</h2>
<?php endforeach; endif; ?>

Resources