2

I know it may be a stupid question. I have a website that I have built in PHP and I have a section that I would like 3 responsive posts to pull in from Wordpress.

I have tried using API and JSON curls and not having any luck, mostly because I lack knowledge on those things. Didn't know if it could be easier to pull into PHPMyAdmin then I could code to pull in the info from that and make it responsive?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Do you want to get information pulled form a single Wordpress-site into your site? Can't you just use the RSS-feed for that? Or, if you have access to the code of both sites, just create a php-file that pulls new data input in the wordpress site(s) and post it directly to the other database? (You will need to allow for external connections to the database, which can be iffy) – junkfoodjunkie Oct 26 '16 at 20:17
  • I would use an RSS feed, but I may post images into the wordpress posts and I was told I can not use the RSS for that. I just want to get my wordpress featured 3 newest posts and put them directly on my personal site that isn't a wordpress site. Reason is that I want to update a small portion of my index.php page from my phone and not have to hand code every time I want to post something. – Digital_Jedi_3 Oct 26 '16 at 21:16
  • Uh, what? Of course you can push images with RSS. Whoever told you differently is wrong. Have a look at this post: http://stackoverflow.com/questions/483675/images-in-rss-feed – junkfoodjunkie Oct 26 '16 at 21:19
  • Haha I feel stupid... I should have looked more into it, but like I said I was going off what someone has told me. Let me take a look at this! – Digital_Jedi_3 Oct 27 '16 at 13:10
  • Okay junkfoodjunkie I added a section below with the code I am now using! hope you can help! – Digital_Jedi_3 Oct 27 '16 at 21:08
  • phpmyadmin is just a tool to view the contents of a database. – Sam Dufel Oct 27 '16 at 21:11
  • I totally understand that now.. Its a powerful tool, I was just trying to find a way to make this work. I have something but I am struggling on limiting the description. I am in the process of learning more on php, but still have a long way to go – Digital_Jedi_3 Oct 31 '16 at 15:18

2 Answers2

1

If I were doing this I would rig my WordPress site to publish an RSS feed of the articles you're trying to present, and use some JavaScript to present that feed on your customized PHP web site.

There are WordPress plugins and JavaScript components available to do all that.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
0

So here is my revised php

<section id="blog">
<div class="container-fluid">
    <div class="row">
<div id="featured_posts">

<?php

// output RSS feed to HTML
output_rss_feed('http://www.bmcsquincy.com/featured_posts/feed', 20, true, true, 200);

// Check http://www.systutorials.com/136102/a-php-function-for-fetching-rss-feed-and-outputing-feed-items-as-html/ for description
// RSS to HTML
/*
    $item_cnt: max number of feed items to be displayed
    $max_words: max number of words (not real words, HTML words)
    if <= 0: no limitation, if > 0 display at most $max_words words
 */
function get_rss_feed_as_html($feed_url, $max_item_cnt = 10, $show_date = true, $show_description = true, $max_words = 0, $cache_timeout = 7200, $cache_prefix = "/tmp/rss2html-")
{
    $result = "";
    // get feeds and parse items
    $rss = new DOMDocument();
    $cache_file = $cache_prefix . md5($feed_url);
    // load from file or load content
    if ($cache_timeout > 0 &&
        is_file($cache_file) &&
        (filemtime($cache_file) + $cache_timeout > time())) {
            $rss->load($cache_file);
    } else {
        $rss->load($feed_url);
        if ($cache_timeout > 0) {
            $rss->save($cache_file);
        }
    }
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
        $content = $node->getElementsByTagName('encoded'); // <content:encoded>
        if ($content->length > 0) {
            $item['content'] = $content->item(0)->nodeValue;
        }
        array_push($feed, $item);
    }
    // real good count
    if ($max_item_cnt > count($feed)) {
        $max_item_cnt = count($feed);
    }
    $result .= '<ul class="feed-lists">';


    //ADDED THIS FOR POST AMOUNT
    $max_item_cnt = 3;

    for ($x=0;$x<$max_item_cnt;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $result .= '<li class="feed-item">';
        $result .= '<div class="feed-title"><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong></div>';
        if ($show_date) {
            $date = date('l F d, Y', strtotime($feed[$x]['date']));
            $result .= '<small class="feed-date"><em>Posted on '.$date.'</em></small>';
        }
        if ($show_description) {
            $description = $feed[$x]['desc'];
            $content = $feed[$x]['content'];
            // find the img
            $has_image = preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $image);
            // no html tags
            $description = strip_tags($description, '');
            // whether cut by number of words
            if ($max_words > 0) {
                $arr = explode(' ', $description);
                if ($max_words < count($arr)) {
                    $description = '';
                    $w_cnt = 0;
                    foreach($arr as $w) {
                        $description .= $w . ' ';
                        $w_cnt = $w_cnt + 1;
                        if ($w_cnt == $max_words) {
                            break;
                        }
                    }
                    $description .= " ...";
                }
            }
            // add img if it exists



                //ADDED THE P IN DESCRIPTION LINE TO FOR A BREAK BY IMAGE

                if ($has_image == 1) {
                    $description = '<p> <img class="feed-item-image" src="' . $image['src'] . '" /></p>' . $description;
                }
                $result .= '<div class="feed-description">' . $description;

                //ADDED THE P IN THIS TO LINE BREAK CONTINUE READING

                $result .= '<p> <a href="'.$link.'" title="'.$title.'">Continue Reading &raquo;</a></p>'.'</div>';
            }
            $result .= '</li>';
        }
        $result .= '</ul>';
        return $result;
    }
    function output_rss_feed($feed_url, $max_item_cnt = 10, $show_date = true, $show_description = true, $max_words = 0)
    {
        echo get_rss_feed_as_html($feed_url, $max_item_cnt, $show_date, $show_description, $max_words);
    }
    ?>

    </div><!--END FEATURED POSTS-->
        </div><!--END ROW-->
        </div><!--END CONTAINER-->
    </section><!--END SECTION BLOG-->       

Here is my css

#blog {
    background-color: yellow;
    color: #dbdbdb;
    width: 100%;
    padding-top: 100px;
    padding-bottom: 100px;
    margin: 0px auto 0px auto;
}

#featured_posts {
    background-color: pink;
    max-width: 1200px;
    margin: 0px auto 0px auto;
    padding: 5px;
}

#featured_posts ul {
    display: inline-block;
    margin: 0px auto 0px auto;
    text-align: center;
    padding: 0px;
}

#featured_posts li {
    list-style-type: none;
    text-align: left;
    padding: 30px;
    float: left;
    font-family: pathwaygothic;
    font-size: 1em;
    background-color: purple;
    max-width: 1200px;
    margin-left: 25px;
}

.feed-lists {
  background-color: aqua;
  width: 100%;

}

.feed-title a {
    color: red;
}

.feed-date {
    color: aqua;
}

.feed-description {
    width: 300px;
}

.feed-lists li {
    background-color: green;
}
  • You would probably want to wrap those `

    `s in a container (like a `

    `) and set those as either `display: inline-block;`with a set width, or use `display: flex;` or something (or even `float: left;`) to get them to line up.
    – junkfoodjunkie Oct 27 '16 at 21:15
  • Okay junkfoodjunkie. Two last questions and I swear I will stop bothering you! I have tried everything I know about php to figure how to get description amount to a certain amount. I can't figure it out! also the last question I have is when I plug in the css and everything my 3 posts are almost centered, but they are still to far left and I cannot figure out why! Any help would greatly be appreciated! First things first, you were right about rss being able to show images! – Digital_Jedi_3 Oct 28 '16 at 21:11