how to echo specific php if element specific element id is active? - php

I've got a wordpress page with tabs on it, and I want to show in one of 'em a query of posts from a category.
I've got an active li like: where the tab info is shown
<li class="tab-pane active" id="ert_pane1-4">
All I came up with is:
<?php
$tab-id= getElementById("ert_pane1-4");
echo $tab-id;
if($tab-id =='<li class="active">'){
query_posts('cat=1');
} endif;
?>)
I know this will sound rather easy but I'm a newbie on php but up to learn.

There are some problems. First, endif; is not needed:
if($tab-id =='<li class="active">'){
query_posts('cat=1');
}
Also, getElementById is JavaScript not PHP. This wouldn't work anyway since the page hasn't loaded yet while PHP is running.

Related

How do I link to a specific section on another page on Wordpress

I'm new to Wordpress and PHP and this might be a dumb question, but I'm trying to link one of my menu items to one of the sections on my index page.
I know that if I just wanted to link it to the index I should use this:
<?php echo home_url(); ?>
But I want the link to send the user to the "About" section. Basically, I need to know how to do this:
index.php#about
but with PHP.
Thank you!
You're on the right track.
The ideal way to do this would be to add a <a name="about></a> tag to the appropriate section of your template. This is called an HTML anchor and is how those #tags know where to point to.
Given that this is Wordpress, you could probably also get away with just appending that to the title of the appropriate section. So wherever you specified 'call this section "About"', you could probably redo it as 'call this section "<a name="about">About</a>"' and then you'll be able to link to it using anchors like in your example-- About
If you are new to php, maybe you should use wordpress's editor ?
In your page (in the admin page), you can put any html you want.
In the editor, you can add custom links (with anchors or not) and you can put a div tag in the "html" tab.
So if you put your link at the top of your page and put your section in a div id="myanchor", it should do it !
You shouldn't do this with HTML or PHP but rather with JS. Specifically for long pages and require in-page navigation, I really like the scrollTo jQuery plugin.
In practice, you'll have some HTML containers that look something like this:
<!-- Your menu -->
<ul>
<li id="about-button"></li>
<li id="product-button"></li>
<li id="something-button"></li>
<li id="else-button"></li>
</ul>
<!--Your page sections-->
<main class="my-page">
<section id="about"></section>
<section id="product"></section>
<section id="something"></section>
<section id="else"></section>
</main>
Once you've included jQuery and the scrollTo plugin, you'll have some JS that looks like this:
$('#about-button').click(function() {
$.scrollTo($('#about'), {
duration: 800,
offset: -50
});
return false;
});
The JS is saying that once you click on the #about-button, take 800 milliseconds and animate the page down to -50px before the position of the #about HTML element. You could just setup a series of click functions for each button and you'd have a slick in-page nav system.

WordPress posts loop resulting in ending of file and unclosed elements

I am building a website for a friend who wants to use WordPress for a blog section. I could have just made the whole site as a custom WordPress theme, but I don't like using WordPress because it's bloated, slow, and buggy. So instead I want to add a recent posts module to the site by including the wp-load.php file from the WordPress site and using their functions.
I have made several attempts at creating the module and each method has resulted in an error.
Using a while (have_posts()) loop resulted in a 500 Internal Server Error so I tried using wp_get_recent_posts(array followed by a foreach statement, which doesn't result in the error but instead gets me an "End of file seen and there were open elements."
Here is the link to the site: http://colinthompson.ca/drake/
Here is the code for that section:
<section id="blog">
<h3>recent posts</h3>
<div id="post-wrap">
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3
));
foreach ($recent_posts as $post) {
echo '<div class="blog-post">
<div class="post-img">'.get_the_thumbnail($post['ID']).'</div>
<h5>'.$post['post_title'].'</h5>
<p>by <span>'.the_author($post['ID']).'</span></p>
<p>'.the_date(d,m,y).'</p>
<p>'.the_excerpt($post['ID']).'</p>
</div>';
}
?>
</div>
<a id="full-blog" href="#" title="Check out the full blog"><div class="slide-in"><p>full blog</p></div><div class="arrow-shaft"></div><div class="arrow-head"></div><p>read more</p></a>
<a class="next-section" href="#contact" title="Let's get in touch"><div class="slide-in"></div><div class="arrow-shaft"></div><div class="arrow-head"></div></a>
</section>
I tried removing the foreach statement to test for unclosed elements in the html that is being echoed, but the error persisted, resulting in the unclosing of the parent div#post-wrap and grandparent section#blog and general fudging of the rest of the document.
When I removed the wp_get_recent_posts array the page loaded as normal, but without the posts of course.
Maybe I did simply forget to escape something but I don't see it. Any help is appreciated.
Thanks!
Where are you going to display the posts? Where is your friend going to write them?
It seems to me you are much better off including WordPress in it's entirety on just one part of the site.

How can I add a image click function to gallery.php temp code?

Ok so here is the link to the template code for gallery.
https://github.com/JoomShaper/Helix-Joomla/blob/master/helixPlugin/shortcodes/gallery.php
However when I insert images on my database (not in Php), the image shows up but I am unable to click that image and be directed to the images page, which I have hyperlinked on my database (Joomla). I will have multiple images where I will need to add this click function to.
I have been trying to figure this out forever. I am new to Php and coding, so forgive my ignorant use of word choices from the above paragraphs.
(I've noticed in the past when speaking to coders it is frustrating for them to code the jargon from a someone who doesn't know the language ha!) Thanks!
Sorry, I know this is not an answer probably, but I cannot comment yet. I have been looking at your code for a little while, and I noticed some things I wanted to ask you about. First:
<?php foreach ($galleryArray as $key=>$item) { ?>
<li style="width:<?php echo round(100/$columns); ?>%" class="<?php echo str_replace(',', ' ', $item['tag']) ?>">
<a class="img-polaroid" data-toggle="modal" href="<?php echo ($modal=='yes')? '#modal-' . $key . '':'#' ?>">
<?php
echo '<img alt=" " src="' . $item['src'] . '" />';
?>
<?php if($item['content'] !='') { ?>
<div>
<div>
<?php echo do_shortcode($item['content']); ?>
</div>
</div>
<?php } ?>
</a>
</li>
You begin your a and li inside the foreach loop, but you finish them outside of it. Considering that the a is your link, and your link is not working, I thought this could be part of your issue.
Also, I noticed that when you link to the modal, you use the same variables from the loop, but the variables were defined inside the loop and would not carry over on clicking the link that was generated. It seems to me that you would need to define a property value with the variable you need from each one, so that you can use JS to do something along the lines of "onClick" grab "this.property.value" so that you could have that information in the modal.
I only thought this was of note because the modal you are opening with the link is named by one of these variables. Unless you are creating a separate modal for each one inside the foreach loop, the name you are linking to does not exist. Sorry if this is confusing.

how to make wordpress posts loop and replace the html output using jquery?

In my wordpress site, I have a page that has 3 sections. Every section contains posts from a specific category. I use this wordpress query to get the latest 3 posts in that category and load it in it's section(div). So, for "Novels" section, I use this query:
<? $novels = get_option('of_novels') ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('category_name=$novels&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="sliderunit">
<?php the_post_thumbnail(); ?>
<div class="novelsslidertitle">
<div class="arrow-left"></div>
<img class="cross" src="<?php bloginfo('stylesheet_directory'); ?>/images/cross.png"/>
<?php the_title(); ?>
<h3>رواية</h3>
</div>
</div>
<?php endwhile;?>
and that gets me the latest 3 posts in #novels category. Now, I have some buttons. I want to write some jequery to loop on this category and get previous and next posts, and reloads the new queries content to that #novels div, replacing the old query code in the html using
$("<div/>").replaceWith("// Replace with a function to get the code of new output");
to act as a slider without using pagination and reloading the page? I hope my problem is well declared, I appreciate any helpful clues, links or references.
I don't think $.replaceWith is really what you need here. You would just listen for when the user reaches a certain point, maybe the last in the currently loaded posts, or the second to last, then run $.ajax() or similar query, append that to your #sliderunit and it will make it so you be able to continue scrolling/sliding. By appending and not replacing, you can go back and forth if you want without having to reload anything. If you are concerned about memory management, you could use $.remove() at a certain threshold.
In case you need a little more info on how to do this, start by appending your new data, then using CSS manipulation such as $.animate() to perform the slide effect. You can either use layers to hide things you don't want showing, or I guess you could also call $.hide() on blocks you don't want shown.

Get specific html content from other site with PHP

I want to try and get the latest movie I checked on the IcheckMovies site and display it on my website. I don't know how, I've read about php_get_contents() and then getting an element but the specific element I want is rather deep in the DOM-structure. Its in a div in a div in a list in a ...
So, this is the link I want to get my content from: http://www.icheckmovies.com/profiles/robinwatchesmovies and I want to get the first title of the movie in the list.
Thanks so much in advance!
EDIT:
So using the file_get_contents() method
<?php
$html = file_get_contents('http://www.icheckmovies.com/profiles/robinwatchesmovies/');
echo $html;
?>
I got this html output. Now, I just need to get 'Smashed' so the content of the href link inside the h3 inside a div inside a div inside a list. This is where I don't know how to get it.
...
<div class="span-7">
<h2>Checks</h2>
<ol class="itemList">
<li class="listItem listItemSmall listItemMovie movie">
<div class="listImage listImageCover">
<a class="dvdCoverSmall" title="View detailed information on Smashed (2012)" href="/movies/smashed/"></a>
<div class="coverImage" style="background: url(/var/covers/small/10/1097928.jpg);"></div>
</div>
<h3>
<a title="View detailed information on Smashed (2012)" href="/movies/smashed/">Smashed</a>
</h3>
<span class="info">6 days ago</span>
</li>
<li class="listItem listItemSmall listItemMovie movie">
<li class="listItem listItemSmall listItemMovie movie">
</ol>
<span>
</div>
...
There are some libraries which could help you!
One I've used for the same purpose, a long time ago, is this: http://simplehtmldom.sourceforge.net/
I hope it help you!
follow steps to achieve this
STEP1:-
First get the contents using file_get_contents in a php file
ex: getcontent.php
<?php
echo file_get_contents("http://www.icheckmovies.com/movies/checked/?user=robinwatchesmovies ");
?>
STEP2:-
CALL the above script using ajax call and add the content to a visibility hidden field in the html.
ex:
$('#hidden_div').html(response);
html:-
<html>
<body>
<div id='hidden_div' style='visibility:hidden'>
</div>
</body>
</html>
STEP3:-
now extract the id what ever you want.
What you are asking for is called as web scraping ,I have done this a few months back, the process goes like this,
Make a HttpRequest to the site from which you need the content,check
the php class for it
Use a DOM parse library for handling the downloaded page (it would be in html),simple HTLM DOM would be a good choice
Extract your required information
Here are some tutorials for you,
HTML Parsing and Screen Scraping with the Simple HTML DOM
Library
Beginning web page scraping with php
SO Posts:
HTML Scraping in Php
And best of all Google is your friend just search for "PHP scraping"

Categories