Wordpress get data from url - wordpress

I am trying to get data (data) from the following URL.
http://www.example.dk/page/?data=1
I use the following code for getting the data to the wordpress page:
<?php $_GET['data']; ?>
But I don't get the data to the page. Isn't it the normal way to get data from URL with php?

As Matt pointed out, you'll need to echo the the result. Like:
<?php echo $_GET['data']; ?>
If your server is setup to use PHP shorthand statements you might also be able to use
<?= $_GET['data'] ?>

Related

What is different between post_navigation() and get_the_post_navigation();?

What is difference between these WordPress functions, and how to implement it?
the_post_navigation(); get_the_post_navigation();
the_archive_title(); get_the_archive_title();
the_post_pagination(); get_the_post_navigation();
the_archive_description(); get_the_archive_description();
I already googled for this, but I'm still not getting it right.
All the functions that starts with get_ are only returning the "result" of the function : If you put this function in a php page and watch this page in a browser, nothing will be displayed.
If you want the result to be displayed, you have to add echo before the function and this is exactly what the function that starts with the_ are doing.
You may ask yourself why sometimes we want only the result to be returned by the function and not displayed. It's because sometime, we have to do some additional operations with the result before displaying it.
Example:
$content = get_the_content();
$content = str_replace('Hello', 'Bye', $content);
echo $content;
If not operation are needed so you only need to do:
the_content();
You also ask "How to implemenent it ?". To implement the function, you will have to add it in some specific php files. For example, for the get_the_post_navigation() function you will have to add it in the single.php file in your theme folder. You will need basics on php.

Wordpress Query Random Posts every X amount of hours

I'm fairly new to PHP so excuse me if this seems like a dumb question. I'm building a WP theme for a client, I've gotten the base-functionality down for a "random posts" section, but there's a factor that I'm unable to figure out for myself.
It is a custom post type, that also is utilizing custom fields. The function is, 4 posts from this post type display at random with the required information.
Now: I was able to figure this out on my own, but the random 4 posts is where I'm stumped. The client is requesting that the "Random" aspect only happens every 24 hours instead of every time the page is reloaded.
So basically... how would I alter my existing query so that it only changes the output every 24 hours, instead of every time the page is loaded?
Please see my existing code below:
<ul>
<?php query_posts("post_type=chapter&orderby=rand&posts_per_page=4");?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="authors">
<a href="<?php the_permalink();?>"><img src="<?php
$post_object = get_field('article_author');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
the_field("profile_image");
$first=get_field("first_name");
$last=get_field("last_name");
endif;
wp_reset_postdata();
?>"></a>
<div class="author-name-home"><?php the_title();?></div>
<div class="author-title-home"><?php echo $first;?> <?php echo $last;?></div>
</div>
</li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
There are a few ways you could do this. Here are some options:
Within a nominated post (custom or otherwise), add some additional metadata - you could store the resultant IDs of your 4 random posts (eg: metaname: 'dailyrand' value: '44,55,11,23'). You would have your routine check the time and date of the last time this particular post was updated every time it runs. Once the time and date reaches 24 hours after the last edit, overwrite the metavalue with newly generated random post IDs. If it is not 24 hours yet, pull back the stored post IDs and display. Of course, if the post data changes for some other reason, it could refresh at the wrong interval - maybe it would be better to also set up a time and date metatag to update as well instead of relying on the actual post updated time.
Create a table in your database with a few fields (dailyrand, recordtime, postids, etc) and essentially do the same thing. Check each time for 24 hours since the last update to the table and rinse and repeat.
Get some plugin or widget to do it for you. :) Here's one: http://wordpress.org/plugins/static-random-posts-widget/ - it doesn't look like it has been updated for a while.
Of course, some may think these are terrible ideas - just offering some quick suggestions as no one has commented as yet.

Connecting to an external database from within Wordpress

I'm trying to connect to and import data into a WordPress 3.5.1 page template from an external database which is hosted on the same localhost server.
I've Googled this extensively and have come up with the most preferred solution for this:
<?php
/*
Template Name: Import Sign data
*/
?>
<?php echo "Import Sign Data Page<br>"; ?>
<?php
$mydb = new wpdb('myname', 'mypassword', 'mydb', 'localhost');
$mydb->show_errors();
$signs = $mydb->get_results("SELECT * FROM signs");
print_r($signs);
foreach ($signs as $sign) {
echo $sign->title . '<br />';
}
?>
The code above only gives me the first echo, 'Import Sign Data Page' and nothing else. No errors, nothing. I've checked all my syntax many times now. Can any see where I might have gone wrong here or perhaps suggest some debugging tips?
TIA
Using the wpdb class is by definition only going to work with the installed wordpress database. You should use whatever functions that pertain to your external database.

Wordpress permalink without domain name

OK it might sound stange but I am facing a good challenge with wordpress.
when echoing the_permalink(); and checking the portfolio pages, I am getting a link something like this:
http://www.domain.com/?portfolio=test
(Where test is the portfolio name).
Is there any option to return the permalink trimmed to ?portfolio=test keeping out the domain url?
While asking, I think I got the answer already (trim()) but I would like to hear your ideas too.
every answer will be much appreciated!
You can obtain the permalink of a post by doing something like so:
<?php
function my_permalink() {
echo substr(get_permalink(), strlen(get_option('home')));
}
?>
The get_option method replaces the deprecated get_settings method, and allows you to retrieve named option values from the database:
http://codex.wordpress.org/Function_Reference/get_option
The 'home' value passed in to the get_option method will return the site URL.

Converting these php files to ASP.NET

I have these two file in my project that I am migrating from php. I have developed most of it by seeing the functionality but there are these two files which I don't know about. If somebody could have a look and help me converting these, I would really be thankful.
Menu.ctp
<?php
Configure::write('debug', 0);
echo($_GET['callback'].'(');
echo ($javascript->object($profiles));
echo(');');
?>
Retrieve.ctp
<?php
Configure::write('debug', 0);
echo($_GET['callback'].'(');
echo ($javascript->object($profile));
echo(');');
?>
Configure::write('debug', 0);
This tell application to put debug mode to off. If you in debug mode, any application error message will be displayed which is not appropiate for production or ajax call.
echo($_GET['callback'].'(');
$_GET is a global variable which hold all GET request parameter. For example, request to index.php?callback=some_callback will set $_GET['callback'] to some_callback.
echo ($javascript->object($profiles));
This is javascript helper in CakePHP which turn PHP array($profiles) into JSON.
echo($_GET['callback'].'(');
echo ($javascript->object($profile));
echo(');');
This will otuput : callback_parameter(JSON form of profiles);

Resources