Pull content from WordPress site to display on HTML site [closed] - html

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a WordPress website with blogs on it, but I also have a HTML/Dreamweaver site.
I'd like to have the WordPress blogs automatically display on my index.html page when I update the WordPress website.

If both sites hosted on same server, it is simply possible. First you have to make a PHP file for example latest-post.php. and add the code below
<?php
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . "/wp-load.php");
?>
<?php
//The Loop - latest 5 posts from blogs category
$query1 = new WP_Query('showposts=5&cat=blogs&offset=0');
if ($query1->have_posts()) :
while ($query1->have_posts()) : $query1->the_post();
?>
<div class="whatever you want to style">
<h2><!--The Title-->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h2>
<!--thumbnail-->
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('Thumbnail'); ?>
</a>
<!--Excerpt-->
<p> <?php the_excerpt(); ?> </p>
</div>
<?php
//loop ends
endwhile; endif; wp_reset_postdata();
?>
Place this file on your non wordpress site where your index file is and include the above latest.php where you want.
<?php include_once("latest-post.php"); ?>
If you are using HTML file, The PHP will not exicute. You can either rename index file .html to .php or add
AddType application/x-httpd-php .html
to your .htaccess. Take a look at
Run PHP from HTML
Change the number "showposts=5" to how many posts you want, and change "cat=blogs" to "cat=your category name"

Here is an example what I am using on my site to read the headings from the database:
<?php
//Database access
$dbname="db-blog";
$dbhost="localhost";
$dbuser="user";
$dbpass="secretpassword";
//SQL Befehl zur Abfrage der Postings
$sql = "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER by ID DESC LIMIT 0,6";
//Open database
$db = mysql_connect($dbhost,$dbuser,$dbpass) or die("no connection to database");
mysql_select_db($dbname, $db);
$sqlRes = mysql_query($sql,$db);
mysql_close($db);
//Output titles
$recordCount = mysql_num_rows($sqlRes);
for ($i = 0;$i < $recordCount;$i++) {
$arCur = mysql_fetch_array($sqlRes);
echo "<li>" . $arCur["post_title"] . "</li>";
}
?>
Should be easy to adapt to also read the contents if needed.
If you dont have direct access to the database, the following script accesses the blog items from the RSS feed.
<?php
//Settings
$blog_url = "http://blog.ekiwi.de"; //URL of blog without / at the end
$count = 5; //Number of posts that should be shown
//--------------------------------------------------------------------------------
$content = #file_get_contents($blog_url . "/?feed=rss2");
preg_match_all("/<item[ ]?.*>(.*)<\/item>/Uis", $content, $items);
$items = $items[1];
if(!empty($items)) {
if ($count > sizeof($items))
$count = sizeof($items);
for($i = 0; $i < $count; $i++) {
//read link
preg_match("/<link>(.*)<\/link>/Uis", $items[$i], $link);
//Read title
preg_match("/<title>(.*)<\/title>/Uis", $items[$i], $array_title);
$title = str_replace("<![CDATA[", "", $array_title[1]);
$title = str_replace("]]>", "", $title);
//Output title with link
echo "<li>\n";
echo " $title\n";
echo "</li>\n";
}
}
?>
Both solutions use PHP.

Related

Display latest wordpress featured images on static html page

I have a wordpress blog page installed in subfolder of my website(converted from html)and a static html home page.
I would like to display 3 latest posts and its featured images on home page. With code below i can display latest posts text but i dont know how to show featured images of posts. Into index.php of a wordpress custom theme i placed featured photo inside a div:
<div id="blogphoto"><?php the_post_thumbnail(); ?></div>
This is the code on static html index.php page which is pulling out latest post. Can anyone help me to get featured images of these posts too?
<div id="wp-post">
<?php
$args = array('numberposts'=>1);
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<h3>".$recent_post['post_title']."</h3> <br>";
echo "<span>".$recent_post['post_date']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post2">
<?php
$args = array('numberposts'=>1 , 'offset'=>1 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post3">
<?php
$args = array('numberposts'=>1 , 'offset'=>2 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
Please try to this code the_post_thumbnail getting the feature image
<?php
if ( $query->have_posts() ) {
$i = 1;
while($query->have_posts()) {
echo '<div id="wp-post-'.$i.'">';
$query->the_post();
?><h2><?php the_title(); ?></h2>
<?php
if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
echo '<p>post says it has a featured image</p>'; // double checking
the_post_thumbnail('thumbnail');
} else {
echo '<p>this post does not have a featured image</p>';
}
echo '</div>';
$i++;
}
} else {
echo '<p>no posts found</p>';
}
?>

I want to extract URL contain in my Wordpress post

I have around 1K posts in my blog. Every post contain URL something like "www.example.com/abcd.html" or "www.example.com/1234.html".
I want to extract those URL from every posts into a file.
Is there any query to do this job done?
Thanks,
Extracting URL within wordpress not easy part to extract. Why to more efforts if there are plugins available.
Install the following plugins and export them in CSV
https://wordpress.org/plugins/export-all-urls/
This will display all post link with title
<?php query_posts('posts_per_page=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$content = $urls = wp_extract_urls( get_the_content() );
echo "<pre>";
print_r($content);
echo "<pre>";
endwhile;
endif; ?>

How do I restart my loop with get_next_post()?

I'm looking for a succinct method of making get_next_post()
double back to the beginning once it hits the last post.
Currently, it stops once it hits the final post.
Here are a few lines of code from the codex
for context that are similar to what I'm using:
<?php $next_post = get_next_post();
if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>
http://codex.wordpress.org/Function_Reference/get_next_post
Thanks in advance for your suggestion.
you can't do it with get_next_post - full stop.
Here's how I've done it...
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
$prev_title = strip_tags(str_replace('"', '', $previous_post->post_title));
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
// get the first posts ID etc
$args = array('post_type'=>'project', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
$first_title = get_the_title( $first_id );
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
$last_title = get_the_title( $last_id );
// if the current post isn't the first post
if($current_id != $first_id) { ?>
Previous Project:<?php echo $prev_title; ?>
<?php
// if the current post is the first post
} else { ?>
Previous Project
<?php }; ?>
<?php
// if the current post isn't the last post
if($current_id != $last_id) { ?>
<a rel="next" href="<?php echo get_permalink($next_id) ?>" title="<?php $next_title ?>" class="prev-next next"> Next Project <?php echo $next_title; ?></a>
<?php
// if the current post is the last post
} else { ?>
<a rel="next" href="<?php echo get_permalink($first_id) ?>" title="<?php $last_title ?>" class="prev-next next"> Next Project <?php echo $first_title; ?></a>
<?php } ?>
Elements take from here Getting first & last post in custom post type outside of loop

how to modify the wordpress main loop?

i have a single page website built on wordpress (twenty-thirteen-child template).
header content footer.
and 3 posts categories :category A category B category C.
i need to split the content into these 3 categories/sections -like this
<div class="section_top">
all the posts from category A
</div>
<div class="section_mid">
all the posts from category B
</div>
<div class="section_bot">
all the posts from category C
</div>
i got a little confused when i started to read about the wordpress main loop ,query_posts and WP_Query,eventually i have this code replacing the main loop ,so my questions are :
1 is this the correct way to do it?
2 how to give each section a unique class since i need to style each section diffrently?
here is the code (index php(in child theme)
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$args = array('category__in' => array(catA,catB,catC));
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div id="<?php
foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>"
class="post-item">
<div class="post-info">
<h2 class="post-title">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
<div class="post-content">
<?php the_content(); ?>
</div>
</div>
<?php
endwhile;
else:
?>
Oops, there are no posts.
<?php
endif;
?>
The problem with how you're trying to solve it is that all posts are still shown chronologically, so this could happen:
Category A post 12/01
Category B post 09/01
Category A post 05/01
What I'd recommend: make 3 different WordPress loops querying each category separately like so:
<div class="section_top">
WordPress loop for all the posts from category A
</div>
<div class="section_mid">
WordPress loop for all the posts from category B
</div>
<div class="section_bot">
WordPress loop for all the posts from category C
</div>
Example of such a loop:
// The Query
$the_query = new WP_Query('category_name=categoryA');
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Difference between get_the_...() and the_...()
When you use functions like get_the_title() you are able to store them in a PHP variable like so:
$title = get_the_title();
echo strtoupper($title); //do something with it later
When you use functions like the_title() then the title is immediately printed on the page, so there's no need for an echo statement:
the_title();
Notice: the_permalink() has a function get_permalink(), the function get_the_permalink() does not exist. Don't ask my why ;)

Manually setting wordpress categories for fontface Icons

I'm editing the twenty eleven theme but I want to change the categories so they display the icons as a #fontface icon instead of a word.
The categories needs to look something like this (using an art category as an example):
<i class="icon-brush"></i>
However this is how it currently looks.
<span class="entry-utility-prep entry-utility-prep-cat-links">Posted in</span>Art
Here is what my code is at the moment:
<?php $show_sep = false; ?>
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );
if ( $categories_list ):
?>
<span class="cat-links">
<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
$show_sep = true; ?>
</span>
<?php endif;?>
<?php endif; // End if categories ?>
The class needs to change depending on what post category is set is there a way to manually set what icon links to which category?
You can view my live template here http://mhutchinson.me.uk/
Thanks for your help!
Megan
You might wanna try something like this:
(change the printed HTML inside the foreach to fit your needs)
<?php
foreach((get_the_category()) as $category) {
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}
?>
Cheers