PHP via shortcode always on top of page - php

I got a plugin for wordpress to show the most popular posts...
But when I add it via shortcode it always gets to the top of the page and not where I placed it... I changed every echo in the plugin PHPs to return but it didn't helped... Here is my shortcode in functions.php:
function top_news(){
$args = array(
'limit' => 15,
'range' => 'daily',
'freshness' => 1,
'order_by' => 'views',
'post_type' => 'post',
'stats_views' => 1,
'stats_author' => 1,
'stats_date' => 1,
'wpp_start' => '<table class="topnachrichten"><tr><th>Datum</th><th>Top Nachrichten von Heute</th><th>Leser</th></tr>',
'wpp_end' => '</table>',
'stats_date_format' => 'd',
'excerpt_by_words' => 1,
'excerpt_length' => 35,
'title_length' => 66,
'post_html' => '<tr><td class="datum">{date}. Aug</td><td class="stext"><details>
<summary>{title}<span class="plus">+</span></summary>{summary}<br>Weiterlesen</details></td><td class="views">{views}</td></tr>'
);
wpp_get_mostpopular( $args );
return $args;
}
add_shortcode( 'topnews', 'top_news' );
Do you know what I can do?
Thanks,
Till

Reading documentation of wpp_get_mostpopular, it states that the function actually prints the popular posts. Which means your popular posts are printed out before it returns anything and since all shortcodes are processed before the posts content is being printed that's why your popular posts always prints before (at top) of the post content.
So, what you can do is catch all the popular posts in a buffer.
function top_news(){
$args = array (
'limit' => 15,
'range' => 'daily',
'freshness' => 1,
'order_by' => 'views',
'post_type' => 'post',
'stats_views' => 1,
'stats_author' => 1,
'stats_date' => 1,
'wpp_start' => '<table class="topnachrichten"><tr><th>Datum</th><th>Top Nachrichten von Heute</th><th>Leser</th></tr>',
'wpp_end' => '</table>',
'stats_date_format' => 'd',
'excerpt_by_words' => 1,
'excerpt_length' => 35,
'title_length' => 66,
'post_html' => '<tr><td class="datum">{date}. Aug</td><td class="stext"><details>
<summary>{title}<span class="plus">+</span></summary>{summary}<br>Weiterlesen</details></td><td class="views">{views}</td></tr>'
);
ob_start();
wpp_get_mostpopular( $args );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'topnews', 'top_news' );

Related

PHP var_export - elgg_get_entities

I am trying to create an API for the social network Elgg. It's working but I am very puzzled by some results.
This is my API:
function my_get_group() {
$group=elgg_get_entities(array(
types => 'group',
guid=>75,
));
return var_export($group[0], true);
}
and this is the reult
{
status: 0
result: "ElggGroup::__set_state(array( 'url_override' => NULL, 'icon_override' => NULL, 'temp_metadata' => array ( ), 'temp_annotations' => array ( ), 'temp_private_settings' => array ( ), 'volatile' => array ( ), 'tables_split' => 2, 'tables_loaded' => 2, 'attributes' => array ( 'guid' => 75, 'type' => 'group', 'subtype' => 0, 'owner_guid' => 54, 'site_guid' => 1, 'container_guid' => 54, 'access_id' => 2, 'time_created' => 1442385556, 'time_updated' => 1442385557, 'last_action' => 1442455132, 'enabled' => 'yes', 'name' => 'goup1', 'description' => '<p>description1</p>', ), 'valid' => false, ))"}
So far so good.
I can now modify my APIto get only the description, by changing
//return var_export($group[0], true);
return var_export($group[0]->description, true);
which gives me
{
status: 0
result: "'<p>description1</p>'"
}
Now the part that I don't understand is the following. if I modify my code to:
//return var_export($group[0], true);
return var_export($group[0]->briefdescription, true);
I get
{
status: 0
result: "'brief description1'"
}
Can someone explain me where this data comes from? I cannot see any mention of briefdescription when I run
return var_export($group[0], true);
Thanks for your help
bonododo
All of those values come through PHP magic methods __get and __set. The reason you see description in var_export result is that it's an attribute, the briefdescription is a metadata.
Have a look at documentation, especially:
Data model overview
Metadata section
ElggGroup-specific attributes

ACF Update Field into nested repeater field

I'm trying to let users post from the front end into a nested ACF repeater field.
I've got the sub fields in the first repeater posting fine. But, I can't quite figure out how to get the nested repeater working properly.
Here's the code I have so far:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '09:00', // would be dynamic
'end-time' => '17:00' // would be dynamic
)
);
update_field($event_field_key, $events, $post_id);
I'm not sure if I can just nest another array in there, or if I need to do something else.
[UPDATE]
I've just done this and it does input into the first row:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '9:00',
'end-time' => ' 17:00'
)
);
update_field($event_field_key, $events, $post_id);
However, this code puts row 1 values both as 9 and row 2 values as 1.
So it looks like:
Row 1: start time: 9, end time: 9
Row 2: start time: 1, end time: 1
I can't seem to find any documentation on this, but it looks like it's possible, just a case of figuring out the syntax.
The fix was an array of arrays:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
array(
'start-time' => '09:00',
'end-time' => '17:00'
),
array(
'start-time' => '10:00',
'end-time' => '16:00'
)
)
);
update_field($event_field_key, $events, $post_id);

Trouble including array output in another array

I have the following code:
$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;
Which echos out as...
10,121,20,36,23,66,24,21,105,76,82,17,22,122,43,47,102,5,6,106,8,75,54,38,57,86,56,101,123,95,25,62,16,39,40,69,37,9,42,7,15,41,87,73,85,104
This is great. However, I actually want to include the result (which I guess is $tagString) in another array as follows...
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $paged,
'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
'tag__not_in' => array ($tagString)
);
I've tried removing brackets adding single / double / no quotes, removing the word 'array' in front of $tagString across all combinations but it just doesn't work. When I manually create...
'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)
The code works perfectly. How can I get the output from $tagString to be the content of 'tag__not_in' array within the brackets? Is this possible?
========
Update to reflect Amal's code...
$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $paged,
'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
/*'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)*/
'tag__not_in' => explode(',', $tagString)
);
$tagString is a string - simply inserting it in array(...) won't create an array (unless you use eval(), which is generally a bad idea). Simply use explode() to create an array from the comma-separated string, like so:
'tag__not_in' => explode(',', $tagString)

How to reorder this array?

I have a database table as follows:
This returns all column titles in the pic, but the one's that are most important are slug, and parent (not sure about id_button).
The array gets ordered automatically by id_button ASC, which really irks me. But, anyways, this is not important, as I need to order it completely different, or re-order it after the array is populated.
The array returns this, by order of id_button:
$new_menu_buttons = array(
0 => array(
'id_button' => 1,
'parent' => 'help',
'position' => 'child_of',
'slug' => 'testing',
),
1 => array(
'id_button' => 2,
'parent' => 'packages',
'position' => 'after',
'slug' => 'sub_test_1',
),
2 => array(
'id_button' => 3,
'parent' => 'google.com',
'position' => 'after',
'slug' => 'another_test',
),
3 => array(
'id_button' => 4,
'parent' => 'testing'
'position' => 'child_of',
'slug' => 'google.com',
)
);
I need to order it so that if a slug is found within any parent, than the slug that is in the parent needs to be loaded before the one that has it defined within the parent.
Its not important if it is directly before it. For example, you see testing is the first slug that gets returned, and yet the parent for this is the last slug (google.com). So as long as the slug row where the parent is defined gets ordered so that it is BEFORE the row that has the slug value in the parent column, everything is fine.
So in this situation, it can be reordered as any of these 3 ordered arrays below:
$new_menu_buttons = array(
0 => array(
'id_button' => 1,
'parent' => 'help',
'position' => 'child_of',
'slug' => 'testing',
),
1 => array(
'id_button' => 2,
'parent' => 'packages',
'position' => 'after',
'slug' => 'sub_test_1',
),
2 => array(
'id_button' => 4,
'parent' => 'testing',
'position' => 'child_of',
'slug' => 'google.com',
),
3 => array(
'id_button' => 3,
'parent' => 'google.com'
'position' => 'after',
'slug' => 'another_test',
)
);
OR this...
$new_menu_buttons = array(
0 => array(
'id_button' => 1,
'parent' => 'help',
'position' => 'child_of',
'slug' => 'testing',
),
1 => array(
'id_button' => 4,
'parent' => 'testing',
'position' => 'child_of',
'slug' => 'google.com',
),
2 => array(
'id_button' => 2,
'parent' => 'packages',
'position' => 'after',
'slug' => 'sub_test_1',
),
3 => array(
'id_button' => 3,
'parent' => 'google.com'
'position' => 'after',
'slug' => 'another_test',
)
);
OR even this...
$new_menu_buttons = array(
0 => array(
'id_button' => 1,
'parent' => 'help',
'position' => 'child_of',
'slug' => 'testing',
),
1 => array(
'id_button' => 4,
'parent' => 'testing',
'position' => 'child_of',
'slug' => 'google.com',
),
2 => array(
'id_button' => 3,
'parent' => 'google.com'
'position' => 'after',
'slug' => 'another_test',
),
3 => array(
'id_button' => 2,
'parent' => 'packages',
'position' => 'after',
'slug' => 'sub_test_1',
)
);
All 3 of these ordered arrays will work because the array with the slug that matches the parent is before the array with the matching parent, and since the slug value, sub_test_1 doesn't match any of the parent values this array order is unimportant, so that array can be located anywhere within the array.
How can I do this? I'm thinking of just looping through the array somehow and trying to determine if the slug is in any of the parents, and just do a reordering somehow...
In short, the slug needs to be ordered before the parent ONLY if there is a parent that matches a slug within the array. Otherwise, if no match is found, the order isn't important.
As Niko suggested, databases support powerful sorting functionality, so you normally can best solve this by telling the database in which order to return the data. If the data is queried with SQL, that's the ORDER BY clause. This is specified in the documentation of your database, assuming you're using MySQL 5.0: http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
If you can not influence the order on the database level, you're in the need to sort the array in PHP. You actually have an array of arrays, in which the outer array is just a list having the id (primary key) of each row and the other fields as a fieldname -> value array as a value (inner array).
Your sort is *user-defined` - you specify the sort order. A common way is to have a sort function that compares two entries which each other. That sort function needs to decide which of those two is of a higher sort-order than the other (or both have the same weight). In you case one item is higher than the other if one is the child of the other.
That's the general principle. You define the sort function that decides (the so called callback function), and PHP takes care to feed it with the array data to sort with the usortDocs function.
A sub-problem you need to solve then is to decide whether or not a child exists in the whole array (an item with a slug having the same value as parent). As this all looks like it can be a bit more complex, it's wise to encapsulate this all into a class of it's own.
Example / Demo:
class menuButtons
{
/**
* #var array
*/
private $buttons;
public function __construct(array $buttons)
{
$this->buttons = $buttons;
}
public function sortChildsFirst()
{
$buttons = $this->buttons;
usort($buttons, array($this, 'sortCallback'));
return $buttons;
}
private function sortCallback($a, $b)
{
// an element is more than any other if it's parent
// value is any other slugs value
if ($this->slugExists($a['parent']))
return 1;
return -1;
}
private function slugExists($slug)
{
foreach($this->buttons as $button)
{
if ($button['slug'] === $slug)
return true;
}
return false;
}
}
$buttons = new menuButtons($new_menu_buttons);
$order = $buttons->sortChildsFirst();
Note: This code is exploiting the fact that your sort order is only roughly specified. You only wrote that you need to have children before parents, so if you take all children first, this will always be the case. It's not that each parent will directly follow the child.
Nevertheless, this skeleton class can work as a base to further improve the search functionality as it's fully encapsulated. You can even change the whole sort method, e.g. to completely write one of your own even w/o usort, like outlined below. The main code does not need to change as it's only making use of the sortChildsFirst method.
You can sort an array once populated using the usort() function.
http://php.net/manual/en/function.usort.php
Since your structure is tree-alike, the first thing that comes to mind is to build a tree out of it. It goes like this:
$tree = array();
foreach($array as $e) {
$p = $e['parent'];
$s = $e['slug'];
if(!isset($tree[$p]))
$tree[$p] = new stdclass;
if(!isset($tree[$s]))
$tree[$s] = new stdclass;
$tree[$s]->data = $e;
$tree[$p]->sub[] = $tree[$s];
}
This creates a set of objects, with the members data and sub = list of child objects.
Now we iterate the tree and for each "root" node, add it and its children to the sorted array:
$out = array();
foreach($tree as $node)
if(!isset($tree[$node->data['parent']]))
add($out, $node);
where add() is
function add(&$out, $node) {
if(isset($node->data))
$out[] = $node->data;
if(isset($node->sub))
foreach($node->sub as $n)
add($out, $n);
}
hope this helps.
Ok, first let me thank you all for your detailed explanations. They are very intuitive. However, I found another way, can you guys let me know if you spot anything wrong with this method here please?
Click here to see a Demo of this working!
$temp_buttons = array();
foreach($new_menu_buttons as $buttons)
$temp_buttons[$buttons['parent']] = $buttons['slug'];
dp_sortArray($new_menu_buttons, $temp_buttons, 'slug');
// The $new_menu_buttons array is now sorted correctly! Let's check it...
var_dump($new_menu_buttons);
function dp_sortArray(&$new_menu_buttons, $sortArray, $sort)
{
$new_array = array();
$temp = array();
foreach ($new_menu_buttons as $key => $menuitem)
{
if (isset($sortArray[$menuitem[$sort]]))
{
$new_array[] = $menuitem;
$temp[$menuitem['parent']] = $menuitem['slug'];
unset($new_menu_buttons[$key]);
}
}
$ordered = array();
if (!empty($new_array))
{
foreach ($new_array as $key => $menuitem)
{
if (isset($temp[$menuitem[$sort]]))
{
$ordered[] = $menuitem;
unset($new_array[$key]);
}
}
}
else
{
$new_menu_buttons = $new_menu_buttons;
return;
}
$new_menu_buttons = array_merge($ordered, $new_array, $new_menu_buttons);
}
Seems to work in all instances that I tested, but ofcourse, their could be a flaw in it somewhere. What do you all think of this?

Pagination without default order

I have the code below:
$firm_ids = array(81, 96, 18, 5, 105);
$this->paginate = array(
'conditions' => array('Firm.id' => $firm_ids),
'limit' => 10,
);
$this->set('firms', $this->paginate('Firm'));
In the results I have the ordering in:
5, 18, 81, 95, 105
How do I disable the default order if I want to order as initial array ordering?
I found a workaround on some forum, see if this works:
$this->paginate = array(
'conditions' => array('Firm.id' => $firm_ids),
'limit' => 10,
'order' => array(
'FIELD(Firm.id,' . implode(',',$firm_ids) . ')'
)
);
what you need is order by field. here's the link.
Order By Field

Categories