Wordpress basics - php

Firstly, I'm a total beginner in this business but I decided to learn some php and now I'm trying to follow wordpress course .. which is by the way 5 years old. Anyway, I did the following code and it worked fine but vs code editor is saying that I am trying to use unknown function PHP(PHP0417).
The code is like this and according to vscode have_posts(), the_title(), the_content() is unknown function
Is the course outdated or what is going on?
<?php
while(have_posts( )) {
the_post(); ?>
<h2><?php the_title();?></h2>
<?php the_content();?>
<hr>
<?php }
?>

Related

PHP content does not switch to other language anymore (Polylang)

Hello I m using Polylang and this php code to switch my slider (I did with Smart Slider) to english-french. I don't know anything about PHP but I got it on a forum. It was working perfectly until yesterday.
http://www.chooseyourtelescope.com/
<?php
$currentlang = get_bloginfo('language');
if($currentlang=="fr_FR"):
?>
<div>
<?php
echo do_shortcode('[smartslider3 slider=6]');
?>
</div>
<?php else: ?>
<div>
<?php
echo do_shortcode('[smartslider3 slider=4]');
?>
</div>
<?php endif; ?>
Now it stays in english. I dont think the problem is coming from Smart Slider. I tried to deactivate the other plugins and to restore the previous theme and Polylang versions but nothing changed.
BTW my site is responsive and the slider is not displaying under 800px width. So you can't see the problem on mobile for example.
Instead of doing it with php I resolved the problem with css: I put both english and french sliders in my header template with different classes and "display:none" the slider I don't want for each frontpage:
.page-id-94 .smartslider-en {
display:none;
}
.page-id-7386 .smartslider-fr {
display:none;
}
Please answer if you find a solution with php only.
Here s the solution :
<?php if(get_locale() == 'fr_FR') : ?>
Actually the "get_bloginfo('language')" function stopped working since I upgraded to WordPress 4.6 and to Polylang 2.0.3.

Wpml and Custom Widgets

In WPML some times custom widgets developed could not get translated.
Any string is translatable if the you copy paste the string then that string if searchable in WPML is translatable.
I have a website that has 4-5 Custom widgets + some hard coded part
For example there was a string in hard coded part-
Bet on your favorite sports online
I made it WPML compatible-
WPML version
N.B. Before posting the question here I have invested around 3-4 hours in understanding all the FAQ's at wpml.org site.
My problem-
I have various widgets, one such widgets has this portion-
<div class="signupoffers">
<div class="signupText"> <h2><?php echo $signup_text_title?></h2>
<p><?php echo $signup_text_content?></p><br>
</div>
<div class="signupbuttonDiv">
<div class="RegisterBlock">
<div class="block"><h2><?php echo $signup_btn_title?></h2>
<div>
<?php echo $signup_btn_content; ?>
</div>
</div>
</div>
<?php //nsu_signup_form();?>
</div>
</div>
Lets take an example this one-
I tried this http://screencast.com/t/3hJCPJgylXS
but the above one doesn't worked.
and various other versions, but no help so far.
Content here is coming dynamically from widget entry. May be I have some syntax issue. Does any one has encountered such situation before, Please guide me how to do that. Thanks!!
Let me ask the question in different way-
The string here is coming in a dynamic format-
<h2><?php echo $signup_btn_title?></h2>
How to convert it into wpml version so that wpml can read and search this string.
I tried this one, but it didn't worked-
<h2><?php _e('(<?php echo $seocontent_text_title;?>)','Casino'); ?></h2>
<h2><?php _e('(<?php echo $seocontent_text_title;?>)','Casino'); ?></h2>
this is definitely wrong
If any of similar approach, it should read similarly to this:
<?php _e( $seocontent_text_title, 'your_text_domain' ); ?>
You would probably also need to edit/create a wpml-config.xml file.
You could also try something along this lines:
(icl_translate function in strings)
<?php echo icl_translate(context, name, value); ?>">
I think this could help you to understand further:
https://wpml.org/forums/topic/transform-echo-to-php_e/
https://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/
https://wpml.org/documentation/support/wpml-coding-api/

Can a Joomla module “know” what position it's in? (2)

This is my very first question at StackOverflow.
I was trying to post comment in the post Can a Joomla module "know" what position it's in?, but could find no way to do that so I have to post another question here. Any tips regarding how to do this properly is greatly apprieciated.
Anyway, here is my questions:
I tried the code mentioned in the above post but it seemed didn't work. I'm not only quite new to PHP, but also coding, so I'm not sure if it's me or the code. here's what I did to the module's default.php file:
1)to ensure I insert the code at the right place, I insert
<?php echo(print_r($module)); ?>
and it output 1 at the right position;
2)the position-name that I need to determine is "showcase-a", so I insert code
<?php if ($module->position == 'showcase-a'):?>test<?php endif;?>
to the above place, but this time it does't show anything;
3)then I tried this code:
<?php if ($module):?><span>test</span><?php endif; ?>
But still it does't display "test" at the position as I expected.
4)I tried
<?php if (1):?><span>test</span><?php endif; ?>
and the test "test" displays as good. So I didn't code the IF statement wrong.
Now I'm totally lost. Since print_r($module) outputs 1, $module must be positive, why PHP ignore test in 3)? This is just a side question for the sake of PHP learning. What I still need to solve is let the module determine which position itself is in.
Please help. Thank you!
I'm not sure about using it in the template (although it doesn't seem wrong altogether) but my modules oftentimes access the position like this:
$module->position
in the module (so mod_something.php) so try to put it there, if it's available just set a variable and it will be available in the view too.
If $module->position didn't work for you. $module->position may only work for joomla 1.7, 2.5 and + but i'm not sure. $module->position works in joomla 2.5.
Otherwise you need to make a module.php function
Check out the file
/templates/you-template/html/modules.php
For example you make your position the following in your template (for example index.php)
<jdoc:include type="modules" name="header" style="includeposition" />
inside of
/templates/you-template/html/modules.php
You make a function like this :
<?php
function modChrome_includeposition($module, &$params, &$attribs){
//Set a value to $module->position
$module->position='WHATEVER YOUWANT';
?>
<div class="moduletable <?php echo $params->get('moduleclass_sfx').' '.$module->name; ?>">
<?php
if($module->showtitle){
?>
<div class="modtitle">
<div class="fleche">
<span>
<?php
echo $module->title;
?>
</span>
</div>
</div>
<?php
}
?>
<div class="modcontent">
<?php echo $module->content; ?>
<div class="clear">
</div>
</div>
</div>
<?php
}
?>
make different functions names that sets different values if needed.
the function name will match the syle you set:
modChrome_includeposition($module, &$params, &$attribs)
because
style="includeposition"
inside of your jdoc.

Wordpress slider for featured post on homepage

This what I am trying to do - source - http://www.torontostandard.com/
Is there any plugin for this look and feel ?
Code im using this time to fetch posts with featured tag.
However it would be better if I can loop using jquery, but unable to find any tutorial on this :(
<?php query_posts('tag=featured'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
PS- Googled a lot for this plugin but unable to find any
I always find that if I want to steal a jquery plugin from a site I just look at the source and google the js files:
<script type="text/javascript" src="/js/libraries/jquery.scrollTo-min.js"></script>
however I always just build my own but that's me.

Get post by its ID (WordPress)

How do I get a Custom Post by its ID in wordpress and show its thumbnail, title and content? Here is what I am using, but didnt the content is messed up.
<div class="left-cont1">
<div class="left-cont1-text">
<?php
query_posts('post_id=790&post_type=homepage');
while (have_posts()): the_post();
?>
<h1><?php the_title(); ?></h1>
<div class="cont1-border"></div>
<p><?php echo(types_render_field("homepage-content", array("raw"=>"true"))); ?></p>
<h2><?php echo(types_render_field("homepage-urltitle", array("raw"=>"true"))); ?></h2>
</div>
<div class="left-cont1-image1"><?php echo get_the_post_thumbnail($page->ID, 'home-circle'); ?></div>
<?php endwhile;?>
</div>
Thanks
You should be able to take your current code, change get_the_post_thumbnail($page->ID, 'home-circle') to get_the_post_thumbnail(get_the_id(), 'home-circle'), and replicate your code for each post you want to pull in, modifying the post_id=xxx to match the ID of the post you want to pull in.
This is not the most attractive solution, but from the information you gave, it should at least work. Just so you know, there is an entire StackExchange site devoted to Wordpress: https://wordpress.stackexchange.com/ where you should be able to get more Wordpress-specific answers. If your question is about PHP, please feel free to ask here, but if it is Wordpress-specific, your better bet would be the dedicated Wordpress site.
Since you're new to PHP, I would recommend getting an introductory book or two and try to get some of the basic concepts down pat before working with WordPress. While there will be a larger up-front investment of time, it will pay off in the long run. Good luck!

Categories