Wordpress Post content layout - php

I am building a custom theme from scratch and come across a slight problem that I need some help with.
So I have on my front-page a list of the 3 latest blog posts showing the 'title', 'Excerpt' & a 'more...' link which both the title and more link take you to single.php.
I am generating the post content in a file named 'content-post.php' which is the following:
<div class="clearfix">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
<header class="title">
<h3><?php the_title(); ?></h3>
</header>
<ul class="info">
<li><?php the_category(', '); ?> | <?php the_time('F Y'); ?></li>
<!--<li>Written By: <a href="<?php bloginfo('siteurl') ?>/about/"><?php the_author(); ?></li>-->
</ul>
<div class="excerpt">
<p><?php if(is_single()): ?>
<?php the_content(); ?>
<?php comments_template(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<a class="post-link" href="<?php the_permalink(); ?>">More...</a></p>
<?php endif; ?>
</div>
</div>
This builds the posts out to front-page.php just fine. I am having the problem when you go into the blog page which is using the same post content and the layout is the same. Is there a way I can specify how it shows on the front-page and how it appears on the blog page?
The post is being displayed on front-page.php like this:
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; endif; ?>
And on the blog page like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; else: ?>
<p>There are no posts to display or pages here</p>
<?php endif; ?>

If I understand your question, try this:
<?php if (is_page(page id)){your query}
Good luck! ;)
PS.. or on your way:
<?php if (is_page(page id)): ?>
your query
<?php endif; ?>

Related

Secondary menu not showing up in wordpress footer

I am a beginner trying to create my own wordpress theme.
I have two nav menus one for the header one for the footer but the secondary one for the footer is not showing up. My code is currently as shown below.
- Functions.php
function base_theme_setup(){
add_theme_support('menus');
register_nav_menu('primary','Primary Header Navigation');
register_nav_menu('secondary','Secondary Footer Navigation');
}
add_action ('init', 'base_theme_setup');
- footer.php
<footer>
<?php wp_nav_menu(array('theme_location'=>'secondary')); ?>
</footer>
<?php wp_footer (); ?>
</body>
</html>
Managed to solve it.
Had some useless undeleted code at the bottom of my index.php. Once deleted the problem went away.
So went from this
<?php
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<h3><?php the_title(); ?>
</h3>
<div class="thumbnail-img"><?php the_post_thumbnail('large'); ?></div>
<p><?php the_content(); ?></p>
<small>Posted on: <?php the_time('F j,Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category(); ?></small>
<hr>
<?php endwhile;
endif;
?>
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>
<?php get_footer();?>
to this
<?php
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_post_thumbnail('large'); ?>
<p><?php the_content(); ?></p>
<small>Posted on: <?php the_time('F j,Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category();?></small>
<hr>
<?php endwhile;
endif;
?>
<?php get_footer();?>
Thanks for everyones tips!

Combining wordpress admin page content with custom template?

Is there a way to combine content from a wordpress page's main textarea with the content in its custom template?
In this case I have a custom template that displays all posts from a single category, but I would also like to have a section that displays what is written in the wordpress admin Page area.
This is how I have the custom template set up to display relevant posts:
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Above this I would like to have the wordpress pages admin area content display, what a user would normally write into the textarea to display on the page, is this possible?
This is what I've come up with:
<?php get_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content() ;?>
<?php endwhile; ?>
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Is this acceptable? It's working but I'm not sure how classy it is!

PHP codes for wordpress

Hello guys so I am making a car review wordpress magazine and I am having issues with php codes as I am not a great programmer. Actually on this page Memes I would like the social plugins to be below each picture and not on the top furthermore I would like it to display the post date and the author of the post. Some formatting for the pictures size etc would be great too. Below is the code I am using
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content" class="archive <?php if(get_option('colabs_layout_settings')=='two-col-right'){echo 'right';}else{?>left<?php }?>">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class(); ?> id="post_<?php the_ID(); ?>">
<span id="map"><?php _e('Home','colabsthemes');?> » <?php the_title(); ?></span>
<h2 class="title"><?php the_title(); ?></h2>
<div class="entry" style="padding-top:15px;">
<?php the_content(__('<p>Read the rest of this page »</p>','colabsthemes')); ?>
<?php echo colabs_share();?>
<?php wp_link_pages(array('before' => __('<p><strong>Pages:</strong>','colabsthemes'), 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<div id="sidebar" class="<?php if(get_option('colabs_layout_settings')=='two-col-right'){echo 'left';}else{?>right<?php }?>">
<?php get_sidebar(); ?>
</div>
<div> <?php get_footer(); ?> </div>
Finally for the review part I would like to do something like Autotest/review but I don't know which code to use to show these kind of square articles. Please help me out.
For adding the author and date of the post, you can add:
<p>Written by:
<?php get_the_author(); ?></p>
get_the_author() will display the author's name as is set in their "Display name publicly as" field in their user profile (Administration > Users > Your Profile).
To also display the date:
<p>Written by:
<?php get_the_author(); ?> on
<?php get_the_date(); ?></p>
I don't see where you're using the social sharing links on the page you linked to - did you remove them?
For more information:
http://codex.wordpress.org/Function_Reference/get_the_author
http://codex.wordpress.org/Function_Reference/get_the_date

Wordpress: Check posts have comments allowed

I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks

Wordpress not rendering correctly & Multiple posts

I've been stuck on this since last night and cant' figure out why the "Work" section of my wordpress doesn't show the the_title. Also, my "work" page is set to the "Work Page" template. Also, how would I go about having multiple posts on this page? Thanks!
http://www.listentotheway.com
<?php
/*
Template Name: Work Page
*/
get_header(); ?>
<p> This is the work.php file </p>
<?php
$args = array( 'post_type'=>'work' );
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><a href="<?php the_permalink() ;?>"<?php the_title(); ?></h3>
<?php the_field( 'description' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
<?php get_footer(); ?>
To summarise my suggestion so far:
<?php if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<h3><a href="<?php the_permalink() ;?>"<?php the_title(); ?></h3>
<?php // the_field( 'description' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
I have coded out the words the_field, there is no such native Wordpress function. Try my code an see what happens!

Categories