SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Hybrid View

  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pull out 3 posts using WP

    Hey,

    I am using WP for the first time, and have managed to pull out the posts, see
    <snip>

    Now that i have done that i want to insert the 3 latest posts but on my homepage, see
    <snip>

    On the home page look at the box in the content which says "Latest from Mey Selections"..

    How can i pull out the 3 latest posts on this page even though the page is not split into header, sidebar, index, and footer..?

    I have seen it on other websites such as http://ringspun.co.uk

    If you look near the bottom you can see what i mean?

    Any help would be much appreciated.

    Thanks
    Regards
    Last edited by Shyflower; Jul 24, 2009 at 05:48. Reason: removed member link. No links to your site allowed here.

  2. #2
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have much time right now, but you might find this page useful:

    http://codex.wordpress.org/Integrati...h_Your_Website

    Get back to us if you have any problems in implementing the above.

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Let me explain what i am trying to do i think i have not exlained. I have made my website using normal CSS etc.. (Without a wordpress theme)

    Then i have uploaded a blog and on the main website i have a link like this:-

    www.abc.com/blog

    This then takes me to my blog.

    however now what i want to do is display the 3 latest posts of the blog on my main website home page..

    I have tried using this code:-

    <?php
    $posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
    foreach ($posts as $post) : start_wp(); ?>
    <?php the_date(); echo "<br />"; ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php
    endforeach;
    ?>

    Now this code only works in my blog pages (index.php)

    Buy my home page website which is default.php comes up with this error when i try using the same code:-

    Fatal error: Call to undefined function get_posts() in /domains/listentotheconstituents.com/http/new_mey/index.php on line 143

    I did try to add this line:-

    <?php
    require('/the/path/to/your/wp-blog-header.php');
    ?>

    But i dont know what path to put in? For exmaple my blog is in new_mey/blog/wp-content/themes/mey_selections

    Regards

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    You should remember that the code you are using is wordpress and I understand that the default.php is not a wordpress file. In order to use wordpress functions you would need to require the needed function files (the file is mentioned on the page the function is explained). If these are included (or required) the functions should be able to be used.
    I hope that this is able to help you.

  5. #5
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for replying. So why do i need to require the header in the blog?

    If i use this code:-


    // Get the last 3 posts.
    <?php
    require('/blog/path/to/your/wp-blog-header.php');
    ?>

    <?php query_posts('showposts=3'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
    <?php endwhile;?>

    What would the required path be because i keep receving the following error


    Warning: require(/blog/wp-content/themes/mey_selections/index.php) [function.require]: failed to open stream: No such file or directory in /domains/listentotheconstituents.com/http/new_mey/food.php on line 117

    Fatal error: require() [function.require]: Failed opening required '/blog/wp-content/themes/mey_selections/index.php' (include_path='.:/usr/share/php:/data/lib/css:/data/lib/js:/data/lib/php:./inc:./inc/Classes:./Classes:./template') in /domains/listentotheconstituents.com/http/new_mey/food.php on line 117

    The path is blog/wp-content/themes/mey_selections/header.php

    Thanks
    Regards

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to include the correct functions pages (for example the get_search_form() is located at wp-includes/general-template.php). First of all, a function can not be executed if there are no definition for the function (which is somewhere in the wp-includes directory). The function list wp uses is located here: http://codex.wordpress.org/Function_Reference/. The theme files is called only to build the page to be viewed and would not require the needed function files.
    I am not sure why it gives the errors because if the path is correct, it should work (require and include both try the path relative to the document the calls them and if that does not work, even try from a level up). You might try using include rather than require as this will continue the page and might give info that can help solving the problems.

  7. #7
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I have managed to correct the path directory, so now i am not getting that error. However i am now receving this line of error:-

    Fatal error: Call to undefined function bloginfo() in /domains/listentotheconstituents.com/http/new_mey/blog/wp-content/themes/mey_selections/header.php on line 6

    Line 6 in header.php has the following code:-

    <title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> &raquo; Blog Archive <?php } ?> <?php wp_title(); ?></title>

    What does this mean, sorry for all the questions i am WP newbie :-)

    Regards

  8. #8
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please paste the whole of your code of that particular page, somehow I don't really follow you at the moment what you have done and what you haven't done. Everything basically should work easily if you just followed the page I linked to.

    And please wrap your code in [ HIGHLIGHT = "PHP" ] [ /HIGHLIGHT ]

  9. #9
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry im new to this forum, dont really know my way around.

    Anyway,

    I have followed a tutorial to create a wordpress theme for my blog, follow the link in previous posts.

    Now that all works fine. BUT now i want to display the 3 latest posts on my homepage. My homepage is not using a wordpress theme.. So how can i go about displaying it?

    I did try a SQL select statement but that all went wrong. So i followed the link that you suggested.

    I now have the following code on my homepage for displaying the latest posts:-

    PHP Code:
            <?php
            
    // Get the last 3 posts.
            
    include("blog/wp-content/themes/mey_selections/header.php");
            include(
    "blog/wp-content/themes/mey_selections/index.php");
            include(
    "blog/wp-content/themes/mey_selections/sidebar.php");
            include(
    "blog/wp-content/themes/mey_selections/footer.php");
            
    ?>
            
            <?php query_posts('showposts=3'); ?>
            <?php while (have_posts()) : the_post(); ?>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
            <?php endwhile;?>
    Do you know why this does not work?

    Thanks for your help..

  10. #10
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The file path is correct but the error is in the header.php page line 6 which is:-

    PHP Code:
    <title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> &raquo; Blog Archive <?php ?> <?php wp_title(); ?></title>
    hope this helps identify the problem, i dont know why it does not work, i understand the procedure and it makes sense but for some reason the error message keeps popping up..

  11. #11
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry had to go briefly out of the house, let's see, you are misunderstanding something here. You don't need your wordpress theme, you need the wordpress functions. Thus that tutorial doesn't ask you to include your theme header, index, sidebar, footer, etc. but the wp-blog-header.php file located in the blog folder, so in your case:

    Code PHP:
    <?php
    require('/new/blog/wp-blog-header.php');
    ?>
     
            <?php query_posts('showposts=3'); ?>
            <?php while (have_posts()) : the_post(); ?>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
            <?php endwhile;?>

  12. #12
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    that works now, the reason i was finding it hard is because im new to WP so dont know exactly how it works..

    Just a slight problem though, the results are showing only the first post date. See below:-


    Mey Selections becomes first Scottish consumer goods company to put carbon labels on products
    July 24, 2009
    Steven Gerrard has been given the all clear..

    Welcome to Mey Selections

    Now it should have the date the post was made after each post title, but it only does this for the first post?

    Thanks alot for your help.

    Regards

  13. #13
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    <?php
    require('/new/blog/wp-blog-header.php');
    ?>
    
    <?php
    $posts = get_posts('numberposts=3');
    foreach ($posts as $post) : start_wp(); ?>
    <div><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    <div><?php the_date();?></div>
    <?php
    endforeach;
    ?>
    P.S. I haven't really done much with Wordpress myself, I'm primarily using Drupal.
    Last edited by c2uk; Jul 24, 2009 at 09:15. Reason: minor corrections

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would find a theme that does what you want. If that doesn't work then find a theme that does what you want and look at the source code to see how they are doing it. Sometimes reinventing the wheel is an exercise in frustration.

  15. #15
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MarketingIQ View Post
    I would find a theme that does what you want. If that doesn't work then find a theme that does what you want and look at the source code to see how they are doing it. Sometimes reinventing the wheel is an exercise in frustration.
    There is no reinventing the wheel involved in this at all. The code and examples are all provided right here. It probably takes more time to find a theme that does what the OP is looking for, implementing it the way he wants it, than to adjusting the 5 lines of code given above.

  16. #16
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I appreciate all the help so far. The code still does not show the date, i understand how it works as first the title of the post is shown as a link to the post itself. Then the date is shown below.. I have altered the code slightly for styling purposes to this:-

    PHP Code:
            <?php
            
    require('../new_mey/blog/wp-blog-header.php');
            
    ?>
             
            <?php
            $posts 
    get_posts('numberposts=3');
            foreach (
    $posts as $post) : start_wp(); ?>
            <b><a href="<?php the_permalink(); ?>" style="color:#fff"><?php the_title(); ?></a></b><br/>
            <?php the_date(); ?><br/>
            <?php endforeach; ?>
            </p>
    take a look at what this is showing me:-

    http://www.listentotheconstituents.com/new_mey/



    As far as i understand the code it should be displaying the date, could this mean that a date is not being stored in the database? I have checked to make sure that there is a date so im stuck now..

    Regards

  17. #17
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    In your theme (default) there is the following code which makes a link in the title and display the date underneath. Try to implement this into your page. (Change the "j F Y" to what you need for your date according to the php date function).
    Code php:
    <?php while (have_posts()) : the_post(); ?>
    		<div <?php post_class() ?>>
    				<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permalink to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    				<small><?php the_time('j F Y') ?></small>

  18. #18
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, please, forget using <b> this is a deprecated tag (you should be using <strong>, but in this case, there are other alternatives) and inline styles like this (use classnames und unique! IDs (again there are a couple of multiple IDs on your site, you need to fix this) and style those in your stylesheet), also while it's probably semantically correct to use the line break between the title and the date, you shouldn't put another line break after the date (there are lots of line breaks that should be avoided on your page, some are correct though), put everything in a div, give the div a class and then style it accordingly, this is all for maximum SEO and accessibility. There are a couple of other things that can and should be done to increase your SEO and accessibility, at the moment, Google thinks your site is all about a Shopping cart, due to it being enclosed in heading 1, in theory, your site title should be enclosed in a h1, the shopping cart probably in a h2, just like hotel of the month and latest from.

    Now to the real problem, well Jacotheron was faster.

  19. #19
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    c2uk thanks alot for the advice i didnt think that something llike h1 would have such an impact on SEO.

    Now i have a question about the code that has been suggested, do i include this in the index.php page where i want to display the title and the date?

    If so i am gettint an error about syntax error:-

    PHP Code:
            <?php while (have_posts()) : the_post(); ?>
            [B]<div <?php post_class() ?>>[/B]
                    <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permalink to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                    <small><?php the_time('j F Y'?></small>
            <div class="align_right" style="margin-right:11px;margin-top:-18px">
            <a href="blog">View our blog now &gt;&gt;</a>
            </div>
    On the line in bold.. Is this meant to be a class?

    Regards

  20. #20
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The class it calls is a class that is custom for each post (for example <div class="post-243 post hentry category-your-category">). You can specify a class for it to use in stead of the function to get a class. (I think that this class is generated for each post and not stored somewhere). The class only specify margin and text align. I hope this can help you.

  21. #21
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I keep getting a syntax error message when running the code that has been suggested..

    Parse error: syntax error, unexpected $end in /domains/listentotheconstituents.com/http/new_mey/index.php on line 272
    Line 272 is the end of the page where the body ends. Do you know what the problem is here?

    Thanks and sorry for all the questions

  22. #22
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    When this happens, you are missing one closing "}". The script can't tell you exactly where it is missing, so you would need to go through every line to make sure that each "{" have an "}". Good program that helps me with this is Notepad++ - when clicked next to one it highlights the other, then you just make sure that it is on the right place.
    I hope that this is able to help.

  23. #23
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php endwhile; ?>
    is missing in Jacotheron's code snippet (goes below the last line of your snippet).

  24. #24
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats perfect, thanks alot it works fine now with the date showing after each post.

    Much appreciated.

  25. #25
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nherbal View Post
    sorry but wordpress forum will help you,

    Thanks
    If I'm not mistaken then the Sitepoint forum has already helped him? And from my experience the Wordpress forum is not as helpful as the Sitepoint forum.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •