I'm trying to store the number of views on a certain post in my database. For example, I have one page index.php, and depending on which article the user clicks on, it will take them to a certain article (still on index.php): index.php?article_id=24. The problem I'm trying to figure out is how do I store a dynamic page that uses php $_GET method to view posts/articles. I can do it on the index.php page, but I can't figure out how to do it on each article, since it's technically still on the same page (index.php).
This is my page counter code:
?php
$filename = "pageviews.txt";
$data = file_get_contents($filename);
settype($data,"integer");
$data++;
$f = fopen($filename,"w+");
fwrite($f,$data);
fclose($f);
//insert $data into db
?>
And this is my code for after a user clicks on an article and I retrieve the article id:
<section class="large-12 columns">
<?php
$inner_article = mysqli_fetch_array(query($art_sql));
?>
<h1><?php echo $inner_article['art_title']; ?></h1>
<img src="<?php echo $inner_article['art_feature_image']; ?>" alt="<?php echo $inner_article['art_description']; ?>">
<p><?php echo $inner_article['art_create']; ?></p>
<p><?php echo html_entity_decode($inner_article['art_content']); ?></p>
<p><?php echo $inner_article['art_tags']; ?></p>
</section>
Now, my last question is whether this is a good approach to storing page views because I have one txt file. How can I store more than one number of page views (for each article) in one txt file. This is why I was thinking using php cookie is a better route.
Add a count field to your post in DB, and while displaying the article, update this field with a count
UPDATE myTable SET Column1=Column1+1
Related
This question already has an answer here:
get child pages of parent ID and display custom image field with link to page in WordPress
(1 answer)
Closed 3 years ago.
I am very new to wordpress and php so excuse me in advance if maybe the answer were already posted, but I looked everwhere and couldnt find a solution to my problem. Or at least I never was able to make work any ideas I tried, and the wordpress documentation is still very complicate for me, when I look through the functions and hook to understand how it is working.
I basically made a slideshow on my home page which display the feature images of the children pages. I have the thumbnails of the images in my side navigation, and the slideshow in my main content section. Basically I am retrieving the ID of each child page by entering the specific id for each slide, but I'd like to make all this dynamic and loop the whole thing, but I don't manage to output the children pages ID dynamically. Here is the hard code I am using now :
<div id="contentContainer">
<div id="wrapper">
<div id="itemOne" class="content">
<?php
$image_id=get_post_thumbnail_id(192);
$image_url=wp_get_attachment_image_src($image_id,'full-size');
echo '<img src="'.$image_url[0].'">';
?>
</div>
<div id="itemTwo" class="content">
<?php
$image_id=get_post_thumbnail_id(196);
$image_url=wp_get_attachment_image_src($image_id,'full-size');
echo '<img src="'.$image_url[0].'">';
?>
</div>
<div id="itemThree" class="content">
<?php
$image_id=get_post_thumbnail_id(198);
$image_url=wp_get_attachment_image_src($image_id,'full-size');
echo '<img src="'.$image_url[0].'">';
?>
</div>
<div id="itemFour" class="content">
<?php
$image_id=get_post_thumbnail_id(200);
$image_url=wp_get_attachment_image_src($image_id,'full-size');
echo '<img src="'.$image_url[0].'">';
?>
</div>
</div>
As I will have more slides than this later on, and in case I need to change the images without going through code, I would like to get this code dynamic, and looping as much slides as it can be.
Thank you for your help!
You can get page id of children by using
get_page_children()
here is an example :-
<?php
// Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type'=>'page','posts_per_page' => '-1'));
// Get the page as an Object
$parent_page = get_page_by_title('parent_page_title');
// Filter through all pages and find page's children
$page_children = get_page_children( $parent_page->ID, $all_wp_pages );
// echo children page id from $page_children array
foreach($page_children as $children):
echo '<pre>';
print_r($children->ID);
echo '</pre>';
endforeach;
?>
Im using lightgallery so i need to load images before call them in lightgallery. Problem is that images are large so it takes too much time to load. Is there any way to load that specific gallery when user click on link.
<div id="lightgallery-<?php echo get_the_ID(); ?>" class="hidelightgallery">
<?php
foreach ($files as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
$attachment_title = get_the_title($image->ID);
$caption = get_post_field('post_excerpt', $image->ID);
?>
<a class="item" href="<?php echo $image_attributes ?>" data-sub-html="<?php echo $attachment_title; ?> <?php if($caption!= '') echo ' - ' ?> <?php echo $caption ?>"><img src="<?php echo $image_attributes ?>"></a>
<?php } ?>
</div>
Now what i want is if user click for example link with this id then do foreach. Is that possible?
Just follow these steps,
create new template / html page where you will write html and populate by foreach loop
add your id = lightgallery whole code to that html page
when you will click on your link (which you mentioned) fire an ajax
Ajax function will get some id or number of images need to show or your logic on how you will populate data in foreach loop
in php you will get all relevant data, and you will populate that data in html file you created in step 1
php function will return that data to ajax function
Ajax function will get all your dynamic html data
populate that html where ever you want or just append that html wherever you want
Go step by step, this will solve your problem.
So i have the single-portfolio.php which present one of my project.
This function makes proper title to my projects every time I choose one.
<h1><?php the_title(); ?></h1>
Now whatever project i choose it always transport me to the Angela...
<h1><?php the_title(); ?></h1>
What i want to do is to have proper link to the proper project in the title.
I figured sth like this but it does not work.
<?php
$f = "http://facebook.com/";
$t = "http://twitter.com/";
$l = "http://linkedin.com/";
if (the_title()=='Facebook') {
Echo "<a href=$f> Facebook</a>";
} elseif (the_title()=='Twitter') {
Echo "<a href=$t> Twitter</a>";
} else {
Echo "<a href=$l> Linkedin</a>";
}
?>
What i get on the page is 3 times written Facebook if its Facebook page or 3 times Twitter e.g:
FacebookFacebookFacebook(the only last one "Facebook" is a link)
The problem is that the_title() echos the title, rather than returning it (as documented in the Codex). That means that, for each of your if conditions, the title gets echoed out.
Try using get_the_title() instead - this returns the post title rather than echoing it.
the_title() is a function that returns the title of the current page/post in wordpress. For the FB/Twitter/LinkedIn portion of your code you should just include "Facebook" instead of using the_title()
Use a custom field in your post to store the link in the post. Call the field "url" for example:
now you can read the field in your template and use it:
<?php $url = get_post_meta( get_the_ID(), "url", true ); ?>
<h1><?php the_title(); ?></h1>
I'm developing a multilang site. The content generated on the varible that passes in the url. Exemple for about us page my url is: domain.com/file.php?id=1 I got one main file and in that file the query gets the id of the selected menu.
If I change the language my url turns to domain.com/file.php?id=1&lang=en. Every time I change the language my url adds one more lang like this: domain.com/file.php?id=1&lang=en&lang=fr&lang=de&lang=en.....
in other multilang project I used this: header("location: ".$_SERVER['SCRIPT_NAME']); But the it were less dynamic pages. like this: domains.com/aboutus.php. I mean: the number of pages were static. The user cannot add or remove pages.
This time because I pass the page id in the url I tried header("location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']); but it gives an redirect cycle error every time I try to change the lang.
UPDATE
Code to select the languages:
<?php $actual= $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];?>
<div id="langContainer">
<span><a <?php if ($_SESSION['idLang']=='en') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=en">EN</a></span>
<span><a <?php if ($_SESSION['idLang']=='fr') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=fr">FR</a> </span>
<span><a <?php if ($_SESSION['idLang']=='es') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=es">ES</a></span>
<span><a <?php if ($_SESSION['idLang']=='de') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=de">DE</a></span>
</div>
in my session.php
if (!isset($_SESSION["idLang"]) )
$_SESSION["idLang"] = 'en';
if (#isset($_GET["lang"])){
$_SESSION["idLang"] = $_GET['lang'];
//header("location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
}
So my question is if there's anyway I can get my url cleaner, hidding the lang variables?
Thanks
Dynamically build query parameters, replacing exiting ones in the current URL:
<a href="...?<?php echo http_build_query(array('lang' => 'foo') + $_GET); ?>">
I am building a mobile version of my company website, and one thing we are in need of is an RSS feed.
I have the RSS pulling in fine with this code:
<?php
$url = 'http://www.someurl.com/rss/articles';
$feed = simplexml_load_file($url, 'SimpleXMLIterator');
$filtered = new LimitIterator($feed->channel->item, 0, 15);
foreach ($filtered as $item) { ?>
<li data-icon="false">
<h2><?php echo $item->title; ?></h2>
<p class="desc"><?php echo $item->description; ?></p>
<br />
<p class="category"><b><?php echo $item->category; ?></b></p>
<a class="link" href="<?php echo $item->link; ?>">Read More</a>
<br />
<p class="pubDate"><?php echo $item->pubDate; ?></p>
<br />
</li>
<?php } ?>
What I would like to do is utilize either the fopen() or file_get_contents() to handle the clicking of the 'Read More' link and strip all of the contents of the incoming page except for the <article> tag.
I have searched Google the past day, and have not been successful in finding any tutorials on this subject.
EDIT:
I would like to load the stripped HTML contents into their own view within my framework.
SECOND EDIT:
I would just like to share how I solved this problem.
I modified my $item->link; to be passed through the URL as a variable:
Read More
On the article.php page, I collect the variable with a if() statement:
if (isset($_GET['rss_url']) && is_string($_GET['rss_url'])) {
$url = $_GET['rss_url'];
}
Then building on the suggestions of the comments below, I built a way to then collect the incoming URL and strip the necessary tags to then format for my mobile view:
<div id="article">
<?php
$link = file_get_contents($url);
$article = strip_tags($link, '<title><div><article><aside><footer><ul><li><img><h1><h2><span><p><a><blockquote><script>');
echo $article;
?>
</div>
Hopefully this helps anyone else who may encounter this problem :)
I'm not sure if I understand it correctly but are you trying to output the contents on the current page whenever someone clicks the more link?
I would probably use Javascipt to do that, maybe jQuery's .load() function which loads html from another page and allows you to load only specific fragments of a page.. but if you need to use php I would look into Simple HTML DOM Parser
$html = file_get_html($yourUrl);
$article = $html->find('article', 0); // Assuming you only have 1 article/page
echo $article;
The only way I can see is to set up your own separate script to route the links through.
So, instead of echo $item->link use
echo 'LinkProcessor.php?link='.$item->link
Then, setup a script called LinkProcessor.php and use file_get_contents on that page. You can then process the XML to only show the article tag and echo the results:
$article = file_get_contents($_GET['link']);
$xml = new SimpleXMLElement($article);
$articleXml = $xml->xpath('//article');
echo articleXml[0];
Note that the code is untested, but it should be OK.