Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End My First WordPress Theme – PHP and function output problems

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #196509

    My first wordpress theme is giving me some trouble. I know it’s pretty buggy, so my apologies in advance. A lot of that is due to syntax errors, I’m sure, as I’m really, really new to PHP and js. For instance, in the Codenvy project I’m sharing here, I know I don’t need to put “echo” at the start of every line, but it’s the only way I was able to get it to function at all.

    The beta site is up here. Please note that the site at the root of that address is not my work.

    • One of the main problems I’m having is that in the right hand column of the main content area of the home page, in each category, only the first blog post excerpt title shows the date of the post.
    • You’ll also see in the 4th box in the right hand column, the author’s name is repeated twice. Same thing in the next box.

    Any help is appreciated!

    #196542
    Alen
    Participant

    @adjmedia Links do not work for me.

    #196543

    Sorry about that. The beta site is at http://www.akilahtzuberi.com/blog. I’ll have to grab the other link later.

    #196547
    Senff
    Participant

    It’s impossible for us to diagnose some of these problems if we don’t see the source code, and only the output.

    I suggest you put your theme on Github or something, so we can see all the source PHP. Alternatively, isolate the code that’s giving you trouble and post that here (if you know the exact parts of the code that are relevant).

    #196548

    Sorry, I thought that codenvy link would show the source code for you (you have to double click on the project name for it to open), Here is the source code that is outputting the blog posts in the right hand column of the main content area (the middle column if you count the sidebar). The live site is at http://akilahtzuberi.com/blog.

    <?php
    //OUTPUT SECTION 1 POSTS
    $args = array( 'post_type' => 'section-1', 'posts_per_page' => 3 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div class="post_introbox">';
    echo '<div class="post_titlebox">';
    echo '<h3 id="post-<?php the_ID(); ?>"><a href="';
    echo the_permalink();
    echo  '">';
    echo  the_title();
    echo '</a></h3>';
    echo '<p><span><a href="author/"';
    echo get_the_author_link();
    echo '</span></a>';
    //echo the_author(); </span></a>;
    echo ' • ';
    echo  the_date('m.d.Y'); '</p>';
    echo '</div>'; //close titlebox
    echo '<div class="containerz">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo add_image_size( 'front-featured', 374, 250, array( 'center', 'center' ) ); // Hard crop left top 
    
    if ( has_post_thumbnail() ) {; // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail( 'front-featured' );
    } 
    
    echo '</a></div>';
    echo '<div class="section_link_box"> '; 
    
    echo '<span class="section1">';
    echo '<a href="/blog/';
    echo get_post_type( $post );
    echo '">';
     $category = get_the_category( $custompost );
    echo $category[0]->cat_name;
    echo '</a></span></div>';//CLOSE SECTION_LINK_BOX
    echo  '<div class="spacer_one"></div>'; 
    echo '<div class="introtext section1">';
    
    echo the_excerpt();
    echo   '<p class="more">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo 'Continue Reading</a></p>';
    echo '<div class="clearfloat"></div>';
    echo '</div></div>';  //close introtext
    
     endwhile;
    
    ?>
    
    
    <?php
    //OUTPUT SECTION 2 POSTS
    $args = array( 'post_type' => 'section-2', 'posts_per_page' => 2 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div class="post_introbox">';
    echo '<div class="post_titlebox">';
    echo '<h3 id="post-<?php the_ID(); ?>"><a href="';
    echo the_permalink();
    echo  '">';
    echo  the_title();
    echo '</a></h3>';
    echo '<p><a href="author/';
    echo get_the_author();
    echo '"><span>';
    echo the_author(); '</span></a>';
    echo ' • ';
    echo  the_date('m.d.Y'); '</p>';
    echo '</div>'; //close titlebox
    echo '<div class="containerz">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo add_image_size( 'front-featured', 374, 250, array( 'center', 'center' ) ); // Hard crop left top 
    
    if ( has_post_thumbnail() ) {; // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail( 'front-featured' );
    } 
    
    echo '</a></div>';
    echo '<div class="section_link_box"> '; 
    echo '<span class="section2">';
    echo '<a href="/blog/';
    echo get_post_type( $post );
    echo '">';
     $category = get_the_category( $custompost );
    echo $category[0]->cat_name;
    echo '</a></span></div>';//CLOSE SECTION_LINK_BOX
    echo  '<div class="spacer"></div>'; 
    echo '<div class="introtext section2">';
    
    echo the_excerpt();
    echo   '<p class="more">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo 'Continue Reading</a></p>';
    echo '<div class="clearfloat"></div>';
    echo '</div></div>';  //close introtext
    
     endwhile;
    
    echo wp_reset_query();
    
    
    ?>
    
    <?php
    //OUTPUT SECTION 3 POSTS
    $args = array( 'post_type' => 'section-3', 'posts_per_page' => 2 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div class="post_introbox">';
    echo '<div class="post_titlebox">';
    echo '<h3 id="post-<?php the_ID(); ?>"><a href="';
    echo the_permalink();
    echo  '">';
    echo  the_title();
    echo '</a></h3>';
    echo '<p><a href="author/';
    echo get_the_author();
    echo '"><span>';
    echo the_author(); '</span></a>';
    echo ' • ';
    echo  the_date('m.d.Y'); '</p>';
    echo '</div>'; //close titlebox
    echo '<div class="containerz">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo add_image_size( 'front-featured', 374, 250, array( 'center', 'center' ) ); // Hard crop left top 
    
    if ( has_post_thumbnail() ) {; // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail( 'front-featured' );
    } 
    
    echo '</a></div>';
    echo '<div class="section_link_box"> '; 
    echo '<span class="section3">';
    echo '<a href="/blog/';
    echo get_post_type( $post );
    echo '">';
     $category = get_the_category( $custompost );
    echo $category[0]->cat_name;
    echo '</a></span></div>';//CLOSE SECTION_LINK_BOX
    
                      echo  '<div class="spacer"></div>'; 
    echo '<div class="introtext section3">';
    
    echo the_excerpt();
    echo   '<p class="more">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo 'Continue Reading</a></p>';
    echo '<div class="clearfloat"></div>';
    echo '</div></div>';  //close introtext
    
     endwhile;
    echo wp_reset_query();
    
    ?>
    
    <?php
    //OUTPUT SECTION 4 POSTS
    $args = array( 'post_type' => 'section-4', 'posts_per_page' => 2 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div class="post_introbox">';
    echo '<div class="post_titlebox">';
    echo '<h3 id="post-<?php the_ID(); ?>"><a href="';
    echo the_permalink();
    echo  '">';
    echo  the_title();
    echo '</a></h3>';
    echo '<p><a href="author/';
    echo get_the_author();
    echo '"><span>';
    echo the_author(); '</span></a>';
    echo ' • ';
    echo  the_date('m.d.Y'); '</p>';
    echo '</div>'; //close titlebox
    echo '<div class="containerz">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo add_image_size( 'front-new644-featured', 1000, 582, array( 'top', 'left' ) ); // Hard crop left top 
    
    if ( has_post_thumbnail() ) {; // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail( 'front-featured' );
    } 
    
    echo '</a></div>';
    echo '<div class="section_link_box"> '; 
    echo '<span class="section4">';
    echo '<a href="/blog/';
    echo get_post_type( $post );
    echo '">';
     $category = get_the_category( $custompost );
    echo $category[0]->cat_name;
    echo '</a></span></div>';//CLOSE SECTION_LINK_BOX
    
                      echo  '<div class="spacer"></div>'; 
    echo '<div class="introtext section4">';
    
    echo the_excerpt();
    echo   '<p class="more">';
    echo   '<a href="';
    echo the_permalink(); 
    echo '">';
    echo 'Continue Reading</a></p>';
    echo '<div class="clearfloat"></div>';
    echo '</div></div>';  //close introtext
    
     endwhile;
    
    echo wp_reset_query();
    ?>
    
    #196556
    Anonymous
    Inactive

    There’s definitely some incorrect code in there:

    echo '&lt;h3 id="post-&lt;?php the_ID(); ?&gt;"&gt;&lt;a href="';
    

    And I suspect:

    echo '<p><span>&lt;a href="author/"';
    echo get_the_author_link();
    echo '</span></a>';
    //echo the_author(); </span></a>;
    

    Wasn’t doing what you were expecting it to either. As far as I can tell from the documentation, get_the_author_link() spits out the anchor tag anyway.

    As you rightly acknowledge, the repeated echos are unnecessary and make the code much harder to visualise. I’ve cleaned up the first section in a pastebin:

    http://pastebin.com/EKJmGzz2

    That should be functionally the same, although I did take the liberty of removing the malformed lines. Hopefully the problems won’t persist after you’ve applied the same principle to the rest of the code.

    #196565
    Alen
    Participant

    @adjmedia just a note, in @BenWalker code you don’t need to put echo in front of &lt;?php the_title(); ?&gt;.

    In WordPress if the function starts with the_ it already echos the value. If you’re trying to get the value and do something with it you’ll need to use:

    $author = get_the_author();
    echo $author;
    
    # is the same as
    
    the_author();
    
    #197030

    @BenWalker and @Alen thank you both for your help! This is definitely an improvement, markup wise, but the lack of a date in subsequent posts after the first post in a post type is still a problem. Any tips? Do I need to reset the query? I think I’ve done that, but doesn’t seem to be helping. The live site is at akilahtzuberi.com/blog. Here is the section 1 code that @BenWalker cleaned up for me with the edits that @Alen suggested.

    Unfortunately, my client only wants to show two articles total in one post type for the next month, so it’s not possible to test the subsequent post type sections in a way that I can share. Maybe I can work something out on github.

    #197043
    Alen
    Participant

    but the lack of a date in subsequent posts after the first post in a post type is still a problem. Any tips?

    @adjmedia

    Try using wp_reset_query() at the end of each output section. Note that in your code above you have echo wp_reset_query(); you don’t need echo here, this function sort of “rewinds” stuff back, and it’s probably the reason your dates don’t work. In some cases your widgets will break as well.

    #197832

    Hello, so I think I’ve resolved the problem on the home page by changing “the_date(m.d.Y)” to “the_time(m.d.Y)” as suggested in this forum.

    I’m still having the same trouble with the date on my archive pages, but there I’m using:

    <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
    

    I’m going to try to play around with this for a while longer because I like that you get the author name and the date for free with that bit of code, rather than having to ask for each individually, but I’ll likely set up my archive pages (i.e. http://www.akilahtzuberi.com/blog/section-1/) like the home page and use “the_time()” instead.

    #197833

    Also, I tired the wp_reset_query() suggestion but it doesn’t seem to solve the problem. That was my hunch too, but unless I’m misusing the function somehow, it doesn’t seem to be having an effect.

Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Back End’ is closed to new topics and replies.