Wordpress dynamic widget content - wordpress

Is there any way to make the sidebar widget of wordpress to be dynamic? like for example, i would have a twitter widget and it will accept some variables like [ page ] or [ tags ] that will be all different according to the page.

This question is on the one hand too broad and on the other too narrow . To really answer your question a simple "yes" would do . Please try to put some actual code or actual question next time .
Having said that :
Yes, It is possible very Easy to do in the widget code using the conditional tags of wordpress.
Actually, This is the nature of the widgets - to be dynamic and easily customizable .
All you have to do is set your condition on the widget code ( function widget() inside the widget class ).
if ( is_front_page() ) {
$output = 'x'; // content only for front page
} else if ( is_single( array( 17, 19, 1, 11 ) ) ) {
$output = 'y'; // content only ID 17,19,1,11
} else {
$output = 'z'; // content only for all the rest not stated above
}
Refer to the codex page here for general and and to the widgets API
EDIT I after comment
It is up to you to decide which condition mechanism to use . The logic is still exactly the same . may it be if/else, switch , ternary or whatever. If you think about it - what you describe in words in your question, is a 'condition'. in computer logic ( and php ) it is done with conditional statements . and wordpress provides you with an easy mechanism for that . all you have to do is change the $output for whatever you want. tweets about cats, dogs, salmon fishes ,carrots or raspberry pie are exactly the same and you can filter them as such using the page slug for example .
But you will still need conditions. ( also filtering by page slug is a type of condition )
How to do that exactly will depend a lot on what mechanism you use in the specific case to retrieve tweets. Which brings me to my first point of you not posting any details or code .

Related

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Hide Widget based on condition using Widget Logic Plugin

My website (http://www.chicagokaraokenight.com/wordpress) is a karaoke directory where bars can list karaoke nights for free OR upgrade to a premium or featured premium listing (paid packages) to receive a more robust profile.
As part of the benefit to upgrading to a paid package, I'd like to have some widgets disappear on the paid listings.
My theme author (Listify) recommended the plugin Widget Logic and said the following:
Use https://wordpress.org/plugins/widget-logic/ to show/hide a widget depending on certain criteria.
Using something like:
wc_paid_listings_get_user_package( $package_id )
I'm still a little unclear on exactly what I should edit the logic to say and hoping someone can help. I know how to get the package ID's (if I hover over the packages on the Product page I can see them).
The widgets I wish to hide based on a listing being a paid package are Google ads, recent listings, and featured listings.
Thanks!
UPDATE: The WC Paid Listing Plugin developer has given me this code and info after I mentioned the Free package ID is 971:
global $post;
$used_package = get_post_meta( $post->ID, '_package_id', true );
if ( 971 === $used_package ) {
// Free
}
You could wrap this in a custom function to use in widget logic:
function job_was_posted_with_package( $package_id ) {
global $post;
$used_package = get_post_meta( $post->ID, '_package_id', true );
return $package_id == $used_package;
}
Called via:
job_was_posted_with_package( 971 );
Do I have what I need now? Can someone help me identify what needs to go into functions.php, what should go in Widget Logic, etc?
Widget Logic is pretty straight forward in form of inner workings.
I assume you have a widget already created what you would need to do is make or use a function that returns a Boolean
function is_paid_member(){
// Verification Code here
return TRUE;
}
Then place is_paid_member() in the widget logic field of the affected Widget.
If you can print some code for the verification method i can most likely edit the Answer to make it workable.
Please clarify the following, is the Widget always going to be available in the Administrative interface? If yes then it would go outside of the Widget Logic scope which affect mostly activated Widgets displaying on the frontend.
Per Mike Jolley from WP Paid Listings, the correct code is:
function job_was_posted_with_package( $package_id ) {
global $post;
$used_package = get_post_meta( $post->ID, '_package_id', true );
return $package_id == $used_package;
}
which should be placed in the functions.php file.
And then:
( ! job_was_posted_with_package( ID ) && ! job_was_posted_with_package( ID ) )
or
job_was_posted_with_package( ID )
should be the condition entered into Widget Logic on the widget you want to show/hide.

Wordpress - Portfolio - Changing Title Length?

I've Googled my fingers off but just can't find the solution! I'm using a Wordpress Child Theme (on top of Cherry) and simply want to change the length of the Title used and displayed in the Portfolio. It is the default portfolio, not a plug in.
Unfortunately I'm not allowed to provide links due to confidentiality! Any help appreciated.
Neil.
EDIT - Well I'm not a developer so apologies for my clear lack of detail. I don't know which part of functions.php controls this post. I can see it loads theme-portfoliometa.php which has what looks like some likely code but I can't read it well enough to know what's what.
I can manage CSS/HTML but not php. I can provide a link via PM if anyone has the patience to help.
So far, I inserted a php script I found on another post into my-functions.php (child theme) but that changed the excerpt length of the news (posts). I've tried looking for '35' in all php files also which is the current limit.
$the_title = esc_attr ( the_title_attribute ( 'echo=0' ) );
$max_length = 50;
$title_length = strlen ( $the_title );
if ( $title_length > $max_length ) {
$title_excerpt = substr ( $the_title, 0, $max_length ) . '...';
} else {
$title_excerpt = $the_title;
}
This will truncate the title to 50 characters if it is larger and append ...
If the title is <= 50 characters it will just display as normal.

Wordpress menu items order

basically I have a three-level menu in wordpress and I've got the following code in the front-end to call the third-level menu:
$children = get_pages('child_of='.$include_page_ids[$i]);
if (count($children) > 1) {
$sub = "<ul>";
foreach ($children as $child){
$sub .= "<li><a href='#$child->post_title'>";
$sub .= $child->post_title;
$sub .= "</a></li>";
}
$sub .="</ul>";
echo $sub;
}
This calls a list for the children of a certain page and also makes the the anchors (which I also need). The problem is that right now they are being displayed in an alphabetical order, but I need to be able to set the right order myself (ie to be the same as in the backend menu). Please hepl me with it, how can I achieve this? For example this is the page http://www.eboxlab.net/transbeam/support/support/, youcan see the third level-menu as the box right next to the banner (Acceptable Use Policy to Terms & Conditions). THe order of the blocks which it corresponds to is right, but the menu is alphabetically ordered.
Help really appreciated.
PS: if you need I can provide the template code
It's pretty simple actually. Here is what your $children call should look like:
$children = get_pages('child_of=' . $include_page_ids[$i] . '&orderby=menu_order&sort_order =ASC');
That's all you need to add - this tells the query to order pages in ascending fashion by their "menu_order" column(or the "Order" field under Page attributes). You can see more details on the get_pages function at Function Reference/get pages
wp_nav_menu to the rescue! Assuming you're using WP 3 or newer, it will let you wrap elements in whatever markup you'd like, and will correspond to however you've configured the menu in the admin Dashboard.

Drupal Displaying the Vote up down widget 2.x

I'm using the drupal vote up down module and a module which overrides the node display, leading me to have to put the vote up down into a block (as opposed to having it be automatically rendered).
Here is an issue with a snippit on how to do this in 1.x http://drupal.org/node/544354
Unfortunately 2.x is totally different. Does anyone know what function I should use? Thanks.
It's depend where to you want to show: node, comment, term?
if node, see vote_up_down\vud_node\vud_node.module file:
function vud_node_nodeapi().
Copy to your block one of these themed function, like this:
...
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$tag = variable_get('vud_tag', 'vote');
$widget = variable_get('vud_node_widget', 'plain');
$output .= theme('vud_widget', arg(1), 'node', $tag, $widget);
}
...
print $output;

Resources