I am working on a wordpress plugin. In plugin, a user first add a slider and then add images of relevant slider.
First i make this with shortcode. User enter the short name of the slider in the shortcode and images slides of relevant slider like this [foo_slider slider=slider_one] or [foo_slider slider=slider_two].
Now i "also" want the snippet, that user can add snippet else shortcode in the code like this echo wp_foo_slider(slider_two). But i dont get that.
Please guide me, how can i do this.
Here is my Code That works for shortcode:
<?php
function wp_foo_sliders($atts) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
ob_start();
extract(shortcode_atts(array(
'slider' => '',
), $atts));
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
return ob_get_clean();
}
add_shortcode("foo_slider", "wp_foo_sliders");
?>
I also try this by my own <?php echo wp_foo_sliders("slider_two") ?> or <?php echo wp_foo_sliders(slider_two) ?>, in code and when i refresh the browser only <div class="foo_main_slider"> </div> appears and no images show.
Edit: I want that user can use short code <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> or user can use snippet <?php echo wp_foo_sliders("slider_two") ?>, only shortcode is working, snippet not work.
What i make mistake please help me.
When you call the shortcode function directly, you are passing a string to it. When you use the shortcode way WordPress will convert the arguments into an associative array.
Try refactoring your code
if( is_array( $atts ) ) {
//Called using shortcode so $atts is an array
extract(shortcode_atts(array(
'slider' => '',
), $atts));
} else {
//Function called directly so $atts is a string
$slider = $atts;
}
So i get my solution by my own.
I make a new function for this:
<?php
function foo_sliders($foo_short_name) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
ob_start();
return $foo_short_name;
return ob_get_clean();
}
?>
And in theme code:
<?php foo_sliders(short_name) ?>
and its work great
Related
Inside a template part file there's a variable I'd like to be able to use in my page templates. Page templates are in the theme directory, and the template part file are in a sub-directory called template parts.
My template part file is called 'roster-get-annual-data.php', and the variable is the title of a custom post type. How can I make this title variable available in my page template?
Inside my template file (roster-get-annual-data.php) - within a wp_query:
if ($rosterQuery->have_posts()) {
while ($rosterQuery->have_posts()) {
$rosterQuery->the_post();
$post_title = $post->post_title;
set_query_var('post_title', $post_title);
?>
<h1><?php echo $post_title; ?></h1>
<div class="teams-wrapper">
<?php
$teams = get_field('single_rosters');
foreach ($teams as $post) {
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post);
// set the individual roster's ID
$rosterId = $post->ID;
// get the Class Title
if (!empty(get_field('class', $rosterId)) && !empty(get_field('grad_year', $rosterId))) {
$classTitle = ucwords(get_field('class', $rosterId)) . ' - Class of ' . get_field('grad_year', $rosterId);
} else {
$classTitle = get_the_title();
}
?>
<h2><?php echo $classTitle; ?></h2>
<?php
// create empty $players array
$players = array();
// loop through the players acf repeater
if (have_rows('players', $rosterId)) {
?>
<?php
while (have_rows('players', $rosterId)) {
the_row();
// place players into an array
$players[] = get_sub_field('name');
}
?>
<?php
}
// display the PLAYERS NAMES
?>
<p class="players"><?php echo implode(', ', $players); ?></p>
<?php
}
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
?>
</div>;
?>
And then in my page template:
$post_title = get_query_var('post_title');
get_template_part('template-parts/roster-get-annual', 'data');
I thought this would give me just the $post_title variable, but instead it outputs the entire template without the $post_title variable.
It´s not 100% clear to me what you want to achieve. But if you want pass args to a template you can do it with the 3rd parameter:
get_template_part('template-part', 'name', array('customkey' => 'value') );
see doc:
https://developer.wordpress.org/reference/functions/get_template_part/
I am using wordpress and would like to offer the user the capability to use a shortcode to pull a slider into the post / page.
i.e [slider id="2"]
I have done the following:
Created custom post type 'Sliders'
Used Advanced Custom Fields to create all the fields required within the post type.
Created the shortcode using:
function landwSliderShortcode($atts = [], $content = null, $tag = '')
{
$atts = array_change_key_case((array)$atts, CASE_LOWER);
$slider_atts = shortcode_atts([
'id' => '1',
], $atts, $tag);
return $slider_atts['id'];
}
function landwSliderShortcodes_init()
{
add_shortcode('slider', 'landwSliderShortcode');
}
add_action('init', 'landwSliderShortcodes_init');
I now need to call another function that is going to perform the WP_Query of the custom post type and build the HTML slider... It is this part I am struggling with. I have got this far:
function getSliderData($sliderId) {
$slidercount = 0;
$args = array(
'post_type' => 'slider',
'post_status' => 'publish',
'posts_per_page' => '1',
'p' => $sliderId,
);
$sliderLoop = new WP_Query( $args );
if ( $sliderLoop->have_posts() ) :
while ( $sliderLoop->have_posts() ) : $sliderLoop->the_post();
$image = get_sub_field('image');
$image_render = $image['sizes'][$image_asset_size];
$add_link = get_sub_field('add_link');
$link_type = get_sub_field('link_type');
$internal_link = get_sub_field('internal_link');
$external_link = get_sub_field('external_link');
$add_lightbox = get_sub_field('add_lightbox');
$lightboxasset = get_sub_field('lightbox_asset');
$lightboxassetcaption = get_sub_field('lightbox_caption');
$caption = get_sub_field('caption');
$sub_caption = get_sub_field('sub_caption');
?>
<div class="slider-wrap">
<?php if ($add_link) {
if ($link_type == "External") {
echo '<a class="carousel-slide-link" href="'.$external_link.'" target="_blank"></a>';
} else {
echo '<a class="carousel-slide-link" href="'.$internal_link.'"></a>';
}
}
?>
<?php if ($add_lightbox) { ?>
<a class="carousel-slide-link" data-fancybox data-src="#SliderModal<?php echo $slidercount ?>" target="_blank" href="javascript:;"></a>
<?php } ?>
<img src="<?php echo $image_render; ?>">
<!-- 6 Mar 17 added conditional to show the caption -->
<?php if (strlen($sub_caption) > 0 || strlen($caption) > 0) : ?>
<div class="post-wrap-meta">
<span><?php echo $sub_caption; ?></span>
<p class="slider-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif ?>
</div>
<!-- output the modal -->
<div style="display: none;" class="standard__modal" id="SliderModal<?php echo $slidercount; ?>">
<?php echo $lightboxasset ?>
<p class="slider-caption-text">
<?php echo $lightboxassetcaption ?>
</p>
</div>
<?php
$slidercount ++;
endwhile;
?>
<?php
endwhile;
wp_reset_postdata();
endif;
}
I hope to be able to pass the sliderId var to this function and return a fully functioning slider (there is a small Javascript function I need to add to this HTML as well).
Many thanks in advance to anyone whole can help with this.
Instead of using WP_Query, ->have_posts(), while, wp_reset_postdata, use a simple get_posts($args) and do a for loop over the results.
- - see When should you use WP_Query vs query_posts() vs get_posts()?
And instead of echo, build a HTML string that will be returned to the shortcode. WordPress shortcodes should never echo anything, they expect a string to be returned. PHP heredoc is perfect for that, example:
function getSliderData($sliderId) {
$some_var = 'Value of the var';
$html = <<<HTML
<div>$some_var</div>
<h3>$post->title</h3>
HTML;
return $html;
}
Important: the closing HTML; must not have white spaces before or after.
Ideally, you'll build the $html bit by bit, making sure it's returning the perfect markup. You can use $html .= to compose the string in more than one operation.
I'm using a dynamic template for a custom made CMS. I want to get variablesfrom one php-page and, for each created site that uses that page, I want to use the variables in a foreach to, for example display each title.
I have made a simple example below to explain this:
home.com/gallery/1
home.com/gallery/2
home.com/gallery/3
What I get:
Gallery 1
Gallery 1
Gallery 1
What I want:
Gallery 1
Gallery 2
Gallery 3
(Assuming that each page was named Gallery 1, 2, 3)
gallery.php
<form action="">
<input type="text" name="page_title">
</form>
<?php
$galleries = array();
$id = intval($_POST["id"]);
? foreach ($galleries as $id => $gallery) {
$title = $_POST["page_title"];
}
$_SESSION['galleries'] = $galleries;
$_SESSION['title'] = $title;
?>
<h1><?php echo $title; ?></h1>
page.php:
$galleries = $_SESSION['galleries'];
$title = $_SESSION['title'];
foreach ($galleries as $id => $gallery) {
echo $title;
echo "<br>";
}
?>
Note: because I want to use the variables on multiple pages I can't assign the form's action to a specific php-page.
Ok, I just found the sollution:
page.php:
$galleries = $_SESSION['galleries'];
$title = $_SESSION['title'];
foreach ($galleries as $id => $gallery) {
echo $gallery["title"]; //echo this line instead of $title
echo "<br>";
}
?>
Can't figure out what I'm doing incorrectly here
this is at the top of the template file
<?php
/**
* #package 1
* #since 1 1.0
*/
$source_name = get_post_meta($post->ID, 'Source Name', true);
$source_url = get_post_meta($post->ID, 'Source URL', true);
?>
here is the other part thats further down:
<?php if($source_url) { ?>
<div id="content-source">
<span>Source:</span> <?php echo $source_name; ?>
</div>
<?php } ?>
If I remove <?php if($source_url) { ?> and <?php } ?> it works fine, but how do I get it to work so if theres no source nothing will be displayed?
A quick look-up of the get_post_meta() function:
If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.
So, try:
<?php if($source_url <> "") { ?>
<div id="content-source">
<span>Source:</span> <?php echo $source_name; ?>
</div>
<?php } ?>
Previous you were checking if nothing is returned. You need to be checking for an empty string instead.
Hello I work on WordPress and I have a premium theme which is not supported from the creator anymore so I am trying to make a change; I am not sure that this is called a bug. On comments when a visitor is place a comment; his avatar is not the default 'mystery' in this case (no_avatar.gif) but it automatically takes the avatar of the current post author. I like to shows the default for unregistered users. I would like to notice that the theme is use it's own avatars. I target the code on functions but I am not able to change it, any help would be appreciated, thanks in advance here is the code:
function tgt_get_avatar_link($user_id = ""){
if (!empty($user_id)){
$avatar = get_the_author_meta('tgt_image', $user_id);
}
else
$avatar = get_the_author_meta('tgt_image');
if (!$avatar){
return TEMPLATE_URL . '/images/no_avatar.gif';
}
return TEMPLATE_URL . $avatar;
}
Update: The function is calling tgt_get_avatar_link but it seems that is:
function tgt_ad_comment($comment, $args, $depth){
$GLOBALS['comment'] = $comment;
global $helper;
?>
<li>
<div class="comment" id="comment-<?php comment_ID()?>">
<?php //echo get_avatar($comment) ?>
<?php echo $helper->image(tgt_get_avatar_link($comment->user_id), $comment->comment_author, array('title' => $comment->comment_author, 'width' => '58px', 'height' => '58px')) ?>
<div class="comment_content">
<strong><?php echo get_comment_author_link() ?></strong> <?php _e('Say ','ad')?>(<?php comment_time('F j, Y \a\t g:i a') ?>)
<br/>
<?php comment_text() ?>
</div>
</div>
The function is calling $user_id but it seems that is:
function tgt_get_avatar_link($user_id = ""){
if (!empty($user_id)){
$avatar = get_the_author_meta('tgt_image', $user_id);
}
else
$avatar = get_the_author_meta('tgt_image');
if (!$avatar){
return TEMPLATE_URL . '/images/no_avatar.gif';
}
return TEMPLATE_URL . $avatar;
}
If the comment author has no defined avatar, you'd like to show a default image. This seems to be the else in your conditional:
else { $avatar = get_the_author_meta('tgt_image'); }
Without a $user_id to pass to get_the_author_meta, it assumes the user ID of the current post author. This is likely causing the behavior you describe.
Try to remove the else statement, skipping straight from if (!empty($user_id)){$avatar = get_the_author_meta('tgt_image', $user_id);} to if (!$avatar){.... Now, when the function is passed a null user_id, it should return TEMPLATE_URL . '/images/no_avatar.gif';