Passing values to wordpress shortcode - php

<?php while (have_rows('home_playlist', 'option')): the_row();
$track = get_sub_field('home_track'); ?>
echo $track;
Here echo generate some selective IDs 2,4,7,10,12 (mp3 songs title) from wp panel. And I need to show the title on the player. How do i pass the value of $track into the 'ids'....

<?php
$tracks=array();
while (have_rows('home_playlist', 'option')): the_row();
$tracks[] = get_sub_field('home_track'); ?>
$list= implode(',',$tracks);
echo $list;
mp3j_put( '[popout ids="'.$list.'" autoplay="y"]' ); ?>

Related

How to make a template variable global?

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/

Restart foreach loop in PHP

I have an array with products as objects in it. Each object contains a key = category_title
I have a foreach loop that generates products in Bootstrap rows with configurable columns, in my case $col=3.
What I need is to have a new row if a new category_title comes up.
I created an array to push all the category_titles in it and create a variable if it is a new category. But now I am stuck.
The foreach looks like this:
<?php foreach($productspercat as $product): ?>
<!-- Make sure product is enabled and visible #front end -->
<?php // if($product->enabled && $product->visibility):?>
<?php
$catnew = false;
$categorytitle = $product->category_title;
if(!in_array($categorytitle, $totalcategories, true)):?>
<?php array_push($totalcategories,$categorytitle );
$catnew=true;
?>
<?php endif;?>
<div class="j2store-products-row <?php echo 'row-'.$row; ?> row">
<?php endif;?>
<div class="col-sm-<?php echo round((12 / $col));?>">
<h2><?php echo $categorytitle; ;
var_dump($catnew);
?></h2>
test
</div>
<?php $counter++;
$firstrun = true; ?>
<?php if (($rowcount == $col) or ($counter == $total)) : ?>
</div>
<?php endif; ?>
<?php // endif; ?>
<?php endforeach;?>
So basically if a new category comes up, it should close the row and start a fresh row with a new column count.
appreciate any help!
If you don't want to page refresh then you can do that by using jQuery or Angular JS.
By jQuery you can add data by jquery prepend() function if you wanna add data in starting or append() function to add ending section.
<?php
// Arranged products by category title
$arrangedProducts = array()
foreach($productspercat as $product) {
if (!array_key_exists($product->category_title, $arrangedProducts)) {
$arrangedProducts[$product->category_title] = array();
}
$arrangedProducts[$product->category_title][] = $product;
}
// each category is a key and its value is an array with all its products
foreach ($arrangedProducts as $categoryTitle => $products) {
// you create a new row
// ....
foreach ($products as $product) {
// you display product information
}
// you end the row
}
?>

How to get list of current wordpress categories and inject each in script

I'm trying to create php code that will get the list of categories that are assigned to a wordpress post (of the current page) and then loop through the array list and each category that IS has been assigned to the post and add it to the script.
this is the script i need to inject the category name in:
<script> window.abkw = '<category name>';</script>
so i wrote this but it does not seem to work...
<?php
$ads = the_category();
for ($i = 0; $i < count($ads); ++$i) {
$d = $ads[$i];
<script> window.abkw = $d[$i];</script>
}
?>here
I'm new to php so please excuse my ignorance... and thank you so much.!
use get_categories() (https://developer.wordpress.org/reference/functions/get_categories/)
$categories = get_categories(array('hide_empty' => 0'));
foreach($categories as $category) {
echo '<script> window.abkw = '.$category->cat_name.';</script> ';
}
What if you use jquery data()...
https://api.jquery.com/data/#data-html5
--edited--
I expand the way
template-example.php
...
<?php
$categories = get_categories(array('hide_empty' => 0'));
foreach($categories as $category): ?>
<div class="js-category" style="visibility:hidden;" data-category = "<?php echo $category->cat_name ?>"></div> //invisible div
<?php endforeach; ?>
...
file.js
var categories = $(".js-category").data("category");
...

Dynamically create variables in foreach loop and use on another page

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>";
}
?>

How would I code this for wordpress?

Hello I am fairly new to wordpress.
I was wondering if you guys have any idea on how to display post from wordpress and tweets
all in the same 'loop'.
I know It is possible using a if statement that displays a tweet ever nth-wordpress post. But I would like for both the tweets and the posts to be displayed by date in the same page. any ideas?
so far this is what my page looks like
<?php
get_header();
if(have_posts()) {
while(have_posts()) {
the_post();
$post_link = get_permalink(get_the_ID());
//($beforeString, $afterString, T/F)
//T/F retun in html-T or in php-F
the_title('<h1>', '</h1>');
the_content('<p>', '<p>');
//echo <a href="get_permalink()"
}
}
get_footer();
?>
I'd try to get both sources into one array, then sort it and print it out:
<?php
get_header();
//define array
$entrys=array();
//get wp-posts
if(have_posts()) {
while(have_posts()) {
the_post();
$timestamp=//timestamp of post
$entries[$timestamp]=array(
'link'=>get_permalink(get_the_ID()),
'title'=>get_the_title(get_the_ID()),
'content'=>get_the_content()
);
}
}
//insert the twitter-data into the same array here
//sort array
ksort($entries);
//print array
foreach($entries as $e){
echo '<div class="entry"><h1>'.$e['title'].'</h1>';
echo '<p>'.$e['content'].'</p>';
echo 'Link</div>';
}
get_footer();
?>

Categories