Image in recent post of Wordpress Q and A theme - php

i am using Instant-QA theme, need to update the recent Post widget with image of the user who has asked the questions. I am using the below code in functions.php of subdomain of wordpress website. Currently its showing the simple post--- Any guess in below code-- ?
function print_requested_template_part() {
// Respond only to requests from the same address...
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['including_template_part']) && isset($_GET['load_part']) && $_GET['load_part'] != '' ) {
$part = $_GET['load_part'];
$func = 'render_' . str_replace('-', '_', $part); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
if ( function_exists($func) ) {
// Allow for passing parameters to the function
if ( isset($_GET['params']) ) {
$params = $_GET['params'];
$params = ( strpos($params, ',') !== false )? explode(',', $params) : array($params);
call_user_func_array($func, $params);
} else {
call_user_func($func);
}
}
exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
}
}
add_action('init', 'print_requested_template_part', 1);
function render_my_recent_posts( $numberposts = 5 ) { ?>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul><?php
}

DO not waste you time to indulge urself in the above code. Usel below plugin and gets the immediate output.
http://wordpress.org/extend/plugins/recent-comments-with-avatars/ Use this plugin
Thankyou

Related

Wordpress ACF Get Block Field

I'm running into an issue with ACF, and I just can't figure out what's going on, and nothing on the internet is helping out.
I've added some fields to the Image Slider block:
But no matter what I try inside of our custom block code: image-slider.php I cannot get the values of any of the auto_play fields. get_field always returns null. I know the value is there, because if I dump out get_fields( $postID ), I can see the ['page_builder'][2] element has the value I want. I could get to it that way, but I can't seem to determine which index I'm on (the 2) programmatically.
So if you know either, how I can access the field directly, or figure out my current 'page_builder' index, that would be extremely helpful.
It's super confusing, because the have_rows( 'slide_setting' ) call obviously knows where to look, and works as expected.
The custom block php looks like:
<?php
if(have_rows( 'slide_setting' ) ) {
$digits = 3;
$randID = rand(pow(10, $digits-1), pow(10, $digits)-1);
echo '<div class="container"><div class="row"><div id="swiper_'.$randID.'" class="col-md-12 wiche-swiper-top-navigation-wrapper">';
echo '<div class="swiper-container wiche-swiper-top-navigation">';
// var_dump( get_fields( get_the_ID() )['page_builder'][2] );
// var_dump( get_post_field( 'auto_play' ) );
// var_dump(get_field('image_slider_settings_auto_play'));
// var_dump(get_row_index());
// var_dump(get_field_objects( $post->ID ));
// var_dump( get_row_index() );
// var_dump( acf_get_field_group( 'slide_setting' ) );
// die();
if ( get_field( 'auto_play' ) ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . get_field( 'auto_play_delay' ) . '" data-swiper-disable-on-interaction="' . get_field( 'auto_play_disable_on_interaction' ) . '">';
} else {
echo '<div class="swiper-wrapper">';
}
while( have_rows( 'slide_setting' ) ) {
the_row();
$title = get_sub_field( 'title' );
$image = get_sub_field( 'image' );
$content = get_sub_field( 'content' );
if ( $image || $content ) {
echo '<div class="swiper-slide swiper-banner-slide swiper-no-swiping">';
if ( $title ) {
echo '<div class="text-center slider-top-title">';
echo $title;
echo '</div>';
}
if ( $image ) {
echo '<div class="banner-image">';
echo wp_get_attachment_image( $image, 'full', '', array( 'loading' => false ) );
echo '</div>';
}
if ( $content ) {
echo '<div class="banner-content">';
echo $content;
echo '</div>';
}
echo '</div>';
}
}
echo '</div>';
echo '</div>';
echo '<div class="swiper-button-next swiper-button-next-outsite">Next</div><div class="swiper-button-prev swiper-button-prev-outsite">Prev</div>';
echo '</div></div></div>';
}
So I wasn't able to get a perfect answer to my question, looks like the API to get what I want doesn't exist (dumb).
What I ended up with - I set up a new function in my theme's functions.php file that looks like the following:
$post_slider_config_index = 0;
function get_the_slider_config( $post_id ) {
global $post_slider_config_index;
$page_builder = get_fields( $post_id )['page_builder'];
$slider_config = null;
foreach ($page_builder as $key => $value) {
if ( $value['acf_fc_layout'] === 'image_slider_settings' ) {
if ( $key > $post_slider_config_index ) {
$slider_config = $value;
$post_slider_config_index = $key;
break;
}
}
}
return $slider_config;
}
And then inside my image-slider.php file I call it like so:
$slider_config = get_the_slider_config( get_the_ID() );
if ( $slider_config[ 'auto_play' ] ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . $slider_config[ 'auto_play_delay' ] . '" data-swiper-disable-on-interaction="' . $slider_config[ 'auto_play_disable_on_interaction' ] . '">';
} else {
echo '<div class="swiper-wrapper">';
}
The $post_slider_config_index variable keeps track of the last index retrieved so that if there are multiple sliders on a page, it'll grab the right one as its rendered.
It's not perfect, it's not super efficient, but it does what I needed. Annoying WP doesn't just give you the information it obviously has already regarding where you are in the page.

Ajax filter not returning page content with filter

I have a special function on some of my pages that returns a modified embed code. It works great:
if (/*is_page() &&*/ has_category('Krajské zprávy')) {
/* get subtitle */
$subtitle = apply_filters( 'plugins/wp_subtitle/get_subtitle', '', array(
) );
if ((strpos($subtitle , 'kraj') == false) && (strpos($subtitle , 'Praha') == false) && (strpos($subtitle , 'Vysočina') == false)) {
$subtitle = "Všechny kraje";
};
/* get page id with embed code */
$id_short = 357;
$tag_id = array (
'Ovzduší' => 357
);
foreach ($tag_id as $k => $v) {
if (has_tag($k)) {$id_short = $v;}
}
/* get embed code & replace */
$acf_kod = get_field('embed_kod', $id_short /*,false*/);
$replace_sub = "<param name='filter' value='Parameters.Kraj=" . $subtitle . "'>";
preg_match_all('/<param [^>]*>/', $acf_kod, $matches);
$m_1 = $matches[0][0]; $m_2 = $matches[0][1];
$pos = strpos( $acf_kod, $m_1) + strlen( $m_1 );
if (strpos($acf_kod,$m_2)-(strpos($acf_kod,$m_1)+strlen($m_1))<2) {
$before = substr ($acf_kod, 0, $pos);
$after = substr( $acf_kod, $pos, strlen( $acf_kod ) );
$whole = $before . $replace_sub . $after;
$content = $whole .'<!--more-->' . $content;
};
/*echo $whole;*/
};
return $content;
};
add_filter('the_content', 'add_embed_parameter');
However, I also have a filter on my site that filters pages by categories and tags and returns them via Ajax. That, too, works just fine - but only with "standard pages" that don't use the code above. For the pages that do, it returns nothing.
This is a snippet of the php code that is used by the filter:
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
/*$content = get_post_field( 'post_content', get_the_ID() );*/
$content = get_the_content (/*get_the_ID()*/);
$content_parts = get_extended( $content );
echo '<h2>' . $query->post->post_title . '</h2>',
$content/*$content_parts['main'] /*'<p>' . $query->post->post_excerpt . '</p>'*/;
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
This is the whole AJAX thing (it's not mine, I'm using the tutorial here):
jQuery(function($){
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('button').text('Processing...'); // changing the button label
},
success:function(data){
filter.find('button').text('Apply filter'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
});
Any idea where the problem might be? Many thanks!

Get all category name of a taxonomy dinamically in wordpress

i have to do a project for a friend and i need to know if It is possibile to retrieve all category name of a taxonomy dinamically in wordpress .If It is possibile how can i do?
Yes, you can do it! First add below codes at the end of function.php file:
function current_cat() {
global $post;
if ( is_page() && $post->post_parent )
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' . '&depth=1' );
else
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' . '&depth=1' );
if ( $childpages ) {
$string = '<ul>' . $childpages . '</ul>';
}
return $string;
}
add_shortcode('currentcat', 'current_cat');
Then you can use it almost anywhere by calling shortcode name:
[currentcat]
Just use this function get_terms()
$taxonomies = get_terms( array(
'taxonomy' => 'taxonomy_name'
) );
if ( !empty($taxonomies) ) :
foreach( $taxonomies as $category ) {
print_r($category);
}
endif;
for more reference https://developer.wordpress.org/reference/functions/get_terms/ , also this tutorial might be help you Show Custom Taxonomy

Looping through ACF in custom post types using functions in php to display the values in page. foreach quits working when placed in the function

I created this code to display the location from ACF that matches part of the URL and it works as expected.
<?php
$myurl= htmlspecialchars($_SERVER["REQUEST_URI"]);
$myexplodes = ( explode ('/', $myurl) );
$posts = get_posts(array(
'post_type' => 'my_vars',
));
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
?>
But when I try to place this code into a function nothing is displayed when I call it.
function local (){
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
}
I suspected that it is a scope problem with the vars but I have tried to make the vars global but had no luck.
The first one probably works because $post is a WordPress global variable, so I advise to use another variable name in your foreach.
For the thing that you want to do you should use also the post id in the get_field function call:
$value = get_field( "location", $article->ID );

How to generate custom page names in wordpress?

I lost my old website's code and am trying to recreate a function I had created ages ago.
I have custom pages in my Wordpress blog: about, work, contact, news as well as posts and subpages.
I am trying to manipulate the value of <h1 class="site-title"><?php bloginfo( 'name' ); ?></h1> to display those different page tiles for those pages and their children so that it would look something like this:
if( $show == 'name' )
{
if( is_page('work') ) $output = 'Work';
if( is_page('contact') ) $output = 'Contact';
if( is_page('about') ) $output = 'About';
}
return $output;
And as for all the rest of the pages, I just want it to display the default site value so somewhere along the line there would be an
else $output = "Site Name";
So I kind of understand the logic because I have done it in the past but cannot remember how the syntax went.
Could anyone refresh my memory?
You could put the following in your functions.php file:
function so20527793_sitename()
{
$output = get_bloginfo( 'name' );
if( is_page( 'work' ) ) $output = 'Work';
if( is_page( 'contact' ) ) $output = 'Contact';
if( is_page( 'about' ) ) $output = 'About';
return $output;
}
Usage:
<h1 class="site-title"><?php echo so20527793_sitename(); ?></h1>
Well I did not get much help in the end so I had to figure it out by googling and combining functions. Here is the result:
function is_child($page_id_or_slug) {
global $post;
if(!is_int($page_id_or_slug)) {
$page = get_page_by_path($page_id_or_slug);
$page_id_or_slug = $page->ID;
}
if(is_page() && $post->post_parent == $page_id_or_slug ) {
return true;
} else {
return false;
}
}
function Custom_PageName(){
$output = get_bloginfo( 'name' );
if (is_page('about') || is_child('about')) $output = 'About title';
if (is_page('work') || is_child('work')) $output = 'Work Page';
if (is_page('contact') || is_child('contact')) $output = 'Contact';
return $output;
}

Categories